diff --git a/README.md b/README.md index bc16729..3e345be 100644 --- a/README.md +++ b/README.md @@ -1,7 +1,7 @@ ## tg-holy-bot Телеграммный бот для канала с праздниками. Раз в сутки забирает список праздников с к сайта celebratoday.com и -показывает его на канальчике в телеграме. +показывает его на канальчике в телеграме. Создан исключительно в обучающих целях, как пример приложения на языке go. ## Как это собрать? diff --git a/internal/holy/celebratoday.go b/internal/holy/celebratoday.go index 8351ffb..e917245 100644 --- a/internal/holy/celebratoday.go +++ b/internal/holy/celebratoday.go @@ -26,10 +26,12 @@ func APIClient() (string, error) { _, _, day := time.Now().Date() userAgent := Config.UserAgents[day-1] + ctx, cancel := context.WithTimeout(ctx, 10*time.Second) + defer cancel() + if err := requests.URL(url).UserAgent(userAgent). Header("Accept", "text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8"). - Header("Accept-Language", "en-US,en;q=0.5"). - Header("User-Agent", "ru-RU"). + Header("Accept-Language", "ru-RU,ru;q=0.9,en-US;q=0.8,en;q=0.7"). Header("Accept-Charset", "utf-8").ToString(&respBody).Fetch(ctx); err != nil { return "", fmt.Errorf("Unable to GET %s: %w", url, err) //nolint: staticcheck // Looks like golangci-lint ignoring settings about fucking ST1005 check. We want readable errors despite what wants author of linter or assholes from go dev team. } diff --git a/internal/holy/cronTask.go b/internal/holy/cronTask.go index 5d7dafd..e24abdf 100644 --- a/internal/holy/cronTask.go +++ b/internal/holy/cronTask.go @@ -5,6 +5,8 @@ import ( "time" ) +const maxRetryAttempts = 15 + // CronTask sends generated holiday list to the channel. func CronTask() { var ( @@ -22,7 +24,7 @@ func CronTask() { if err != nil { errcnt++ - log.Errorf("Unable to get holidays (try %d/15): %s", errcnt, err) + log.Errorf("Unable to get holidays (try %d/%d): %s", errcnt, maxRetryAttempts, err) switch errcnt { case 1, 2: diff --git a/internal/holy/readConfig.go b/internal/holy/readConfig.go index a990feb..98aa539 100644 --- a/internal/holy/readConfig.go +++ b/internal/holy/readConfig.go @@ -3,6 +3,7 @@ package holy import ( "encoding/json" "errors" + "fmt" "os" "tg-holy-bot/internal/log" @@ -103,6 +104,13 @@ func ReadConfig() error { return errors.New("Amount of user_agent strings in config must be exactly 31! Refusing to start") //nolint: staticcheck // Looks like golangci-lint ignoring settings about fucking ST1005 check. We want readable errors despite what wants author of linter or assholes from go dev team. } + // Validate that all user agent strings are non-empty + for i, userAgent := range sampleConfig.UserAgents { + if userAgent == "" { + return fmt.Errorf("User agent string at index %d is empty! Refusing to start", i) //nolint: staticcheck // Looks like golangci-lint ignoring settings about fucking ST1005 check. We want readable errors despite what wants author of linter or assholes from go dev team. + } + } + log.Infof("Using %s as config file", location) Config = sampleConfig diff --git a/internal/holy/sigHandler.go b/internal/holy/sigHandler.go index 124ab67..1019af3 100644 --- a/internal/holy/sigHandler.go +++ b/internal/holy/sigHandler.go @@ -24,8 +24,10 @@ func SigHandler() { } Shutdown = true - close(Run) + + // Ensure we exit cleanly after handling the signal + return } }