Commit 864a792746

864a792746b157ef8b32f60d8ee699a9a994a875

parent: ef9a1dde4e

Unregistered key

Rushikesh Gaikwad <81632222+Wraient@users.noreply.github.com> · 2026-01-01 22:22 UTC
committer: <noreply@github.com>

Add: Auto prompt to add sequel to list (#77)

Layout: unified · split

internal/anilist.go +201
@@ -910,3 +910,204 @@ func GetAnimeDataByID(id int, token string) (Anime, error) {
910910
911911 return anime, nil
912912}
913
914// SequelInfo holds information about a sequel anime
915type SequelInfo struct {
916 ID int
917 Title AnimeTitle
918 CoverImage string
919 Episodes int
920 Status string // "FINISHED", "RELEASING", "NOT_YET_RELEASED"
921}
922
923// GetAnimeSequel fetches sequel information for a given anime from AniList
924func GetAnimeSequel(animeID int, token string) (*SequelInfo, error) {
925 url := "https://graphql.anilist.co"
926 query := `
927 query ($id: Int) {
928 Media(id: $id, type: ANIME) {
929 relations {
930 edges {
931 relationType
932 node {
933 id
934 title {
935 romaji
936 english
937 }
938 coverImage {
939 large
940 }
941 episodes
942 status
943 }
944 }
945 }
946 }
947 }`
948
949 variables := map[string]interface{}{
950 "id": animeID,
951 }
952
953 headers := map[string]string{
954 "Authorization": "Bearer " + token,
955 "Content-Type": "application/json",
956 }
957
958 response, err := makePostRequest(url, query, variables, headers)
959 if err != nil {
960 return nil, fmt.Errorf("failed to get anime relations: %w", err)
961 }
962
963 data, ok := response["data"].(map[string]interface{})
964 if !ok {
965 return nil, fmt.Errorf("invalid response format: data field missing")
966 }
967
968 media, ok := data["Media"].(map[string]interface{})
969 if !ok {
970 return nil, fmt.Errorf("invalid response format: Media field missing")
971 }
972
973 relations, ok := media["relations"].(map[string]interface{})
974 if !ok {
975 return nil, nil // No relations found
976 }
977
978 edges, ok := relations["edges"].([]interface{})
979 if !ok || len(edges) == 0 {
980 return nil, nil // No edges found
981 }
982
983 // Look for a SEQUEL relation
984 for _, edge := range edges {
985 edgeData, ok := edge.(map[string]interface{})
986 if !ok {
987 continue
988 }
989
990 relationType, ok := edgeData["relationType"].(string)
991 if !ok || relationType != "SEQUEL" {
992 continue
993 }
994
995 node, ok := edgeData["node"].(map[string]interface{})
996 if !ok {
997 continue
998 }
999
1000 sequel := &SequelInfo{}
1001
1002 // Parse ID
1003 if id, ok := node["id"].(float64); ok {
1004 sequel.ID = int(id)
1005 }
1006
1007 // Parse title
1008 if title, ok := node["title"].(map[string]interface{}); ok {
1009 if romaji, ok := title["romaji"].(string); ok {
1010 sequel.Title.Romaji = romaji
1011 }
1012 if english, ok := title["english"].(string); ok {
1013 sequel.Title.English = english
1014 }
1015 }
1016
1017 // Parse cover image
1018 if coverImage, ok := node["coverImage"].(map[string]interface{}); ok {
1019 if large, ok := coverImage["large"].(string); ok {
1020 sequel.CoverImage = large
1021 }
1022 }
1023
1024 // Parse episodes
1025 if episodes, ok := node["episodes"].(float64); ok {
1026 sequel.Episodes = int(episodes)
1027 }
1028
1029 // Parse status
1030 if status, ok := node["status"].(string); ok {
1031 sequel.Status = status
1032 }
1033
1034 return sequel, nil
1035 }
1036
1037 return nil, nil // No sequel found
1038}
1039
1040// AddAnimeToList adds an anime to a specified list (CURRENT, PLANNING, PAUSED, DROPPED)
1041func AddAnimeToList(animeID int, status string, token string) error {
1042 url := "https://graphql.anilist.co"
1043 mutation := `
1044 mutation ($mediaId: Int, $status: MediaListStatus) {
1045 SaveMediaListEntry (mediaId: $mediaId, status: $status) {
1046 id
1047 status
1048 }
1049 }`
1050
1051 variables := map[string]interface{}{
1052 "mediaId": animeID,
1053 "status": status,
1054 }
1055
1056 headers := map[string]string{
1057 "Authorization": "Bearer " + token,
1058 "Content-Type": "application/json",
1059 }
1060
1061 _, err := makePostRequest(url, mutation, variables, headers)
1062 if err != nil {
1063 return fmt.Errorf("failed to add anime to list: %w", err)
1064 }
1065
1066 statusMap := map[string]string{
1067 "CURRENT": "Currently Watching",
1068 "COMPLETED": "Completed",
1069 "PAUSED": "On Hold",
1070 "DROPPED": "Dropped",
1071 "PLANNING": "Plan to Watch",
1072 }
1073
1074 CurdOut(fmt.Sprintf("Anime added to: %s", statusMap[status]))
1075 return nil
1076}
1077
1078// FindSequelInAnimeList searches for a sequel in the user's anime list and returns its status
1079func FindSequelInAnimeList(list AnimeList, sequelID int) (string, bool) {
1080 // Check all categories
1081 for _, entry := range list.Watching {
1082 if entry.Media.ID == sequelID {
1083 return "CURRENT", true
1084 }
1085 }
1086 for _, entry := range list.Planning {
1087 if entry.Media.ID == sequelID {
1088 return "PLANNING", true
1089 }
1090 }
1091 for _, entry := range list.Completed {
1092 if entry.Media.ID == sequelID {
1093 return "COMPLETED", true
1094 }
1095 }
1096 for _, entry := range list.Paused {
1097 if entry.Media.ID == sequelID {
1098 return "PAUSED", true
1099 }
1100 }
1101 for _, entry := range list.Dropped {
1102 if entry.Media.ID == sequelID {
1103 return "DROPPED", true
1104 }
1105 }
1106 for _, entry := range list.Rewatching {
1107 if entry.Media.ID == sequelID {
1108 return "REWATCHING", true
1109 }
1110 }
1111
1112 return "", false
1113}
internal/curd.go +201 −10
@@ -1442,8 +1442,6 @@ func NextEpisodePromptContinuous(userCurdConfig *CurdConfig, databaseFile string
14421442 err = UpdateAnimeProgress(userToken, anime.AnilistId, anime.Ep.Number)
14431443 if err != nil {
14441444 Log("Error updating Anilist progress on quit: " + err.Error())
1445 } else {
1446 CurdOut(fmt.Sprintf("Episode marked as completed! Progress updated: %d", anime.Ep.Number))
14471445 }
14481446 }()
14491447 }
@@ -1474,8 +1472,6 @@ func NextEpisodePromptContinuous(userCurdConfig *CurdConfig, databaseFile string
14741472 err = UpdateAnimeProgress(userToken, anime.AnilistId, anime.Ep.Number)
14751473 if err != nil {
14761474 Log("Error updating Anilist progress: " + err.Error())
1477 } else {
1478 CurdOut(fmt.Sprintf("Episode completed! Progress updated: %d", anime.Ep.Number))
14791475 }
14801476 }()
14811477 }
@@ -1546,9 +1542,8 @@ func StartNextEpisode(anime *Anime, userCurdConfig *CurdConfig, databaseFile str
15461542 err := UpdateAnimeProgress(userToken, anime.AnilistId, prevEpisode)
15471543 if err != nil {
15481544 Log("Error updating Anilist progress: " + err.Error())
1549 } else {
1550 CurdOut(fmt.Sprintf("Anime progress updated! Latest watched episode: %d", prevEpisode))
15511545 }
1546 // Note: UpdateAnimeProgress already outputs a message on success
15521547 }()
15531548 }
15541549
@@ -1596,8 +1591,6 @@ func StartNextEpisode(anime *Anime, userCurdConfig *CurdConfig, databaseFile str
15961591 err = UpdateAnimeProgress(userToken, anime.AnilistId, prevEpisode)
15971592 if err != nil {
15981593 Log("Error updating Anilist progress: " + err.Error())
1599 } else {
1600 CurdOut(fmt.Sprintf("Anime progress updated! Latest watched episode: %d", prevEpisode))
16011594 }
16021595 }()
16031596 }
@@ -1640,11 +1633,209 @@ func HandleLastEpisodeCompletion(userCurdConfig *CurdConfig, anime *Anime, userT
16401633 err := UpdateAnimeStatus(userToken, anime.AnilistId, "COMPLETED")
16411634 if err != nil {
16421635 Log("Error updating anime status to completed: " + err.Error())
1643 } else {
1644 CurdOut("Anime status updated to completed!")
16451636 }
1637 // Note: UpdateAnimeStatus already outputs a message on success
16461638 }()
16471639 }
16481640 }
1641
1642 // Check for sequel after completion (only if this is the last episode)
1643 if anime.TotalEpisodes > 0 && anime.Ep.Number == anime.TotalEpisodes {
1644 handleSequelCheck(userCurdConfig, anime, userToken)
1645 }
1646}
1647
1648// handleSequelCheck checks for sequels and prompts the user accordingly
1649func handleSequelCheck(userCurdConfig *CurdConfig, anime *Anime, userToken string) {
1650 // Recover from any panics in this function to prevent crashes
1651 defer func() {
1652 if r := recover(); r != nil {
1653 Log(fmt.Sprintf("Recovered from panic in handleSequelCheck: %v", r))
1654 }
1655 }()
1656
1657 // Fetch sequel information
1658 sequel, err := GetAnimeSequel(anime.AnilistId, userToken)
1659 if err != nil {
1660 Log(fmt.Sprintf("Error fetching sequel information: %v", err))
1661 return
1662 }
1663
1664 if sequel == nil {
1665 Log("No sequel found for this anime")
1666 return
1667 }
1668
1669 // Get sequel title based on user's language preference
1670 sequelTitle := sequel.Title.Romaji
1671 if sequel.Title.English != "" && userCurdConfig.AnimeNameLanguage == "english" {
1672 sequelTitle = sequel.Title.English
1673 }
1674
1675 Log(fmt.Sprintf("Found sequel: %s (ID: %d)", sequelTitle, sequel.ID))
1676
1677 // Check if sequel is not yet released
1678 if sequel.Status == "NOT_YET_RELEASED" {
1679 CurdOut(fmt.Sprintf("A sequel '%s' is announced but not yet released.", sequelTitle))
1680 return
1681 }
1682
1683 // Fetch user's anime list to check if sequel is already there
1684 userId, _, err := GetAnilistUserID(userToken)
1685 if err != nil {
1686 Log(fmt.Sprintf("Error getting user ID: %v", err))
1687 return
1688 }
1689
1690 userData, err := GetUserData(userToken, userId)
1691 if err != nil {
1692 Log(fmt.Sprintf("Error getting user data: %v", err))
1693 return
1694 }
1695
1696 // Check if userData is valid before parsing
1697 if userData == nil || userData["data"] == nil {
1698 Log("User data is nil or malformed, skipping sequel list check")
1699 // Still show the sequel prompt, but assume it's not in any list
1700 promptSequelNotInList(sequel, sequelTitle, userToken, anime)
1701 return
1702 }
1703
1704 userAnimeList := ParseAnimeList(userData)
1705 sequelStatus, isInList := FindSequelInAnimeList(userAnimeList, sequel.ID)
1706
1707 if !isInList {
1708 // Sequel is not in any list - ask if user wants to add it
1709 CurdOut(fmt.Sprintf("A sequel is available: %s", sequelTitle))
1710
1711 options := []SelectionOption{
1712 {Key: "watching", Label: "Add to Watching list"},
1713 {Key: "planning", Label: "Add to Plan to Watch"},
1714 {Key: "skip", Label: "No thanks"},
1715 }
1716
1717 selectedOption, err := DynamicSelect(options)
1718 if err != nil {
1719 Log(fmt.Sprintf("Error in sequel prompt: %v", err))
1720 return
1721 }
1722
1723 switch selectedOption.Key {
1724 case "watching":
1725 err = AddAnimeToList(sequel.ID, "CURRENT", userToken)
1726 if err != nil {
1727 Log(fmt.Sprintf("Error adding sequel to watching list: %v", err))
1728 CurdOut("Failed to add sequel to watching list")
1729 } else {
1730 CurdOut(fmt.Sprintf("Added '%s' to your Watching list!", sequelTitle))
1731 }
1732 case "planning":
1733 err = AddAnimeToList(sequel.ID, "PLANNING", userToken)
1734 if err != nil {
1735 Log(fmt.Sprintf("Error adding sequel to planning list: %v", err))
1736 CurdOut("Failed to add sequel to Plan to Watch")
1737 } else {
1738 CurdOut(fmt.Sprintf("Added '%s' to your Plan to Watch list!", sequelTitle))
1739 }
1740 case "skip", "-1":
1741 Log("User declined to add sequel")
1742 }
1743 } else {
1744 // Sequel is already in a list
1745 switch sequelStatus {
1746 case "CURRENT":
1747 // Already in watching list
1748 CurdOut(fmt.Sprintf("The sequel '%s' is already in your Watching list!", sequelTitle))
1749
1750 case "PLANNING":
1751 // In planning list - ask if user wants to move to watching
1752 CurdOut(fmt.Sprintf("The sequel '%s' is in your Plan to Watch list. Move to Watching?", sequelTitle))
1753
1754 options := []SelectionOption{
1755 {Key: "yes", Label: "Yes, move to Watching list"},
1756 {Key: "no", Label: "No, keep in Plan to Watch"},
1757 }
1758
1759 selectedOption, err := DynamicSelect(options)
1760 if err != nil {
1761 Log(fmt.Sprintf("Error in sequel planning prompt: %v", err))
1762 return
1763 }
1764
1765 if selectedOption.Key == "yes" {
1766 err = AddAnimeToList(sequel.ID, "CURRENT", userToken)
1767 if err != nil {
1768 Log(fmt.Sprintf("Error moving sequel to watching list: %v", err))
1769 CurdOut("Failed to move sequel to Watching list")
1770 } else {
1771 CurdOut(fmt.Sprintf("Moved '%s' to Watching list!", sequelTitle))
1772 }
1773 }
1774
1775 case "COMPLETED":
1776 CurdOut(fmt.Sprintf("You've already completed the sequel '%s'!", sequelTitle))
1777
1778 case "PAUSED", "DROPPED":
1779 CurdOut(fmt.Sprintf("The sequel '%s' is in your %s list.", sequelTitle, sequelStatus))
1780
1781 options := []SelectionOption{
1782 {Key: "yes", Label: "Move to Watching list"},
1783 {Key: "no", Label: "No thanks"},
1784 }
1785
1786 selectedOption, err := DynamicSelect(options)
1787 if err != nil {
1788 Log(fmt.Sprintf("Error in sequel resume prompt: %v", err))
1789 return
1790 }
1791
1792 if selectedOption.Key == "yes" {
1793 err = AddAnimeToList(sequel.ID, "CURRENT", userToken)
1794 if err != nil {
1795 Log(fmt.Sprintf("Error moving sequel to watching list: %v", err))
1796 CurdOut("Failed to move sequel to Watching list")
1797 } else {
1798 CurdOut(fmt.Sprintf("Moved '%s' to Watching list!", sequelTitle))
1799 }
1800 }
1801 }
1802 }
16491803}
16501804
1805// promptSequelNotInList prompts user to add sequel when we can't check their list
1806func promptSequelNotInList(sequel *SequelInfo, sequelTitle string, userToken string, anime *Anime) {
1807 CurdOut(fmt.Sprintf("A sequel is available: %s", sequelTitle))
1808
1809 options := []SelectionOption{
1810 {Key: "watching", Label: "Add to Watching list"},
1811 {Key: "planning", Label: "Add to Plan to Watch"},
1812 {Key: "skip", Label: "No thanks"},
1813 }
1814
1815 selectedOption, err := DynamicSelect(options)
1816 if err != nil {
1817 Log(fmt.Sprintf("Error in sequel prompt: %v", err))
1818 return
1819 }
1820
1821 switch selectedOption.Key {
1822 case "watching":
1823 err = AddAnimeToList(sequel.ID, "CURRENT", userToken)
1824 if err != nil {
1825 Log(fmt.Sprintf("Error adding sequel to watching list: %v", err))
1826 CurdOut("Failed to add sequel to watching list")
1827 } else {
1828 CurdOut(fmt.Sprintf("Added '%s' to your Watching list!", sequelTitle))
1829 }
1830 case "planning":
1831 err = AddAnimeToList(sequel.ID, "PLANNING", userToken)
1832 if err != nil {
1833 Log(fmt.Sprintf("Error adding sequel to planning list: %v", err))
1834 CurdOut("Failed to add sequel to Plan to Watch")
1835 } else {
1836 CurdOut(fmt.Sprintf("Added '%s' to your Plan to Watch list!", sequelTitle))
1837 }
1838 case "skip", "-1":
1839 Log("User declined to add sequel")
1840 }
1841}