fixes + avoid rate limit

This commit is contained in:
Gabe Farrell 2025-03-04 09:34:00 -05:00
parent 6bc7923d16
commit 78c2173197

31
main.go
View file

@ -8,6 +8,7 @@ import (
"reflect" "reflect"
"strconv" "strconv"
"strings" "strings"
"time"
"github.com/darenliang/jikan-go" "github.com/darenliang/jikan-go"
) )
@ -34,12 +35,17 @@ func main() {
func handleAnimeSearch(malToTvdb map[int]int) func(w http.ResponseWriter, r *http.Request) { func handleAnimeSearch(malToTvdb map[int]int) func(w http.ResponseWriter, r *http.Request) {
return func(w http.ResponseWriter, r *http.Request) { return func(w http.ResponseWriter, r *http.Request) {
w.WriteHeader(200) log.Printf("%s %s?%s", r.Method, r.URL.Path, r.URL.RawQuery)
w.Write([]byte(getAnimeSearch(malToTvdb, r))) 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() q := r.URL.Query()
hasNextPage := true hasNextPage := true
@ -50,7 +56,8 @@ func getAnimeSearch(malToTvdb map[int]int, r *http.Request) string {
q.Set("page", strconv.Itoa(page)) q.Set("page", strconv.Itoa(page))
result, err := jikan.GetAnimeSearch(q) result, err := jikan.GetAnimeSearch(q)
if err != nil { if err != nil {
log.Fatal(err) log.Println("Error sending request to Jikan: ", err)
return "", err
} }
// map the data // map the data
@ -63,13 +70,17 @@ func getAnimeSearch(malToTvdb map[int]int, r *http.Request) string {
}) })
} }
hasNextPage = result.Pagination.HasNextPage 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, "", " ") respJson, err := json.MarshalIndent(resp, "", " ")
if err != nil { 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 { func buildIdMap() map[int]int {
@ -77,17 +88,17 @@ func buildIdMap() map[int]int {
var idListBytes []byte var idListBytes []byte
resp, err := http.Get("https://raw.githubusercontent.com/Kometa-Team/Anime-IDs/master/anime_ids.json") resp, err := http.Get("https://raw.githubusercontent.com/Kometa-Team/Anime-IDs/master/anime_ids.json")
if err != nil { if err != nil {
log.Fatal(err) log.Fatal("Error fetching anime_ids.json: ", err)
} }
idListBytes, err = io.ReadAll(resp.Body) idListBytes, err = io.ReadAll(resp.Body)
if err != nil { if err != nil {
log.Fatal(err) log.Fatal("Error reading anime_ids.json: ", err)
} }
var animeMap map[string]AnimeEntry var animeMap map[string]AnimeEntry
err = json.Unmarshal(idListBytes, &animeMap) err = json.Unmarshal(idListBytes, &animeMap)
if err != nil { if err != nil {
log.Fatal(err) log.Fatal("Error unmarshalling anime_ids.json: ", err)
} }
malToTvdb := make(map[int]int, 0) malToTvdb := make(map[int]int, 0)
for _, entry := range animeMap { for _, entry := range animeMap {
@ -101,7 +112,7 @@ func buildIdMap() map[int]int {
for _, ss := range s { for _, ss := range s {
id, err := strconv.Atoi(ss) id, err := strconv.Atoi(ss)
if err != nil { if err != nil {
log.Fatal(err) log.Fatal("Error building anime id associations: ", err)
} }
malIdList = append(malIdList, id) malIdList = append(malIdList, id)
} }