1

Add main business logic of this program

Now we can download torrents like old Python
version does.
This commit is contained in:
2019-09-28 22:51:10 +04:00
parent a599d898b0
commit 227d974e37
11 changed files with 371 additions and 22 deletions

View File

@@ -19,7 +19,7 @@ func downloadAdditionalPages() {
for i := range forumPagesLinks {
forumPage, _ := strconv.Atoi(i)
dlog.Info().Int("номер страницы", forumPage).Msg("Скачивается ещё одна страница форума")
pageFile, err := dumpForumPage("https://" + c.Config.URL + "/forum/" + forumPagesLinks[i])
pageFile, err := downloadFile("https://"+c.Config.URL+"/forum/"+forumPagesLinks[i], true)
if err != nil {
dlog.Fatal().Err(err).Msg("Не удалось загрузить страницу форума")
}
@@ -28,7 +28,7 @@ func downloadAdditionalPages() {
}
}
func dumpForumPage(url string) (string, error) {
func downloadFile(url string, transformEncoding bool) (string, error) {
req, err := http.NewRequest(http.MethodGet, url, nil)
if err != nil {
return "", err
@@ -63,10 +63,21 @@ func dumpForumPage(url string) (string, error) {
}
defer tempF.Close()
respInUTF8 := transform.NewReader(resp.Body, charmap.Windows1251.NewDecoder())
_, err = io.Copy(tempF, respInUTF8)
if err != nil {
return "", err
if transformEncoding {
respInUTF8 := transform.NewReader(resp.Body, charmap.Windows1251.NewDecoder())
_, err = io.Copy(tempF, respInUTF8)
if err != nil {
return "", err
}
} else {
_, err = io.Copy(tempF, resp.Body)
if err != nil {
return "", err
}
}
if uberDebug {
dlog.Debug().Str("расположение", tempF.Name()).Msg("Загружен файл из сети")
}
return tempF.Name(), nil