From 032d880706f4a2b3b66ab652d71455e74b5dbef6 Mon Sep 17 00:00:00 2001 From: Thomas Voss Date: Sat, 2 Aug 2025 18:29:48 +0200 Subject: Handle rate-limiting --- src/wikipedia/wikipedia.go | 12 ++++++++++++ 1 file changed, 12 insertions(+) (limited to 'src') 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) -- cgit v1.2.3