Commit ed7d66de7a
Unsigned
Layout: unified · split
internal/anilist.go +64 −6
| @@ -38,7 +38,6 @@ type AnimeTitle struct { | |||
| 38 | Japanese string | 38 | Japanese string |
| 39 | } | 39 | } |
| 40 | 40 | ||
| 41 | |||
| 42 | type Media struct { | 41 | type Media struct { |
| 43 | Duration int `json:"duration"` | 42 | Duration int `json:"duration"` |
| 44 | Episodes int `json:"episodes"` | 43 | Episodes int `json:"episodes"` |
| @@ -54,11 +53,42 @@ type Entry struct { | |||
| 54 | } | 53 | } |
| 55 | 54 | ||
| 56 | type AnimeList struct { | 55 | type AnimeList struct { |
| 57 | Watching []Entry `json:"watching"` | 56 | Watching []Entry `json:"watching"` |
| 58 | Completed []Entry `json:"completed"` | 57 | Completed []Entry `json:"completed"` |
| 59 | Paused []Entry `json:"paused"` | 58 | Paused []Entry `json:"paused"` |
| 60 | Dropped []Entry `json:"dropped"` | 59 | Dropped []Entry `json:"dropped"` |
| 61 | Planning []Entry `json:"planning"` | 60 | Planning []Entry `json:"planning"` |
| 61 | } | ||
| 62 | |||
| 63 | // FindKeyByValue searches for a key associated with a given value in a map[string]string | ||
| 64 | func FindKeyByValue(m map[string]string, value string) (string, error) { | ||
| 65 | for key, val := range m { | ||
| 66 | if val == value { | ||
| 67 | return key, nil // Return the key and true if the value is found | ||
| 68 | } | ||
| 69 | } | ||
| 70 | return "", fmt.Errorf("No key with value %v", value) // Return empty string and false if the value is not found | ||
| 71 | } | ||
| 72 | |||
| 73 | // GetAnimeMap takes an AnimeList and returns a map with media.id as key and media.title.english as value. | ||
| 74 | func GetAnimeMap(animeList AnimeList) map[string]string { | ||
| 75 | animeMap := make(map[string]string) | ||
| 76 | |||
| 77 | // Helper function to populate the map from a slice of entries | ||
| 78 | populateMap := func(entries []Entry) { | ||
| 79 | for _, entry := range entries { | ||
| 80 | animeMap[strconv.Itoa(entry.Media.ID)] = entry.Media.Title.English | ||
| 81 | } | ||
| 82 | } | ||
| 83 | |||
| 84 | // Populate the map for each category | ||
| 85 | populateMap(animeList.Watching) | ||
| 86 | populateMap(animeList.Completed) | ||
| 87 | populateMap(animeList.Paused) | ||
| 88 | populateMap(animeList.Dropped) | ||
| 89 | populateMap(animeList.Planning) | ||
| 90 | |||
| 91 | return animeMap | ||
| 62 | } | 92 | } |
| 63 | 93 | ||
| 64 | // SearchAnimeAnilist sends the query to AniList and returns a map of title to ID | 94 | // SearchAnimeAnilist sends the query to AniList and returns a map of title to ID |
| @@ -513,3 +543,31 @@ func ParseAnimeList(input map[string]interface{}) AnimeList { | |||
| 513 | 543 | ||
| 514 | return animeList | 544 | return animeList |
| 515 | } | 545 | } |
| 546 | |||
| 547 | // FindAnimeByID searches for an anime by its ID in the AnimeList | ||
| 548 | func FindAnimeByAnilistID(list AnimeList, idStr string) (*Entry, error) { | ||
| 549 | id, err := strconv.Atoi(idStr) | ||
| 550 | if err != nil { | ||
| 551 | return nil, fmt.Errorf("invalid ID format: %s", idStr) | ||
| 552 | } | ||
| 553 | |||
| 554 | // Define a slice of pointers to hold categories | ||
| 555 | categories := [][]Entry{ | ||
| 556 | list.Watching, | ||
| 557 | list.Completed, | ||
| 558 | list.Paused, | ||
| 559 | list.Dropped, | ||
| 560 | list.Planning, | ||
| 561 | } | ||
| 562 | |||
| 563 | // Iterate through each category | ||
| 564 | for _, category := range categories { | ||
| 565 | for _, entry := range category { | ||
| 566 | if entry.Media.ID == id { | ||
| 567 | return &entry, nil // Return a pointer to the found entry | ||
| 568 | } | ||
| 569 | } | ||
| 570 | } | ||
| 571 | |||
| 572 | return nil, fmt.Errorf("anime with ID %d not found", id) // Return an error if not found | ||
| 573 | } | ||