Commit ec51a8678a

ec51a8678aee27cf0b449a3bae75aab1df4f292c

parent: f7879bc013

Unsigned

Wraient <wraient.911@gmail.com> · 2024-10-19 19:24 UTC

add: parse anime

Layout: unified · split

internal/anilist.go +102 −4
@@ -12,9 +12,9 @@ import (
12 12
13// AnimeTitle represents the title in different languages 13// AnimeTitle represents the title in different languages
14type AnimeTitle struct { 14type AnimeTitle struct {
15 Romanji_title string 15 Romaji string
16 English_title string 16 English string
17 Japanese_title string 17 Japanese string
18} 18}
19 19
20// AniListAnime is the struct for the API response 20// AniListAnime is the struct for the API response
@@ -37,6 +37,29 @@ type ResponseData struct {
37 Page Page `json:"Page"` 37 Page Page `json:"Page"`
38} 38}
39 39
40
41type Media struct {
42 Duration int `json:"duration"`
43 Episodes int `json:"episodes"`
44 ID int `json:"id"`
45 Title AnimeTitle `json:"title"`
46}
47
48type Entry struct {
49 Media Media `json:"media"`
50 Progress int `json:"progress"`
51 Score float64 `json:"score"`
52 Status string `json:"status"`
53}
54
55type AnimeList struct {
56 Watching []Entry `json:"watching"`
57 Completed []Entry `json:"completed"`
58 Paused []Entry `json:"paused"`
59 Dropped []Entry `json:"dropped"`
60 Planning []Entry `json:"planning"`
61}
62
40// SearchAnimeAnilist sends the query to AniList and returns a map of title to ID 63// SearchAnimeAnilist sends the query to AniList and returns a map of title to ID
41func SearchAnimeAnilist(query, token string) (map[string]string, error) { 64func SearchAnimeAnilist(query, token string) (map[string]string, error) {
42 url := "https://graphql.anilist.co" 65 url := "https://graphql.anilist.co"
@@ -235,6 +258,7 @@ func GetUserData(token string, userID int) (map[string]interface{}, error) {
235 title { 258 title {
236 romaji 259 romaji
237 english 260 english
261 native
238 } 262 }
239 } 263 }
240 status 264 status
@@ -413,4 +437,78 @@ func makePostRequest(url, query string, variables map[string]interface{}, header
413 } 437 }
414 438
415 return responseData, nil 439 return responseData, nil
416}\ No newline at end of file 440}
441
442func ParseAnimeList(input map[string]interface{}) AnimeList {
443 var animeList AnimeList
444
445 toInt := func(value interface{}) int {
446 switch v := value.(type) {
447 case int:
448 return v
449 case float64:
450 return int(v) // You could also use int(math.Round(v)) to round
451 default:
452 return 0 // Default value for unexpected types
453 }
454 }
455
456 safeString := func(value interface{}) string {
457 if value == nil {
458 return ""
459 }
460
461 // Attempt to assert the value as a string
462 if str, ok := value.(string); ok {
463 return str
464 }
465
466 // If it's not a string, return an empty string or handle it as needed
467 return ""
468 }
469
470 // Access the list entries in the input map
471 data := input["data"].(map[string]interface{})
472 mediaList := data["MediaListCollection"].(map[string]interface{})["lists"].([]interface{})
473
474 for _, list := range mediaList {
475 entries := list.(map[string]interface{})["entries"].([]interface{})
476
477 for _, entry := range entries {
478 entryData := entry.(map[string]interface{})
479 media := entryData["media"].(map[string]interface{})
480
481 animeEntry := Entry{
482 Media: Media{
483 Duration: toInt(media["duration"]),
484 Episodes: toInt(media["episodes"]),
485 ID: toInt(media["id"]),
486 Title: AnimeTitle{
487 English: safeString(media["title"].(map[string]interface{})["english"]),
488 Romaji: safeString(media["title"].(map[string]interface{})["romaji"]),
489 Japanese: safeString(media["title"].(map[string]interface{})["native"]),
490 },
491 },
492 Progress: toInt(entryData["progress"]),
493 Score: entryData["score"].(float64),
494 Status: safeString(entryData["status"]), // Ensure status is fetched safely
495 }
496
497 // Append entries based on their status
498 switch animeEntry.Status {
499 case "CURRENT":
500 animeList.Watching = append(animeList.Watching, animeEntry)
501 case "COMPLETED":
502 animeList.Completed = append(animeList.Completed, animeEntry) // Fix: Ensure Completed list is used
503 case "PAUSED":
504 animeList.Paused = append(animeList.Paused, animeEntry) // Fix: Append to Paused list
505 case "DROPPED":
506 animeList.Dropped = append(animeList.Dropped, animeEntry) // Fix: Append to Dropped list
507 case "PLANNING":
508 animeList.Planning = append(animeList.Planning, animeEntry) // Fix: Append to Planning list
509 }
510 }
511 }
512
513 return animeList
514}