From 78c2173197c8c3d4353c599f635575ab09b95959 Mon Sep 17 00:00:00 2001 From: Gabe Farrell Date: Tue, 4 Mar 2025 09:34:00 -0500 Subject: [PATCH] fixes + avoid rate limit --- main.go | 31 +++++++++++++++++++++---------- 1 file changed, 21 insertions(+), 10 deletions(-) diff --git a/main.go b/main.go index 62df333..386a29f 100644 --- a/main.go +++ b/main.go @@ -8,6 +8,7 @@ import ( "reflect" "strconv" "strings" + "time" "github.com/darenliang/jikan-go" ) @@ -34,12 +35,17 @@ func main() { func handleAnimeSearch(malToTvdb map[int]int) func(w http.ResponseWriter, r *http.Request) { return func(w http.ResponseWriter, r *http.Request) { - w.WriteHeader(200) - w.Write([]byte(getAnimeSearch(malToTvdb, r))) + log.Printf("%s %s?%s", r.Method, r.URL.Path, r.URL.RawQuery) + search, err := getAnimeSearch(malToTvdb, r) + if err != nil { + w.WriteHeader(500) + } else { + w.Write([]byte(search)) + } } } -func getAnimeSearch(malToTvdb map[int]int, r *http.Request) string { +func getAnimeSearch(malToTvdb map[int]int, r *http.Request) (string, error) { q := r.URL.Query() hasNextPage := true @@ -50,7 +56,8 @@ func getAnimeSearch(malToTvdb map[int]int, r *http.Request) string { q.Set("page", strconv.Itoa(page)) result, err := jikan.GetAnimeSearch(q) if err != nil { - log.Fatal(err) + log.Println("Error sending request to Jikan: ", err) + return "", err } // map the data @@ -63,13 +70,17 @@ func getAnimeSearch(malToTvdb map[int]int, r *http.Request) string { }) } hasNextPage = result.Pagination.HasNextPage + if hasNextPage { + time.Sleep(1 * time.Second) // sleep between requests for new page to try and avoid rate limits + } } respJson, err := json.MarshalIndent(resp, "", " ") if err != nil { - log.Fatal(err) + log.Println("Error marshalling response: ", err) + return "", err } - return string(respJson) + return string(respJson), nil } func buildIdMap() map[int]int { @@ -77,17 +88,17 @@ func buildIdMap() map[int]int { var idListBytes []byte resp, err := http.Get("https://raw.githubusercontent.com/Kometa-Team/Anime-IDs/master/anime_ids.json") if err != nil { - log.Fatal(err) + log.Fatal("Error fetching anime_ids.json: ", err) } idListBytes, err = io.ReadAll(resp.Body) if err != nil { - log.Fatal(err) + log.Fatal("Error reading anime_ids.json: ", err) } var animeMap map[string]AnimeEntry err = json.Unmarshal(idListBytes, &animeMap) if err != nil { - log.Fatal(err) + log.Fatal("Error unmarshalling anime_ids.json: ", err) } malToTvdb := make(map[int]int, 0) for _, entry := range animeMap { @@ -101,7 +112,7 @@ func buildIdMap() map[int]int { for _, ss := range s { id, err := strconv.Atoi(ss) if err != nil { - log.Fatal(err) + log.Fatal("Error building anime id associations: ", err) } malIdList = append(malIdList, id) }