diff options
author | Thomas Voss <mail@thomasvoss.com> | 2025-08-02 18:29:48 +0200 |
---|---|---|
committer | Thomas Voss <mail@thomasvoss.com> | 2025-08-02 18:29:48 +0200 |
commit | 032d880706f4a2b3b66ab652d71455e74b5dbef6 (patch) | |
tree | ce14a00d13921b665824a5c553349471939f33c2 /src | |
parent | 48ef38f18b034dc59d62cc9c6240196536c12f66 (diff) |
Handle rate-limiting
Diffstat (limited to 'src')
-rw-r--r-- | src/wikipedia/wikipedia.go | 12 |
1 files changed, 12 insertions, 0 deletions
diff --git a/src/wikipedia/wikipedia.go b/src/wikipedia/wikipedia.go index ebcdacb..9faad57 100644 --- a/src/wikipedia/wikipedia.go +++ b/src/wikipedia/wikipedia.go @@ -7,7 +7,9 @@ import ( "log" "net/http" "net/url" + "strconv" "strings" + "time" ) var ( @@ -47,8 +49,18 @@ func Init(locale string) { log.Println(err) return } + if respjson.StatusCode >= 400 && + respjson.StatusCode != http.StatusTooManyRequests { + log.Printf("Failed to GET %s: %s\n", u, respjson.Status) + return + } defer respjson.Body.Close() + secs, err := strconv.Atoi(respjson.Header.Get("Retry-After")) + if err != nil { + time.Sleep(time.Duration(secs) * time.Second) + } + body, err := io.ReadAll(respjson.Body) if err != nil { log.Println(err) |