Commit 53899cc4c9

53899cc4c9a1b100a5e135206979deb045eb1694

parent: 3957bf0a41

Unsigned

Wraient <wraient.911@gmail.com> · 2024-12-18 17:44 UTC

Fix: Continue last and filler block progress update after few episodes (api unfuck)

Layout: unified · split

cmd/curd/main.go +10 −6
@@ -222,6 +222,9 @@ func main() {
222222
223223 // If not filler/recap (or skip is disabled), break and continue with playback
224224 if !((anime.Ep.IsFiller && userCurdConfig.SkipFiller) || (anime.Ep.IsRecap && userCurdConfig.SkipRecap)) {
225 if anime.Ep.LastWasSkipped {
226 go internal.UpdateAnimeProgress(user.Token, anime.AnilistId, anime.Ep.Number-1)
227 }
225228 break
226229 }
227230
@@ -231,11 +234,12 @@ func main() {
231234 } else {
232235 internal.CurdOut(fmt.Sprint("Recap episode, skipping: ", anime.Ep.Number))
233236 }
234
237
235238 anime.Ep.Number++
239 anime.Ep.LastWasSkipped = true
236240 anime.Ep.Started = false
237241 internal.LocalUpdateAnime(databaseFile, anime.AnilistId, anime.AllanimeId, anime.Ep.Number, 0, 0, internal.GetAnimeName(anime))
238
242
239243 // Check if we've reached the end of the series
240244 if anime.Ep.Number > anime.TotalEpisodes {
241245 internal.CurdOut("Reached end of series")
@@ -447,9 +451,8 @@ func main() {
447451 }
448452 }
449453 _, err := internal.MPVSendCommand(anime.Ep.Player.SocketPath, []interface{}{"get_property", "time-pos"})
450 if err == nil && anime.Ep.Started{
454 if err == nil && anime.Ep.Started {
451455 anime.Ep.Player.Speed, err = internal.GetMPVPlaybackSpeed(anime.Ep.Player.SocketPath)
452 internal.Log(anime.Ep.Player.Speed, logFile)
453456 if err != nil {
454457 internal.Log("Failed to get mpv speed "+err.Error(), logFile)
455458 }
@@ -466,6 +469,7 @@ func main() {
466469 wg = sync.WaitGroup{}
467470
468471 if anime.Ep.IsCompleted && !anime.Rewatching {
472 // Update progress for both regular episodes and skipped fillers
469473 go func() {
470474 err = internal.UpdateAnimeProgress(user.Token, anime.AnilistId, anime.Ep.Number-1)
471475 if err != nil {
@@ -481,7 +485,7 @@ func main() {
481485 err = internal.RateAnime(user.Token, anime.AnilistId)
482486 if err != nil {
483487 internal.Log("Error rating anime: "+err.Error(), logFile)
484 internal.CurdOut("Error rating anime: "+err.Error())
488 internal.CurdOut("Error rating anime: " + err.Error())
485489 }
486490 internal.LocalDeleteAnime(databaseFile, anime.AnilistId, anime.AllanimeId)
487491 internal.ExitCurd(nil)
@@ -499,7 +503,7 @@ func main() {
499503 "yes": "Yes",
500504 "no": "No",
501505 }
502
506
503507 selectedOption, err := internal.DynamicSelect(options, false)
504508 if err != nil {
505509 internal.ExitCurd(err)
internal/curd.go +97 −63
@@ -555,6 +555,11 @@ func SetupCurd(userCurdConfig *CurdConfig, anime *Anime, user *User, databaseAni
555555 var anilistUserData map[string]interface{}
556556 var anilistUserDataPreview map[string]interface{}
557557
558 // Filter anime list based on selected category
559 var animeListMap map[string]string
560 var animeListMapPreview map[string]RofiSelectPreview
561
562
558563 // Get user id, username and Anime list
559564 user.Id, user.Username, err = GetAnilistUserID(user.Token)
560565 if err != nil {
@@ -579,77 +584,106 @@ func SetupCurd(userCurdConfig *CurdConfig, anime *Anime, user *User, databaseAni
579584 user.AnimeList = ParseAnimeList(anilistUserData)
580585 }
581586
582 // Skip category selection if Current flag is set
583 var categorySelection SelectionOption
584 if userCurdConfig.CurrentCategory {
585 categorySelection = SelectionOption{
586 Key: "CURRENT",
587 Label: "Currently Watching",
588 }
589 } else {
590 // Create category selection map
591 categories := map[string]string{
592 "ALL": "Show All",
593 "CURRENT": "Currently Watching",
594 "PAUSED": "On Hold",
595 "PLANNING": "Plan to Watch",
596 "COMPLETED": "Completed",
597 "DROPPED": "Dropped",
598 "UPDATE": "Update Anime Entry",
599 "UNTRACKED": "Untracked Watching",
587 // If continueLast flag is set, directly get the last watched anime
588 if anime.Ep.ContinueLast {
589 // Get the last anime ID from the curd_id file
590 idFilePath := filepath.Join(os.ExpandEnv(userCurdConfig.StoragePath), "curd_id")
591 idBytes, err := os.ReadFile(idFilePath)
592 if err != nil {
593 Log("Error reading curd_id file: "+err.Error(), logFile)
594 ExitCurd(fmt.Errorf("No last watched anime found"))
600595 }
601
602 // Select category using DynamicSelect
603 var err error
604 categorySelection, err = DynamicSelect(categories, false)
596
597 anilistID, err := strconv.Atoi(string(idBytes))
605598 if err != nil {
606 Log(fmt.Sprintf("Failed to select category: %v", err), logFile)
607 ExitCurd(fmt.Errorf("Failed to select category"))
599 Log("Error converting anilist ID: "+err.Error(), logFile)
600 ExitCurd(fmt.Errorf("Invalid anime ID in curd_id file"))
608601 }
609602
610 if categorySelection.Key == "-1" {
611 ExitCurd(nil)
603 // Find the anime in database
604 animePointer := LocalFindAnime(*databaseAnimes, anilistID, "")
605 if animePointer == nil {
606 ExitCurd(fmt.Errorf("Last watched anime not found in database"))
612607 }
613608
614 // Handle UPDATE option
615 if categorySelection.Key == "UPDATE" {
616 ClearScreen()
617 UpdateAnimeEntry(userCurdConfig, user, logFile)
618 ExitCurd(nil)
619 } else if categorySelection.Key == "UNTRACKED" {
620 ClearScreen()
621 WatchUntracked(userCurdConfig, logFile)
622 }
609 // Set the anime details
610 anime.AnilistId = animePointer.AnilistId
611 // anime.AllanimeId = animePointer.AllanimeId
612 // anime.Title = animePointer.Title
613 // anime.Ep.Number = animePointer.Ep.Number
614 // anime.Ep.Player.PlaybackTime = animePointer.Ep.Player.PlaybackTime
615 // anime.Ep.Resume = true
616
617 } else {
618 // Skip category selection if Current flag is set
619 var categorySelection SelectionOption
620 if userCurdConfig.CurrentCategory {
621 categorySelection = SelectionOption{
622 Key: "CURRENT",
623 Label: "Currently Watching",
624 }
625 } else {
626 // Create category selection map
627 categories := map[string]string{
628 "ALL": "Show All",
629 "CURRENT": "Currently Watching",
630 "PAUSED": "On Hold",
631 "PLANNING": "Plan to Watch",
632 "COMPLETED": "Completed",
633 "DROPPED": "Dropped",
634 "UPDATE": "Update Anime Entry",
635 "UNTRACKED": "Untracked Watching",
636 }
623637
624 ClearScreen()
625 }
638 // Select category using DynamicSelect
639 var err error
640 categorySelection, err = DynamicSelect(categories, false)
641 if err != nil {
642 Log(fmt.Sprintf("Failed to select category: %v", err), logFile)
643 ExitCurd(fmt.Errorf("Failed to select category"))
644 }
626645
627 // Filter anime list based on selected category
628 var animeListMap map[string]string
629 var animeListMapPreview map[string]RofiSelectPreview
630
631 if userCurdConfig.RofiSelection && userCurdConfig.ImagePreview {
632 animeListMapPreview = make(map[string]RofiSelectPreview)
633 for _, entry := range getEntriesByCategory(user.AnimeList, categorySelection.Key) {
634 title := entry.Media.Title.English
635 if title == "" || userCurdConfig.AnimeNameLanguage == "romaji" {
636 title = entry.Media.Title.Romaji
637 }
638 animeListMapPreview[strconv.Itoa(entry.Media.ID)] = RofiSelectPreview{
639 Title: title,
640 CoverImage: entry.CoverImage,
641 }
642 }
643 } else {
644 animeListMap = make(map[string]string)
645 for _, entry := range getEntriesByCategory(user.AnimeList, categorySelection.Key) {
646 title := entry.Media.Title.English
647 if title == "" || userCurdConfig.AnimeNameLanguage == "romaji" {
648 title = entry.Media.Title.Romaji
649 }
650 animeListMap[strconv.Itoa(entry.Media.ID)] = title
651 }
652 }
646 if categorySelection.Key == "-1" {
647 ExitCurd(nil)
648 }
649
650 // Handle UPDATE option
651 if categorySelection.Key == "UPDATE" {
652 ClearScreen()
653 UpdateAnimeEntry(userCurdConfig, user, logFile)
654 ExitCurd(nil)
655 } else if categorySelection.Key == "UNTRACKED" {
656 ClearScreen()
657 WatchUntracked(userCurdConfig, logFile)
658 }
659
660 ClearScreen()
661 }
662
663
664 if userCurdConfig.RofiSelection && userCurdConfig.ImagePreview {
665 animeListMapPreview = make(map[string]RofiSelectPreview)
666 for _, entry := range getEntriesByCategory(user.AnimeList, categorySelection.Key) {
667 title := entry.Media.Title.English
668 if title == "" || userCurdConfig.AnimeNameLanguage == "romaji" {
669 title = entry.Media.Title.Romaji
670 }
671 animeListMapPreview[strconv.Itoa(entry.Media.ID)] = RofiSelectPreview{
672 Title: title,
673 CoverImage: entry.CoverImage,
674 }
675 }
676 } else {
677 animeListMap = make(map[string]string)
678 for _, entry := range getEntriesByCategory(user.AnimeList, categorySelection.Key) {
679 title := entry.Media.Title.English
680 if title == "" || userCurdConfig.AnimeNameLanguage == "romaji" {
681 title = entry.Media.Title.Romaji
682 }
683 animeListMap[strconv.Itoa(entry.Media.ID)] = title
684 }
685 }
686 }
653687
654688 var anilistSelectedOption SelectionOption
655689 var selectedAllanimeAnime SelectionOption
internal/structs.go +41 −40
@@ -8,13 +8,13 @@ type AnimeTitle struct {
88
99type Anime struct {
1010 Title AnimeTitle `json:"title"`
11 Ep Episode `json:"ep"`
12 CoverImage string `json:"url"` // Assuming this field corresponds to the cover image URL
13 TotalEpisodes int `json:"total_episodes"` // If provided by the API
11 Ep Episode `json:"ep"`
12 CoverImage string `json:"url"` // Assuming this field corresponds to the cover image URL
13 TotalEpisodes int `json:"total_episodes"` // If provided by the API
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 AllanimeId string // Can be populated as necessary
17 AllanimeId string // Can be populated as necessary
1818}
1919
2020type Skip struct {
@@ -28,34 +28,35 @@ type SkipTimes struct {
2828}
2929
3030type Episode struct {
31 Title AnimeTitle `json:"title"`
32 Number int `json:"number"`
33 SkipTimes SkipTimes `json:"skip_times"`
34 Player playingVideo `json:"player"`
35 Resume bool `json:"resume"`
36 Started bool `json:"started"`
37 Duration int `json:"duration"`
38 Links []string `json:"links"`
39 IsFiller bool `json:"filler"`
40 IsRecap bool `json:"recap"`
41 Aired string `json:"aired"`
42 Synopsis string `json:"synopsis"`
43 ContinueLast bool
44 IsCompleted bool
31 Title AnimeTitle `json:"title"`
32 Number int `json:"number"`
33 SkipTimes SkipTimes `json:"skip_times"`
34 Player playingVideo `json:"player"`
35 Resume bool `json:"resume"`
36 Started bool `json:"started"`
37 Duration int `json:"duration"`
38 Links []string `json:"links"`
39 IsFiller bool `json:"filler"`
40 IsRecap bool `json:"recap"`
41 Aired string `json:"aired"`
42 Synopsis string `json:"synopsis"`
43 ContinueLast bool
44 LastWasSkipped bool // used in filler check
45 IsCompleted bool
4546}
4647
4748type playingVideo struct {
4849 Url string
4950 Speed float64 `json:"speed"`
5051 PlaybackTime int `json:"playback_time"`
51 SocketPath string
52 SocketPath string
5253}
5354
5455type User struct {
55 Token string
56 Username string
57 Id int
58 AnimeList AnimeList
56 Token string
57 Username string
58 Id int
59 AnimeList AnimeList
5960}
6061
6162// AniListAnime is the struct for the API response
@@ -82,29 +83,29 @@ type ResponseData struct {
8283}
8384
8485type Media struct {
85 Duration int `json:"duration"`
86 Episodes int `json:"episodes"`
87 ID int `json:"id"`
88 Title AnimeTitle `json:"title"`
86 Duration int `json:"duration"`
87 Episodes int `json:"episodes"`
88 ID int `json:"id"`
89 Title AnimeTitle `json:"title"`
8990}
9091
9192type Entry struct {
92 Media Media `json:"media"`
93 Progress int `json:"progress"`
94 Score float64 `json:"score"`
95 Status string `json:"status"`
96 CoverImage string `json:"coverImage"`
93 Media Media `json:"media"`
94 Progress int `json:"progress"`
95 Score float64 `json:"score"`
96 Status string `json:"status"`
97 CoverImage string `json:"coverImage"`
9798}
9899
99100type AnimeList struct {
100 Watching []Entry `json:"watching"`
101 Completed []Entry `json:"completed"`
102 Paused []Entry `json:"paused"`
103 Dropped []Entry `json:"dropped"`
104 Planning []Entry `json:"planning"`
101 Watching []Entry `json:"watching"`
102 Completed []Entry `json:"completed"`
103 Paused []Entry `json:"paused"`
104 Dropped []Entry `json:"dropped"`
105 Planning []Entry `json:"planning"`
105106}
106107
107108type RofiSelectPreview struct {
108 Title string `json:"title"`
109 CoverImage string `json:"coverImage"`
110}
\ No newline at end of file
109 Title string `json:"title"`
110 CoverImage string `json:"coverImage"`
111}