Commit 9f2baecd2e

9f2baecd2e5160dba1e6241b835eadd5c0bab976

parent: 34198c64ae

Unsigned

Wraient <wraient.911@gmail.com> · 2024-10-22 17:53 UTC

Add: Continue last anime function added

Layout: unified · split

cmd/curd/main.go +6 −4
@@ -50,10 +50,11 @@ func main() {
5050 flag.BoolVar(&userCurdConfig.ScoreOnCompletion, "score-on-completion", userCurdConfig.ScoreOnCompletion, "Score on episode completion (true/false)")
5151 flag.BoolVar(&userCurdConfig.SaveMpvSpeed, "save-mpv-speed", userCurdConfig.SaveMpvSpeed, "Save MPV speed setting (true/false)")
5252 flag.BoolVar(&userCurdConfig.DiscordPresence, "discord-presence", userCurdConfig.DiscordPresence, "Enable Discord presence (true/false)")
53
53 continueLast := flag.Bool("c", false, "Continue last episode")
54
5455 subFlag := flag.Bool("sub", false, "Watch sub version")
5556 dubFlag := flag.Bool("dub", false, "Watch dub version")
56
57
5758 // Custom help/usage function
5859 flag.Usage = func() {
5960 fmt.Fprintf(os.Stderr, "Curd is a CLI tool to manage anime playback with advanced features like skipping intro, outro, filler, recap, tracking progress, and integrating with Discord.\n")
@@ -62,6 +63,8 @@ func main() {
6263 }
6364
6465 flag.Parse()
66
67 anime.Ep.ContinueLast = *continueLast
6568
6669 // Set SubOrDub based on the flags
6770 if *subFlag {
@@ -81,7 +84,7 @@ func main() {
8184 internal.WriteTokenToFile(user.Token, os.ExpandEnv("$HOME/Projects/curd/.local/share/curd/token"))
8285 }
8386
84 // Load anime in database
87 // Load animes in database
8588 databaseFile := os.ExpandEnv("$HOME/Projects/curd/.config/curd/curd_history.txt")
8689 databaseAnimes := internal.LocalGetAllAnime(databaseFile)
8790
@@ -211,7 +214,6 @@ func main() {
211214 // Exit the skip loop
212215 close(skipLoopDone)
213216 } else if fmt.Sprintf("%v", err) != "invalid character '{' after top-level value" { // Episode is not completed
214 fmt.Println("Have a great day!")
215217 internal.ExitCurd()
216218 } else {
217219 internal.Log("Received invalid JSON response, continuing...", logFile)
internal/curd.go +70 −27
@@ -82,6 +82,7 @@ func RestoreScreen() {
8282
8383func ExitCurd() {
8484 RestoreScreen()
85 fmt.Println("Have a great day!")
8586 os.Exit(0)
8687}
8788
@@ -93,47 +94,78 @@ func SetupCurd(userCurdConfig *CurdConfig, anime *Anime, user *User, databaseAni
9394 anilistUserData, err := GetUserData(user.Token, user.Id)
9495 user.AnimeList = ParseAnimeList(anilistUserData)
9596 animeListMap := GetAnimeMap(user.AnimeList)
97 var anilistSelectedOption SelectionOption
98 var userQuery string
9699
97 // Select anime to watch (Anilist)
98 anilistSelectedOption, err := DynamicSelect(animeListMap)
99 if anilistSelectedOption.Key == "-1" || anilistSelectedOption.Label == "quit" {
100 ExitCurd()
101 }
102 if anilistSelectedOption.Label == "add_new" { // Todo: Add new anime
103 fmt.Println("Enter the anime name:")
104 var query string
105 fmt.Scanln(&query)
106 animeMap, err := SearchAnimeAnilist(query, user.Token)
100 if anime.Ep.ContinueLast {
101 // Get the last watched anime ID from the curd_id file
102 curdIDPath := os.ExpandEnv("$HOME/Projects/curd/.local/share/curd/curd_id")
103 curdIDBytes, err := os.ReadFile(curdIDPath)
107104 if err != nil {
108 fmt.Println("Failed to search anime")
105 Log(fmt.Sprintf("Error reading curd_id file: %v", err), logFile)
109106 ExitCurd()
110107 }
111 anilistSelectedOption, err = DynamicSelect(animeMap)
108
109 lastWatchedID, err := strconv.Atoi(strings.TrimSpace(string(curdIDBytes)))
112110 if err != nil {
113 fmt.Println("No anime available")
111 Log(fmt.Sprintf("Error converting curd_id to integer: %v", err), logFile)
114112 ExitCurd()
115113 }
116 animeID, err := strconv.Atoi(anilistSelectedOption.Key)
114
115 anime.AnilistId = lastWatchedID
116 anilistSelectedOption.Key = strconv.Itoa(lastWatchedID)
117 } else {
118 // Select anime to watch (Anilist)
119 var err error
120 anilistSelectedOption, err = DynamicSelect(animeListMap)
117121 if err != nil {
118 fmt.Println("Failed to convert anime ID to integer")
122 Log(fmt.Sprintf("Error selecting anime: %v", err), logFile)
119123 ExitCurd()
120124 }
121 err = AddAnimeToWatchingList(animeID, user.Token)
125
126 if anilistSelectedOption.Key == "-1" || anilistSelectedOption.Label == "quit" {
127 ExitCurd()
128 }
129
130 if anilistSelectedOption.Label == "add_new" {
131 fmt.Println("Enter the anime name:")
132 var query string
133 fmt.Scanln(&query)
134 animeMap, err := SearchAnimeAnilist(query, user.Token)
135 if err != nil {
136 Log(fmt.Sprintf("Failed to search anime: %v", err), logFile)
137 ExitCurd()
138 }
139 anilistSelectedOption, err = DynamicSelect(animeMap)
140 if err != nil {
141 Log(fmt.Sprintf("No anime available: %v", err), logFile)
142 ExitCurd()
143 }
144 animeID, err := strconv.Atoi(anilistSelectedOption.Key)
145 if err != nil {
146 Log(fmt.Sprintf("Failed to convert anime ID to integer: %v", err), logFile)
147 ExitCurd()
148 }
149 err = AddAnimeToWatchingList(animeID, user.Token)
150 if err != nil {
151 Log(fmt.Sprintf("Failed to add anime to watching list: %v", err), logFile)
152 ExitCurd()
153 }
154 anilistUserData, err := GetUserData(user.Token, user.Id)
155 if err != nil {
156 Log(fmt.Sprintf("Failed to get user data: %v", err), logFile)
157 ExitCurd()
158 }
159 user.AnimeList = ParseAnimeList(anilistUserData)
160 }
161
162 userQuery = anilistSelectedOption.Label
163 anime.AnilistId, err = strconv.Atoi(anilistSelectedOption.Key)
122164 if err != nil {
123 fmt.Println("Failed to add anime to watching list")
165 Log(fmt.Sprintf("Error converting Anilist ID: %v", err), logFile)
124166 ExitCurd()
125167 }
126 anilistUserData, err := GetUserData(user.Token, user.Id)
127 user.AnimeList = ParseAnimeList(anilistUserData)
128 // animeListMap := GetAnimeMap(user.AnimeList)
129168 }
130 userQuery := anilistSelectedOption.Label
131 anime.AnilistId, err = strconv.Atoi(anilistSelectedOption.Key)
132 if err != nil {
133 fmt.Println("Error converting Anilist ID:", err)
134 os.Exit(1)
135 }
136
137169 // Find anime in Local history
138170 animePointer := LocalFindAnime(*databaseAnimes, anime.AnilistId, "")
139171
@@ -244,6 +276,17 @@ func StartCurd(userCurdConfig *CurdConfig, anime *Anime, logFile string) string
244276 }
245277
246278 Log(anime, logFile)
279
280 // Write anime.AnilistId to .local/share/curd/curd_id
281 idFilePath := filepath.Join(os.Getenv("HOME"), "Projects", "curd", ".local", "share", "curd", "curd_id")
282 if err := os.MkdirAll(filepath.Dir(idFilePath), 0755); err != nil {
283 Log(fmt.Sprintf("Failed to create directory for curd_id: %v", err), logFile)
284 } else {
285 if err := os.WriteFile(idFilePath, []byte(fmt.Sprintf("%d", anime.AnilistId)), 0644); err != nil {
286 Log(fmt.Sprintf("Failed to write AnilistId to file: %v", err), logFile)
287 }
288 }
289
247290 mpvSocketPath, err := StartVideo(PrioritizeLink(anime.Ep.Links), []string{})
248291
249292 if err != nil {
internal/structs.go +1
@@ -40,6 +40,7 @@ type Episode struct {
4040 IsRecap bool `json:"recap"`
4141 Aired string `json:"aired"`
4242 Synopsis string `json:"synopsis"`
43 ContinueLast bool
4344 IsCompleted bool
4445}
4546