Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions src/pkg/parser/rosstat/rosstat/config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,12 +34,12 @@ func NewConfig() *Config {
},
DownloadHTMLMaxAttempts: 6,
DownloadHTMLTimeoutSeconds: 5,
DownloadHTMLBatchSize: 5,
DownloadHTMLBatchSize: 10,
DownloadHTMLTimeSleepSeconds: 2,
DownloadCSVMaxAttempts: 8,
DownloadCSVTimeoutSeconds: 5,
DownloadCSVBatchSize: 5,
DownloadCSVTimeSleepSeconds: 3,
DownloadCSVBatchSize: 10,
DownloadCSVTimeSleepSeconds: 2,
}
}

Expand Down
125 changes: 65 additions & 60 deletions src/pkg/parser/rosstat/rosstat/downloader/downloader.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,8 +34,6 @@ func DownloadCSV(ctx context.Context, subjectCode int, indicator int, codes []in

bodyStr := params.buildRequestBodyStr()
attempts := 0
var resp *http.Response
var err error

for attempts < config.DownloadCSVMaxAttempts {
req, err := http.NewRequestWithContext(ctx, "POST", url, strings.NewReader(bodyStr))
Expand All @@ -57,44 +55,48 @@ func DownloadCSV(ctx context.Context, subjectCode int, indicator int, codes []in
},
}

resp, err = client.Do(req)
if err == nil {
break
resp, err := client.Do(req)
if err != nil {
if resp != nil {
resp.Body.Close()
}
attempts++
time.Sleep(time.Duration(config.DownloadHTMLTimeSleepSeconds) * time.Second)
continue
}
attempts++
time.Sleep(time.Duration(config.DownloadCSVTimeSleepSeconds) * time.Second)
}

if err != nil {
return "", fmt.Errorf("error executing request: %w", err)
}
if err != nil {
return "", fmt.Errorf("error executing request: %w", err)
}

defer resp.Body.Close()
if resp.StatusCode != http.StatusOK {
log.Printf("ERROR: unexpected status %d: code: %d\n", resp.StatusCode, subjectCode)
continue
}

if resp.StatusCode != http.StatusOK {
body, _ := io.ReadAll(resp.Body)
return "", fmt.Errorf("unexpected status %d: code: %d, body: %s", resp.StatusCode, subjectCode, string(body))
}
rawBody, err := io.ReadAll(resp.Body)
if err != nil {
return "", fmt.Errorf("reading body: %w", err)
}

rawBody, err := io.ReadAll(resp.Body)
if err != nil {
return "", fmt.Errorf("reading body: %w", err)
}
decoder := charmap.Windows1251.NewDecoder()
utf8Body, err := decoder.Bytes(rawBody)
if err != nil {
return "", fmt.Errorf("decoding Windows-1251: %w", err)
}

decoder := charmap.Windows1251.NewDecoder()
utf8Body, err := decoder.Bytes(rawBody)
if err != nil {
return "", fmt.Errorf("decoding Windows-1251: %w", err)
}
fileName := getCSVFileName(subjectCode, indicator)

fileName := getCSVFileName(subjectCode, indicator)
if err := os.WriteFile(fileName, utf8Body, 0644); err != nil {
return "", fmt.Errorf("error writing file %s: %w", fileName, err)
}

if err := os.WriteFile(fileName, utf8Body, 0644); err != nil {
return "", fmt.Errorf("error writing file %s: %w", fileName, err)
resp.Body.Close()
log.Printf("Saved CSV to %s\n", fileName)
return fileName, nil
}

log.Printf("Saved CSV to %s\n", fileName)
return fileName, nil
return "", fmt.Errorf("Failed CSV request by code %d\n", subjectCode)
}

func DownloadHTML(ctx context.Context, subjectCode int) (string, error) {
Expand All @@ -107,8 +109,6 @@ func DownloadHTML(ctx context.Context, subjectCode int) (string, error) {
body := url.Values{}
body.Set("pl", strconv.Itoa(config.GetPopulationIndicator(subjectCode)))
attempts := 0
var resp *http.Response
var err error

for attempts < config.DownloadHTMLMaxAttempts {
req, err := http.NewRequestWithContext(ctx, "POST", urlStr, strings.NewReader(body.Encode()))
Expand All @@ -126,44 +126,49 @@ func DownloadHTML(ctx context.Context, subjectCode int) (string, error) {
},
}

resp, err = client.Do(req)
if err == nil {
break
resp, err := client.Do(req)

if err != nil {
if resp != nil {
resp.Body.Close()
}
attempts++
time.Sleep(time.Duration(config.DownloadHTMLTimeSleepSeconds) * time.Second)
continue
}
attempts++
time.Sleep(time.Duration(config.DownloadHTMLTimeSleepSeconds) * time.Second)
}

if err != nil {
return "", fmt.Errorf("error executing request: %w", err)
}
if err != nil {
return "", fmt.Errorf("error executing request: %w", err)
}

defer resp.Body.Close()
if resp.StatusCode != http.StatusOK {
log.Printf("ERROR: unexpected status %d: code: %d\n", resp.StatusCode, subjectCode)
continue
}

if resp.StatusCode != http.StatusOK {
body, _ := io.ReadAll(resp.Body)
return "", fmt.Errorf("unexpected status %d: code: %d, body: %s", resp.StatusCode, subjectCode, string(body))
}
rawBody, err := io.ReadAll(resp.Body)
if err != nil {
return "", fmt.Errorf("reading body: %w", err)
}

rawBody, err := io.ReadAll(resp.Body)
if err != nil {
return "", fmt.Errorf("reading body: %w", err)
}
decoder := charmap.Windows1251.NewDecoder()
utf8Body, err := decoder.Bytes(rawBody)
if err != nil {
return "", fmt.Errorf("decoding Windows-1251: %w", err)
}

decoder := charmap.Windows1251.NewDecoder()
utf8Body, err := decoder.Bytes(rawBody)
if err != nil {
return "", fmt.Errorf("decoding Windows-1251: %w", err)
}
fileName := getHTMLFileName(subjectCode)

fileName := getHTMLFileName(subjectCode)
if err := os.WriteFile(fileName, utf8Body, 0644); err != nil {
return "", fmt.Errorf("error writing file %s: %w", fileName, err)
}

if err := os.WriteFile(fileName, utf8Body, 0644); err != nil {
return "", fmt.Errorf("error writing file %s: %w", fileName, err)
resp.Body.Close()
log.Printf("Saved HTML to %s\n", fileName)
return fileName, nil
}

log.Printf("Saved HTML to %s\n", fileName)
return fileName, nil
return "", fmt.Errorf("Failed HTML request by code %d\n", subjectCode)
}

func CleanupFile(path string) {
Expand Down
4 changes: 3 additions & 1 deletion src/pkg/parser/rosstat/rosstat/extractor/population.go
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,9 @@ func (ext *PopulationExtractor) getNameOrPopulation(row []string, manager *state
func (ext *PopulationExtractor) getPopulation(row []string, manager *state_manager.StateManager[dao.PopulationExtracted]) {
populationDAOs := make([]*dao.PopulationExtracted, 0, len(ext.years)-1)

for i := 1; i < len(ext.years); i++ {
maxIndex := min(len(ext.years), len(row))

for i := 1; i < maxIndex; i++ {
population, err := strconv.Atoi(row[i])

if err == nil {
Expand Down
Loading