Compare commits
3 Commits
27ce4d3c78
...
master
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
11ea7b8398 | ||
|
|
7d3c083cea | ||
|
|
dbfbd6865a |
@@ -1,7 +1,7 @@
|
||||
## tg-holy-bot
|
||||
|
||||
Телеграммный бот для канала с праздниками. Раз в сутки забирает список праздников с к сайта celebratoday.com и
|
||||
показывает его на канальчике в телеграме.
|
||||
показывает его на канальчике в телеграме. Создан исключительно в обучающих целях, как пример приложения на языке go.
|
||||
|
||||
## Как это собрать?
|
||||
|
||||
|
||||
@@ -24,12 +24,14 @@ func APIClient() (string, error) {
|
||||
|
||||
// Формальная попытка притвориться браузером.
|
||||
_, _, day := time.Now().Date()
|
||||
userAgent := Config.UserAgents[day]
|
||||
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.
|
||||
}
|
||||
|
||||
@@ -5,6 +5,8 @@ import (
|
||||
"time"
|
||||
)
|
||||
|
||||
const maxRetryAttempts = 15
|
||||
|
||||
// CronTask sends generated holiday list to the channel.
|
||||
func CronTask() {
|
||||
var (
|
||||
@@ -22,10 +24,10 @@ 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 0, 1, 2:
|
||||
case 1, 2:
|
||||
timeout = 1 * time.Second
|
||||
|
||||
case 3, 4, 5:
|
||||
|
||||
@@ -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
|
||||
|
||||
@@ -24,8 +24,10 @@ func SigHandler() {
|
||||
}
|
||||
|
||||
Shutdown = true
|
||||
|
||||
close(Run)
|
||||
|
||||
// Ensure we exit cleanly after handling the signal
|
||||
return
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user