Commit 447310e3ba

447310e3baa5b19e2c6b99aac66d56646e7c14bb

parent: b08c42f0d4

Unregistered key

Rushikesh Gaikwad <81632222+Wraient@users.noreply.github.com> · 2026-03-31 15:53 UTC
committer: <noreply@github.com>

Add: Rewatching Support (#84)

Layout: unified · split

internal/anilist.go +120 −37
@@ -454,6 +454,17 @@ func GetUserData(token string, userID int) (map[string]interface{}, error) {
454454 status
455455 score
456456 progress
457 repeat
458 startedAt {
459 year
460 month
461 day
462 }
463 completedAt {
464 year
465 month
466 day
467 }
457468 }
458469 }
459470 }
@@ -495,6 +506,17 @@ func GetUserDataPreview(token string, userID int) (map[string]interface{}, error
495506 status
496507 score
497508 progress
509 repeat
510 startedAt {
511 year
512 month
513 day
514 }
515 completedAt {
516 year
517 month
518 day
519 }
498520 }
499521 }
500522 }
@@ -562,26 +584,7 @@ func SearchAnimeByTitle(jsonData map[string]interface{}, searchTitle string) []m
562584
563585// Function to update anime progress
564586func UpdateAnimeProgress(token string, mediaID, progress int) error {
565 url := "https://graphql.anilist.co"
566 query := `
567 mutation($mediaId: Int, $progress: Int) {
568 SaveMediaListEntry(mediaId: $mediaId, progress: $progress) {
569 id
570 progress
571 }
572 }`
573
574 variables := map[string]interface{}{
575 "mediaId": mediaID,
576 "progress": progress,
577 }
578
579 headers := map[string]string{
580 "Authorization": "Bearer " + token,
581 "Content-Type": "application/json",
582 }
583
584 _, err := makePostRequest(url, query, variables, headers)
587 err := SaveAnimeListEntry(token, mediaID, nil, &progress, nil, nil, nil)
585588 if err != nil {
586589 return err
587590 }
@@ -591,18 +594,85 @@ func UpdateAnimeProgress(token string, mediaID, progress int) error {
591594}
592595
593596func UpdateAnimeStatus(token string, mediaID int, status string) error {
597 err := SaveAnimeListEntry(token, mediaID, &status, nil, nil, nil, nil)
598 if err != nil {
599 return fmt.Errorf("failed to update anime status: %w", err)
600 }
601
602 statusMap := map[string]string{
603 "CURRENT": "Currently Watching",
604 "COMPLETED": "Completed",
605 "PAUSED": "On Hold",
606 "DROPPED": "Dropped",
607 "PLANNING": "Plan to Watch",
608 "REPEATING": "Rewatching",
609 }
610
611 CurdOut(fmt.Sprintf("Anime status updated to: %s", statusMap[status]))
612 return nil
613}
614
615func SaveAnimeListEntry(token string, mediaID int, status *string, progress *int, repeat *int, startedAt *FuzzyDate, completedAt *FuzzyDate) error {
594616 url := "https://graphql.anilist.co"
595617 query := `
596 mutation($mediaId: Int, $status: MediaListStatus) {
597 SaveMediaListEntry(mediaId: $mediaId, status: $status) {
618 mutation(
619 $mediaId: Int
620 $status: MediaListStatus
621 $progress: Int
622 $repeat: Int
623 $startedAt: FuzzyDateInput
624 $completedAt: FuzzyDateInput
625 ) {
626 SaveMediaListEntry(
627 mediaId: $mediaId
628 status: $status
629 progress: $progress
630 repeat: $repeat
631 startedAt: $startedAt
632 completedAt: $completedAt
633 ) {
598634 id
599635 status
636 progress
637 repeat
638 startedAt {
639 year
640 month
641 day
642 }
643 completedAt {
644 year
645 month
646 day
647 }
600648 }
601649 }`
602650
603651 variables := map[string]interface{}{
604652 "mediaId": mediaID,
605 "status": status,
653 }
654 if status != nil {
655 variables["status"] = *status
656 }
657 if progress != nil {
658 variables["progress"] = *progress
659 }
660 if repeat != nil {
661 variables["repeat"] = *repeat
662 }
663 if startedAt != nil && (startedAt.Year != 0 || startedAt.Month != 0 || startedAt.Day != 0) {
664 variables["startedAt"] = map[string]int{
665 "year": startedAt.Year,
666 "month": startedAt.Month,
667 "day": startedAt.Day,
668 }
669 }
670 if completedAt != nil && (completedAt.Year != 0 || completedAt.Month != 0 || completedAt.Day != 0) {
671 variables["completedAt"] = map[string]int{
672 "year": completedAt.Year,
673 "month": completedAt.Month,
674 "day": completedAt.Day,
675 }
606676 }
607677
608678 headers := map[string]string{
@@ -612,21 +682,17 @@ func UpdateAnimeStatus(token string, mediaID int, status string) error {
612682
613683 _, err := makePostRequest(url, query, variables, headers)
614684 if err != nil {
615 return fmt.Errorf("failed to update anime status: %w", err)
616 }
617
618 statusMap := map[string]string{
619 "CURRENT": "Currently Watching",
620 "COMPLETED": "Completed",
621 "PAUSED": "On Hold",
622 "DROPPED": "Dropped",
623 "PLANNING": "Plan to Watch",
685 return err
624686 }
625
626 CurdOut(fmt.Sprintf("Anime status updated to: %s", statusMap[status]))
627687 return nil
628688}
629689
690func CompleteAnimeRewatch(token string, anime Anime) error {
691 status := "COMPLETED"
692 repeat := anime.Repeat + 1
693 return SaveAnimeListEntry(token, anime.AnilistId, &status, nil, &repeat, &anime.StartedAt, &anime.CompletedAt)
694}
695
630696// Function to rate an anime on AniList
631697func RateAnime(token string, mediaID int) error {
632698 var score float64
@@ -755,6 +821,19 @@ func ParseAnimeList(input map[string]interface{}) AnimeList {
755821 return ""
756822 }
757823
824 parseFuzzyDate := func(value interface{}) FuzzyDate {
825 dateMap, ok := value.(map[string]interface{})
826 if !ok || dateMap == nil {
827 return FuzzyDate{}
828 }
829
830 return FuzzyDate{
831 Year: toInt(dateMap["year"]),
832 Month: toInt(dateMap["month"]),
833 Day: toInt(dateMap["day"]),
834 }
835 }
836
758837 // Access the list entries in the input map
759838 if input["data"] == nil {
760839 Log("Anilist request failed")
@@ -783,9 +862,12 @@ func ParseAnimeList(input map[string]interface{}) AnimeList {
783862 },
784863 Status: safeString(media["status"]),
785864 },
786 Progress: toInt(entryData["progress"]),
787 Score: entryData["score"].(float64),
788 Status: safeString(entryData["status"]), // Ensure status is fetched safely
865 Progress: toInt(entryData["progress"]),
866 Repeat: toInt(entryData["repeat"]),
867 Score: entryData["score"].(float64),
868 Status: safeString(entryData["status"]), // Ensure status is fetched safely
869 StartedAt: parseFuzzyDate(entryData["startedAt"]),
870 CompletedAt: parseFuzzyDate(entryData["completedAt"]),
789871 }
790872
791873 if userCurdConfig.RofiSelection && userCurdConfig.ImagePreview {
@@ -1086,6 +1168,7 @@ func AddAnimeToList(animeID int, status string, token string) error {
10861168 "PAUSED": "On Hold",
10871169 "DROPPED": "Dropped",
10881170 "PLANNING": "Plan to Watch",
1171 "REPEATING": "Rewatching",
10891172 }
10901173
10911174 CurdOut(fmt.Sprintf("Anime added to: %s", statusMap[status]))
internal/curd.go +97 −43
@@ -726,6 +726,7 @@ func SetupCurd(userCurdConfig *CurdConfig, anime *Anime, user *User, databaseAni
726726 var err error
727727 var anilistUserData map[string]interface{}
728728 var anilistUserDataPreview map[string]interface{}
729 var startingRewatch bool
729730
730731 // Filter anime list based on selected category
731732 var animeListOptions []SelectionOption
@@ -763,6 +764,8 @@ func SetupCurd(userCurdConfig *CurdConfig, anime *Anime, user *User, databaseAni
763764
764765 // Navigation loop for the entire setup process
765766 for {
767 startingRewatch = false
768
766769 // If continueLast flag is set, directly get the last watched anime
767770 if anime.Ep.ContinueLast {
768771 // Get the last anime ID from the curd_id file
@@ -966,10 +969,47 @@ func SetupCurd(userCurdConfig *CurdConfig, anime *Anime, user *User, databaseAni
966969 anime.Title = selectedAnilistAnime.Media.Title
967970 anime.TotalEpisodes = selectedAnilistAnime.Media.Episodes
968971 anime.IsAiring = selectedAnilistAnime.Media.Status == "RELEASING" || selectedAnilistAnime.Media.Status == "NOT_YET_RELEASED"
972 anime.Rewatching = selectedAnilistAnime.Status == "REPEATING"
973 anime.Repeat = selectedAnilistAnime.Repeat
974 anime.StartedAt = selectedAnilistAnime.StartedAt
975 anime.CompletedAt = selectedAnilistAnime.CompletedAt
969976 anime.Ep.Number = selectedAnilistAnime.Progress + 1
970977 var animeList []SelectionOption
971978 userQuery = anime.Title.Romaji
972979
980 if selectedAnilistAnime.Status == "COMPLETED" {
981 status := "REPEATING"
982 progress := 0
983 err = SaveAnimeListEntry(user.Token, anime.AnilistId, &status, &progress, nil, &anime.StartedAt, &anime.CompletedAt)
984 if err != nil {
985 Log(fmt.Sprintf("Error starting anime rewatch: %v", err))
986 ExitCurd(fmt.Errorf("Failed to move anime to rewatching"))
987 }
988
989 anime.Rewatching = true
990 anime.Ep.Number = 1
991 anime.Ep.Player.PlaybackTime = 0
992 anime.Ep.Resume = false
993 startingRewatch = true
994 CurdOut("Moved anime to Rewatching and restarting from episode 1.")
995
996 if userCurdConfig.RofiSelection && userCurdConfig.ImagePreview {
997 anilistUserDataPreview, err = GetUserDataPreview(user.Token, user.Id)
998 if err != nil {
999 Log("Error refreshing anime list: " + err.Error())
1000 ExitCurd(err)
1001 }
1002 user.AnimeList = ParseAnimeList(anilistUserDataPreview)
1003 } else {
1004 anilistUserData, err = GetUserData(user.Token, user.Id)
1005 if err != nil {
1006 Log("Error refreshing anime list: " + err.Error())
1007 ExitCurd(err)
1008 }
1009 user.AnimeList = ParseAnimeList(anilistUserData)
1010 }
1011 }
1012
9731013 // if anime not found in database, find it in animeList
9741014 if animePointer == nil {
9751015 Log("Anime not found in database, searching in animeList...")
@@ -1013,7 +1053,7 @@ func SetupCurd(userCurdConfig *CurdConfig, anime *Anime, user *User, databaseAni
10131053 matches := malRegex.FindStringSubmatch(option.Thumbnail)
10141054 if len(matches) > 1 {
10151055 fileName := matches[1]
1016
1056
10171057 // Fetch Jikan pictures lazily (only once)
10181058 if !fetchedJikan {
10191059 if anime.MalId == 0 {
@@ -1094,6 +1134,11 @@ func SetupCurd(userCurdConfig *CurdConfig, anime *Anime, user *User, databaseAni
10941134 }
10951135 }
10961136
1137 if startingRewatch {
1138 anime.Ep.Player.PlaybackTime = 0
1139 anime.Ep.Resume = false
1140 }
1141
10971142 if selectedAllanimeAnime.Key == "-1" {
10981143 ExitCurd(nil)
10991144 }
@@ -1106,6 +1151,17 @@ func SetupCurd(userCurdConfig *CurdConfig, anime *Anime, user *User, databaseAni
11061151 break
11071152 }
11081153 }
1154 if !isInWatchingList {
1155 for _, entry := range user.AnimeList.Rewatching {
1156 if entry.Media.ID == anime.AnilistId {
1157 isInWatchingList = true
1158 break
1159 }
1160 }
1161 }
1162 if anime.Rewatching {
1163 isInWatchingList = true
1164 }
11091165
11101166 if !isInWatchingList {
11111167 // Create options for the prompt
@@ -1444,7 +1500,10 @@ func getEntriesByCategory(list AnimeList, category string) []Entry {
14441500 allEntries = append(allEntries, list.Rewatching...)
14451501 return allEntries
14461502 case "CURRENT":
1447 return list.Watching
1503 currentEntries := make([]Entry, 0, len(list.Watching)+len(list.Rewatching))
1504 currentEntries = append(currentEntries, list.Watching...)
1505 currentEntries = append(currentEntries, list.Rewatching...)
1506 return currentEntries
14481507 case "COMPLETED":
14491508 return list.Completed
14501509 case "PAUSED":
@@ -1541,15 +1600,12 @@ func NextEpisodePromptContinuous(userCurdConfig *CurdConfig, databaseFile string
15411600 Log("Error updating local database on quit: " + err.Error())
15421601 }
15431602
1544 // Update Anilist progress if not rewatching
1545 if !anime.Rewatching {
1546 go func() {
1547 err = UpdateAnimeProgress(userToken, anime.AnilistId, anime.Ep.Number)
1548 if err != nil {
1549 Log("Error updating Anilist progress on quit: " + err.Error())
1550 }
1551 }()
1552 }
1603 go func() {
1604 err = UpdateAnimeProgress(userToken, anime.AnilistId, anime.Ep.Number)
1605 if err != nil {
1606 Log("Error updating Anilist progress on quit: " + err.Error())
1607 }
1608 }()
15531609
15541610 CurdOut(fmt.Sprintf("Episode completed (%.1f%% watched). Exiting.", percentageWatched))
15551611 } else {
@@ -1571,15 +1627,12 @@ func NextEpisodePromptContinuous(userCurdConfig *CurdConfig, databaseFile string
15711627 Log("Error updating local database with completed episode: " + err.Error())
15721628 }
15731629
1574 // Update Anilist progress for the completed episode if not rewatching
1575 if !anime.Rewatching {
1576 go func() {
1577 err = UpdateAnimeProgress(userToken, anime.AnilistId, anime.Ep.Number)
1578 if err != nil {
1579 Log("Error updating Anilist progress: " + err.Error())
1580 }
1581 }()
1582 }
1630 go func() {
1631 err = UpdateAnimeProgress(userToken, anime.AnilistId, anime.Ep.Number)
1632 if err != nil {
1633 Log("Error updating Anilist progress: " + err.Error())
1634 }
1635 }()
15831636
15841637 // Increment to next episode and update database with next episode number and 0 playback time
15851638 anime.Ep.Number++
@@ -1641,14 +1694,11 @@ func StartNextEpisode(anime *Anime, userCurdConfig *CurdConfig, databaseFile str
16411694 // Handle scoring and completion for the last episode
16421695 HandleLastEpisodeCompletion(userCurdConfig, anime, userToken)
16431696
1644 // Update Anilist progress for the last episode if not rewatching
1645 if !anime.Rewatching {
1646 err := UpdateAnimeProgress(userToken, anime.AnilistId, prevEpisode)
1647 if err != nil {
1648 Log("Error updating Anilist progress: " + err.Error())
1649 }
1650 // Note: UpdateAnimeProgress already outputs a message on success
1697 err := UpdateAnimeProgress(userToken, anime.AnilistId, prevEpisode)
1698 if err != nil {
1699 Log("Error updating Anilist progress: " + err.Error())
16511700 }
1701 // Note: UpdateAnimeProgress already outputs a message on success
16521702
16531703 CurdOut("Series completed!")
16541704 ExitCurd(nil)
@@ -1688,15 +1738,12 @@ func StartNextEpisode(anime *Anime, userCurdConfig *CurdConfig, databaseFile str
16881738 Log("Error updating local database: " + err.Error())
16891739 }
16901740
1691 // Update Anilist progress for the previous episode if not rewatching
1692 if !anime.Rewatching {
1693 go func() {
1694 err = UpdateAnimeProgress(userToken, anime.AnilistId, prevEpisode)
1695 if err != nil {
1696 Log("Error updating Anilist progress: " + err.Error())
1697 }
1698 }()
1699 }
1741 go func() {
1742 err = UpdateAnimeProgress(userToken, anime.AnilistId, prevEpisode)
1743 if err != nil {
1744 Log("Error updating Anilist progress: " + err.Error())
1745 }
1746 }()
17001747
17011748 // Output message to user
17021749 CurdOut(fmt.Sprint("Starting next episode: ", anime.Ep.Number))
@@ -1729,14 +1776,21 @@ func HandleLastEpisodeCompletion(userCurdConfig *CurdConfig, anime *Anime, userT
17291776 // Back (-2) and no are treated as skip
17301777 }
17311778
1732 // Update anime status to completed on AniList
1733 // This is done regardless of scoring preference
1734 if anime.TotalEpisodes > 0 && anime.Ep.Number == anime.TotalEpisodes && !anime.Rewatching && !anime.IsAiring {
1735 err := UpdateAnimeStatus(userToken, anime.AnilistId, "COMPLETED")
1736 if err != nil {
1737 Log("Error updating anime status to completed: " + err.Error())
1779 // Update anime status to completed on AniList.
1780 // For rewatches, preserve the original completion date and only increment repeat count.
1781 if anime.TotalEpisodes > 0 && anime.Ep.Number == anime.TotalEpisodes && !anime.IsAiring {
1782 if anime.Rewatching {
1783 err := CompleteAnimeRewatch(userToken, *anime)
1784 if err != nil {
1785 Log("Error completing anime rewatch: " + err.Error())
1786 }
1787 } else {
1788 err := UpdateAnimeStatus(userToken, anime.AnilistId, "COMPLETED")
1789 if err != nil {
1790 Log("Error updating anime status to completed: " + err.Error())
1791 }
1792 // Note: UpdateAnimeStatus already outputs a message on success
17381793 }
1739 // Note: UpdateAnimeStatus already outputs a message on success
17401794 }
17411795
17421796 // Check for sequel after completion (only if this is the last episode)
internal/structs.go +17 −5
@@ -14,11 +14,20 @@ type Anime struct {
1414 MalId int `json:"mal_id"`
1515 AnilistId int `json:"anilist_id"` // Assuming you have an Anilist ID in your struct
1616 Rewatching bool
17 Repeat int
18 StartedAt FuzzyDate
19 CompletedAt FuzzyDate
1720 AllanimeId string // Can be populated as necessary
1821 FillerEpisodes []int
1922 IsAiring bool
2023}
2124
25type FuzzyDate struct {
26 Year int `json:"year"`
27 Month int `json:"month"`
28 Day int `json:"day"`
29}
30
2231type Skip struct {
2332 Start int `json:"start"`
2433 End int `json:"end"`
@@ -99,11 +108,14 @@ type Media struct {
99108}
100109
101110type Entry struct {
102 Media Media `json:"media"`
103 Progress int `json:"progress"`
104 Score float64 `json:"score"`
105 Status string `json:"status"`
106 CoverImage string `json:"coverImage"`
111 Media Media `json:"media"`
112 Progress int `json:"progress"`
113 Repeat int `json:"repeat"`
114 Score float64 `json:"score"`
115 Status string `json:"status"`
116 StartedAt FuzzyDate `json:"startedAt"`
117 CompletedAt FuzzyDate `json:"completedAt"`
118 CoverImage string `json:"coverImage"`
107119}
108120
109121type AnimeList struct {