| @@ -5,7 +5,6 @@ import ( |
| 5 | 5 | "encoding/json" |
| 6 | 6 | "fmt" |
| 7 | 7 | "io" |
| 8 | | "io/ioutil" |
| 9 | 8 | "net/http" |
| 10 | 9 | "os" |
| 11 | 10 | "path/filepath" |
| @@ -33,7 +32,7 @@ func GetAnimeMap(animeList AnimeList) map[string]string { |
| 33 | 32 | for _, entry := range entries { |
| 34 | 33 | // Only include entries with a non-empty English title |
| 35 | 34 | |
| 36 | | if entry.Media.Title.English != "" && userCurdConfig.AnimeNameLanguage == "english" { |
| 35 | if entry.Media.Title.English != "" && userCurdConfig.AnimeNameLanguage == "english" { |
| 37 | 36 | animeMap[strconv.Itoa(entry.Media.ID)] = entry.Media.Title.English |
| 38 | 37 | } else { |
| 39 | 38 | animeMap[strconv.Itoa(entry.Media.ID)] = entry.Media.Title.Romaji |
| @@ -63,12 +62,12 @@ func GetAnimeMapPreview(animeList AnimeList) map[string]RofiSelectPreview { |
| 63 | 62 | Log(fmt.Sprintf("AnimeNameLanguage: ", userCurdConfig.AnimeNameLanguage), logFile) |
| 64 | 63 | if entry.Media.Title.English != "" && userCurdConfig.AnimeNameLanguage == "english" { |
| 65 | 64 | animeMap[strconv.Itoa(entry.Media.ID)] = RofiSelectPreview{ |
| 66 | | Title: entry.Media.Title.English, |
| 65 | Title: entry.Media.Title.English, |
| 67 | 66 | CoverImage: entry.CoverImage, |
| 68 | 67 | } |
| 69 | 68 | } else { |
| 70 | 69 | animeMap[strconv.Itoa(entry.Media.ID)] = RofiSelectPreview{ |
| 71 | | Title: entry.Media.Title.Romaji, |
| 70 | Title: entry.Media.Title.Romaji, |
| 72 | 71 | CoverImage: entry.CoverImage, |
| 73 | 72 | } |
| 74 | 73 | } |
| @@ -459,9 +458,9 @@ func SearchAnimeByTitle(jsonData map[string]interface{}, searchTitle string) []m |
| 459 | 458 | |
| 460 | 459 | if strings.Contains(strings.ToLower(romajiTitle), strings.ToLower(searchTitle)) || strings.Contains(strings.ToLower(englishTitle), strings.ToLower(searchTitle)) { |
| 461 | 460 | result := map[string]interface{}{ |
| 462 | | "id": media["id"], |
| 463 | | "progress": entry.(map[string]interface{})["progress"], |
| 464 | | "romaji_title": romajiTitle, |
| 461 | "id": media["id"], |
| 462 | "progress": entry.(map[string]interface{})["progress"], |
| 463 | "romaji_title": romajiTitle, |
| 465 | 464 | "english_title": englishTitle, |
| 466 | 465 | "episodes": episodes, |
| 467 | 466 | "duration": duration, |
| @@ -609,7 +608,7 @@ func makePostRequest(url, query string, variables map[string]interface{}, header |
| 609 | 608 | return nil, fmt.Errorf("failed to create request: %w", err) |
| 610 | 609 | } |
| 611 | 610 | |
| 612 | | req.Header.Set("Content-Type", "application/json") // <-- Important! |
| 611 | req.Header.Set("Content-Type", "application/json") // <-- Important! |
| 613 | 612 | for key, value := range headers { |
| 614 | 613 | req.Header.Set(key, value) |
| 615 | 614 | } |
| @@ -691,9 +690,9 @@ func ParseAnimeList(input map[string]interface{}) AnimeList { |
| 691 | 690 | Episodes: toInt(media["episodes"]), |
| 692 | 691 | ID: toInt(media["id"]), |
| 693 | 692 | Title: AnimeTitle{ |
| 694 | | English: safeString(media["title"].(map[string]interface{})["english"]), |
| 695 | | Romaji: safeString(media["title"].(map[string]interface{})["romaji"]), |
| 696 | | Japanese: safeString(media["title"].(map[string]interface{})["native"]), |
| 693 | English: safeString(media["title"].(map[string]interface{})["english"]), |
| 694 | Romaji: safeString(media["title"].(map[string]interface{})["romaji"]), |
| 695 | Japanese: safeString(media["title"].(map[string]interface{})["native"]), |
| 697 | 696 | }, |
| 698 | 697 | }, |
| 699 | 698 | Progress: toInt(entryData["progress"]), |
| @@ -763,78 +762,62 @@ func FindAnimeByAnilistIDInAnimes(animes []Anime, anilistID int) (*Anime, error) |
| 763 | 762 | } |
| 764 | 763 | |
| 765 | 764 | // GetAnimeDataByID retrieves detailed anime data from AniList using the anime's ID and user token |
| 766 | | func GetAnimeDataByID(anilistID int, token string) (Anime, error) { |
| 765 | func GetAnimeDataByID(id int, token string) (Anime, error) { |
| 766 | url := "https://graphql.anilist.co" |
| 767 | 767 | query := ` |
| 768 | 768 | query ($id: Int) { |
| 769 | | Media (id: $id, type: ANIME) { |
| 769 | Media(id: $id, type: ANIME) { |
| 770 | 770 | id |
| 771 | | title { |
| 772 | | romaji |
| 773 | | english |
| 774 | | native |
| 775 | | } |
| 776 | 771 | episodes |
| 777 | | duration |
| 778 | 772 | status |
| 773 | nextAiringEpisode { |
| 774 | episode |
| 775 | } |
| 779 | 776 | } |
| 780 | | } |
| 781 | | ` |
| 782 | | |
| 777 | }` |
| 778 | |
| 783 | 779 | variables := map[string]interface{}{ |
| 784 | | "id": anilistID, |
| 780 | "id": id, |
| 785 | 781 | } |
| 786 | 782 | |
| 787 | | jsonValue, _ := json.Marshal(map[string]interface{}{ |
| 788 | | "query": query, |
| 789 | | "variables": variables, |
| 790 | | }) |
| 783 | headers := map[string]string{ |
| 784 | "Authorization": "Bearer " + token, |
| 785 | "Content-Type": "application/json", |
| 786 | } |
| 791 | 787 | |
| 792 | | req, err := http.NewRequest("POST", "https://graphql.anilist.co", bytes.NewBuffer(jsonValue)) |
| 788 | response, err := makePostRequest(url, query, variables, headers) |
| 793 | 789 | if err != nil { |
| 794 | | return Anime{}, fmt.Errorf("error creating request: %v", err) |
| 790 | return Anime{}, fmt.Errorf("failed to get anime data: %w", err) |
| 795 | 791 | } |
| 796 | 792 | |
| 797 | | req.Header.Set("Content-Type", "application/json") |
| 798 | | req.Header.Set("Authorization", "Bearer "+token) |
| 793 | data, ok := response["data"].(map[string]interface{}) |
| 794 | if !ok { |
| 795 | return Anime{}, fmt.Errorf("invalid response format: data field missing") |
| 796 | } |
| 799 | 797 | |
| 800 | | client := &http.Client{} |
| 801 | | resp, err := client.Do(req) |
| 802 | | if err != nil { |
| 803 | | return Anime{}, fmt.Errorf("error sending request: %v", err) |
| 798 | media, ok := data["Media"].(map[string]interface{}) |
| 799 | if !ok { |
| 800 | return Anime{}, fmt.Errorf("invalid response format: Media field missing") |
| 804 | 801 | } |
| 805 | | defer resp.Body.Close() |
| 806 | 802 | |
| 807 | | body, err := ioutil.ReadAll(resp.Body) |
| 808 | | if err != nil { |
| 809 | | return Anime{}, fmt.Errorf("error reading response: %v", err) |
| 803 | anime := Anime{ |
| 804 | AnilistId: id, |
| 805 | IsAiring: false, |
| 810 | 806 | } |
| 811 | 807 | |
| 812 | | var result struct { |
| 813 | | Data struct { |
| 814 | | Media struct { |
| 815 | | ID int `json:"id"` |
| 816 | | Title AnimeTitle `json:"title"` |
| 817 | | Episodes int `json:"episodes"` |
| 818 | | Duration int `json:"duration"` |
| 819 | | Status string `json:"status"` |
| 820 | | CoverImage struct { |
| 821 | | Large string `json:"large"` |
| 822 | | } `json:"coverImage"` |
| 823 | | Genres []string `json:"genres"` |
| 824 | | AverageScore int `json:"averageScore"` |
| 825 | | } `json:"Media"` |
| 826 | | } `json:"data"` |
| 808 | // Safely handle episodes field which might be nil for currently airing shows |
| 809 | if episodes, ok := media["episodes"].(float64); ok { |
| 810 | anime.TotalEpisodes = int(episodes) |
| 827 | 811 | } |
| 828 | 812 | |
| 829 | | if err := json.Unmarshal(body, &result); err != nil { |
| 830 | | return Anime{}, fmt.Errorf("error unmarshaling JSON: %v", err) |
| 813 | // Check status |
| 814 | if status, ok := media["status"].(string); ok { |
| 815 | anime.IsAiring = status == "RELEASING" |
| 831 | 816 | } |
| 832 | 817 | |
| 833 | | anime := Anime{ |
| 834 | | AnilistId: result.Data.Media.ID, |
| 835 | | Title: result.Data.Media.Title, |
| 836 | | TotalEpisodes: result.Data.Media.Episodes, |
| 837 | | CoverImage: result.Data.Media.CoverImage.Large, |
| 818 | // Double check with nextAiringEpisode |
| 819 | if nextEp, ok := media["nextAiringEpisode"].(map[string]interface{}); ok && nextEp != nil { |
| 820 | anime.IsAiring = true |
| 838 | 821 | } |
| 839 | 822 | |
| 840 | 823 | return anime, nil |