Add basic HTTP errors handling
This commit is contained in:
parent
a504299150
commit
a599d898b0
domains
@ -6,11 +6,14 @@ package fetcherv1
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
"strconv"
|
"strconv"
|
||||||
|
"time"
|
||||||
)
|
)
|
||||||
|
|
||||||
func fetch(forumID int) {
|
func fetch(forumID int) {
|
||||||
startPage := "https://" + c.Config.URL + "/forum/viewforum.php?f=" + strconv.Itoa(forumID)
|
startPage := "https://" + c.Config.URL + "/forum/viewforum.php?f=" + strconv.Itoa(forumID)
|
||||||
|
|
||||||
|
time.Sleep(5 * time.Second)
|
||||||
|
|
||||||
startPageFile, err := dumpForumPage(startPage)
|
startPageFile, err := dumpForumPage(startPage)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
dlog.Error().Err(err).Msg("Не удалось получить данные с форума")
|
dlog.Error().Err(err).Msg("Не удалось получить данные с форума")
|
||||||
|
@ -9,6 +9,7 @@ import (
|
|||||||
"io/ioutil"
|
"io/ioutil"
|
||||||
"net/http"
|
"net/http"
|
||||||
"strconv"
|
"strconv"
|
||||||
|
"time"
|
||||||
|
|
||||||
"golang.org/x/text/encoding/charmap"
|
"golang.org/x/text/encoding/charmap"
|
||||||
"golang.org/x/text/transform"
|
"golang.org/x/text/transform"
|
||||||
@ -36,10 +37,24 @@ func dumpForumPage(url string) (string, error) {
|
|||||||
req.AddCookie(c.Cookies[i])
|
req.AddCookie(c.Cookies[i])
|
||||||
}
|
}
|
||||||
|
|
||||||
resp, err := dclient.Do(req)
|
var resp *http.Response
|
||||||
|
retryCount := 0
|
||||||
|
for {
|
||||||
|
if retryCount < 5 {
|
||||||
|
resp, err = dclient.Do(req)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
if uberDebug {
|
||||||
|
dlog.Debug().Err(err).Int("попытка", retryCount+1).Msg("Не удалось получить данные, пытаемся ещё раз")
|
||||||
|
}
|
||||||
|
time.Sleep(time.Second)
|
||||||
|
retryCount++
|
||||||
|
} else {
|
||||||
|
break
|
||||||
|
}
|
||||||
|
} else {
|
||||||
return "", err
|
return "", err
|
||||||
}
|
}
|
||||||
|
}
|
||||||
defer resp.Body.Close()
|
defer resp.Body.Close()
|
||||||
|
|
||||||
tempF, err := ioutil.TempFile("", "massdl-*")
|
tempF, err := ioutil.TempFile("", "massdl-*")
|
||||||
|
@ -10,6 +10,7 @@ import (
|
|||||||
"net/url"
|
"net/url"
|
||||||
"os"
|
"os"
|
||||||
"strings"
|
"strings"
|
||||||
|
"time"
|
||||||
)
|
)
|
||||||
|
|
||||||
// login function name is self-descriptive.
|
// login function name is self-descriptive.
|
||||||
@ -74,10 +75,22 @@ func obtainCookies() {
|
|||||||
req, _ := http.NewRequest(http.MethodPost, url, strings.NewReader(formData.Encode()))
|
req, _ := http.NewRequest(http.MethodPost, url, strings.NewReader(formData.Encode()))
|
||||||
req.Header.Set("Content-Type", "application/x-www-form-urlencoded; charset=utf-8")
|
req.Header.Set("Content-Type", "application/x-www-form-urlencoded; charset=utf-8")
|
||||||
|
|
||||||
resp, err := client.Do(req)
|
retryCount := 0
|
||||||
if err != nil {
|
var resp *http.Response
|
||||||
|
var err error
|
||||||
|
for {
|
||||||
|
if retryCount == 10 {
|
||||||
dlog.Fatal().Err(err).Msg("Не удалось отправить запрос на авторизацию в NNM-Club.")
|
dlog.Fatal().Err(err).Msg("Не удалось отправить запрос на авторизацию в NNM-Club.")
|
||||||
}
|
}
|
||||||
|
resp, err = client.Do(req)
|
||||||
|
if err != nil {
|
||||||
|
dlog.Debug().Err(err).Int("попытка", retryCount).Msg("Не удалось отправить запрос, попробуем ещё раз")
|
||||||
|
retryCount++
|
||||||
|
time.Sleep(2 * time.Second)
|
||||||
|
} else {
|
||||||
|
break
|
||||||
|
}
|
||||||
|
}
|
||||||
defer resp.Body.Close()
|
defer resp.Body.Close()
|
||||||
|
|
||||||
if len(resp.Cookies()) == 0 {
|
if len(resp.Cookies()) == 0 {
|
||||||
|
Loading…
Reference in New Issue
Block a user