| @@ -1,6 +1,7 @@ |
| 1 | package internal |
1 | package internal |
| 2 | |
2 | |
| 3 | import ( |
3 | import ( |
| |
4 | "crypto/md5" |
| 4 | "encoding/json" |
5 | "encoding/json" |
| 5 | "fmt" |
6 | "fmt" |
| 6 | "io" |
7 | "io" |
| @@ -148,10 +149,28 @@ func CurdOut(data interface{}) { |
| 148 | Log(fmt.Sprintf("Failed to send notification: %v", err), logFile) |
149 | Log(fmt.Sprintf("Failed to send notification: %v", err), logFile) |
| 149 | } |
150 | } |
| 150 | case "linux": |
151 | case "linux": |
| 151 | cmd := exec.Command("notify-send", fmt.Sprintf("%v", data)) |
152 | // Check if the input starts with "-i" for image notification |
| 152 | err := cmd.Run() |
153 | dataStr := fmt.Sprintf("%v", data) |
| 153 | if err != nil { |
154 | if strings.HasPrefix(dataStr, "-i") && userCurdConfig.ImagePreview && userCurdConfig.RofiSelection { |
| 154 | Log(fmt.Sprintf("Failed to send notification: %v", err), logFile) |
155 | // Split the string to get image path and message |
| |
156 | parts := strings.SplitN(dataStr, " ", 3) |
| |
157 | if len(parts) == 3 { |
| |
158 | // Remove quotes from the message |
| |
159 | message := strings.Trim(parts[2], "\"") |
| |
160 | cmd := exec.Command("notify-send", "-i", parts[1], message) |
| |
161 | err := cmd.Run() |
| |
162 | if err != nil { |
| |
163 | Log(fmt.Sprintf("%v", cmd), logFile) |
| |
164 | Log(fmt.Sprintf("Failed to send notification: %v", err), logFile) |
| |
165 | } |
| |
166 | } |
| |
167 | } else { |
| |
168 | cmd := exec.Command("notify-send", dataStr) |
| |
169 | err := cmd.Run() |
| |
170 | if err != nil { |
| |
171 | Log(fmt.Sprintf("%v", cmd), logFile) |
| |
172 | Log(fmt.Sprintf("Failed to send notification: %v", err), logFile) |
| |
173 | } |
| 155 | } |
174 | } |
| 156 | } |
175 | } |
| 157 | } |
176 | } |
| @@ -293,226 +312,283 @@ func AddNewAnime(userCurdConfig *CurdConfig, anime *Anime, user *User, databaseA |
| 293 | } |
312 | } |
| 294 | |
313 | |
| 295 | func SetupCurd(userCurdConfig *CurdConfig, anime *Anime, user *User, databaseAnimes *[]Anime, logFile string) { |
314 | func SetupCurd(userCurdConfig *CurdConfig, anime *Anime, user *User, databaseAnimes *[]Anime, logFile string) { |
| 296 | var err error |
315 | var err error |
| 297 | var anilistUserData map[string]interface{} |
316 | var anilistUserData map[string]interface{} |
| 298 | var anilistUserDataPreview map[string]interface{} |
317 | var anilistUserDataPreview map[string]interface{} |
| 299 | var animeListMap map[string]string |
318 | |
| 300 | var animeListMapPreview map[string]RofiSelectPreview |
319 | // Get user id, username and Anime list |
| 301 | |
320 | user.Id, user.Username, err = GetAnilistUserID(user.Token) |
| 302 | // Get user id, username and Anime list |
321 | if err != nil { |
| 303 | user.Id, user.Username, err = GetAnilistUserID(user.Token) |
322 | Log(fmt.Sprintf("Failed to get user ID: %v", err), logFile) |
| 304 | if err != nil { |
323 | ExitCurd(fmt.Errorf("Failed to get user ID\nYou can reset the token by running `curd -change-token`")) |
| 305 | Log(fmt.Sprintf("Failed to get user ID: %v", err), logFile) |
324 | } |
| 306 | ExitCurd(fmt.Errorf("Failed to get user ID\nYou can reset the token by running `curd -change-token`")) |
| |
| 307 | } |
| |
| 308 | if userCurdConfig.RofiSelection && userCurdConfig.ImagePreview { |
| |
| 309 | anilistUserDataPreview, err = GetUserDataPreview(user.Token, user.Id) |
| |
| 310 | if err != nil { |
| |
| 311 | Log(fmt.Sprintf("Failed to get user data preview: %v", err), logFile) |
| |
| 312 | ExitCurd(fmt.Errorf("Failed to get user data preview")) |
| |
| 313 | } |
| |
| 314 | user.AnimeList = ParseAnimeList(anilistUserDataPreview) |
| |
| 315 | animeListMapPreview = GetAnimeMapPreview(user.AnimeList) |
| |
| 316 | } else { |
| |
| 317 | anilistUserData, err = GetUserData(user.Token, user.Id) |
| |
| 318 | if err != nil { |
| |
| 319 | Log(fmt.Sprintf("Failed to get user data: %v", err), logFile) |
| |
| 320 | ExitCurd(fmt.Errorf("Failed to get user ID\nYou can reset the token by running `curd -change-token`")) |
| |
| 321 | } |
| |
| 322 | user.AnimeList = ParseAnimeList(anilistUserData) |
| |
| 323 | animeListMap = GetAnimeMap(user.AnimeList) |
| |
| 324 | } |
| |
| 325 | var anilistSelectedOption SelectionOption |
| |
| 326 | var selectedAllanimeAnime SelectionOption |
| |
| 327 | var userQuery string |
| |
| 328 | |
| |
| 329 | if anime.Ep.ContinueLast { |
| |
| 330 | // Get the last watched anime ID from the curd_id file |
| |
| 331 | curdIDPath := filepath.Join(os.ExpandEnv(userCurdConfig.StoragePath), "curd_id") |
| |
| 332 | curdIDBytes, err := os.ReadFile(curdIDPath) |
| |
| 333 | if err != nil { |
| |
| 334 | Log(fmt.Sprintf("Error reading curd_id file: %v", err), logFile) |
| |
| 335 | ExitCurd(fmt.Errorf("Error reading curd_id file")) |
| |
| 336 | } |
| |
| 337 | |
| |
| 338 | lastWatchedID, err := strconv.Atoi(strings.TrimSpace(string(curdIDBytes))) |
| |
| 339 | if err != nil { |
| |
| 340 | Log(fmt.Sprintf("Error converting curd_id to integer: %v", err), logFile) |
| |
| 341 | ExitCurd(fmt.Errorf("Error converting curd_id to integer")) |
| |
| 342 | } |
| |
| 343 | |
| |
| 344 | anime.AnilistId = lastWatchedID |
| |
| 345 | anilistSelectedOption.Key = strconv.Itoa(lastWatchedID) |
| |
| 346 | } else { |
| |
| 347 | // Select anime to watch (Anilist) |
| |
| 348 | var err error |
| |
| 349 | if userCurdConfig.RofiSelection && userCurdConfig.ImagePreview { |
| |
| 350 | anilistSelectedOption, err = DynamicSelectPreview(animeListMapPreview, true) |
| |
| 351 | } else { |
| |
| 352 | anilistSelectedOption, err = DynamicSelect(animeListMap, true) |
| |
| 353 | } |
| |
| 354 | if err != nil { |
| |
| 355 | Log(fmt.Sprintf("Error selecting anime: %v", err), logFile) |
| |
| 356 | ExitCurd(fmt.Errorf("Error selecting anime")) |
| |
| 357 | } |
| |
| 358 | |
| |
| 359 | Log(anilistSelectedOption, logFile) |
| |
| 360 | |
325 | |
| 361 | if anilistSelectedOption.Key == "-1" { |
326 | // Get the anime list data |
| 362 | ExitCurd(nil) |
327 | if userCurdConfig.RofiSelection && userCurdConfig.ImagePreview { |
| 363 | } |
328 | anilistUserDataPreview, err = GetUserDataPreview(user.Token, user.Id) |
| 364 | |
329 | if err != nil { |
| 365 | if anilistSelectedOption.Label == "add_new" || anilistSelectedOption.Key == "add_new" { |
330 | Log(fmt.Sprintf("Failed to get user data preview: %v", err), logFile) |
| 366 | anilistSelectedOption = AddNewAnime(userCurdConfig, anime, user, databaseAnimes, logFile) |
331 | ExitCurd(fmt.Errorf("Failed to get user data preview")) |
| 367 | } |
332 | } |
| 368 | |
333 | user.AnimeList = ParseAnimeList(anilistUserDataPreview) |
| 369 | anime.AnilistId, err = strconv.Atoi(anilistSelectedOption.Key) |
334 | } else { |
| 370 | if err != nil { |
335 | anilistUserData, err = GetUserData(user.Token, user.Id) |
| 371 | Log(fmt.Sprintf("Error converting Anilist ID: %v", err), logFile) |
336 | if err != nil { |
| 372 | ExitCurd(fmt.Errorf("Error converting Anilist ID")) |
337 | Log(fmt.Sprintf("Failed to get user data: %v", err), logFile) |
| 373 | } |
338 | ExitCurd(fmt.Errorf("Failed to get user ID\nYou can reset the token by running `curd -change-token`")) |
| 374 | } |
339 | } |
| 375 | // Find anime in Local history |
340 | user.AnimeList = ParseAnimeList(anilistUserData) |
| 376 | animePointer := LocalFindAnime(*databaseAnimes, anime.AnilistId, "") |
341 | } |
| 377 | |
| |
| 378 | // Get anime entry |
| |
| 379 | selectedAnilistAnime, err := FindAnimeByAnilistID(user.AnimeList, anilistSelectedOption.Key) |
| |
| 380 | if err != nil { |
| |
| 381 | Log(fmt.Sprintf("Can not find the anime in anilist animelist: %v", err), logFile) |
| |
| 382 | ExitCurd(fmt.Errorf("Can not find the anime in anilist animelist")) |
| |
| 383 | } |
| |
| 384 | |
342 | |
| 385 | // Set anime entry |
343 | // Skip category selection if Current flag is set |
| 386 | anime.Title = selectedAnilistAnime.Media.Title |
344 | var categorySelection SelectionOption |
| 387 | anime.TotalEpisodes = selectedAnilistAnime.Media.Episodes |
345 | if userCurdConfig.CurrentCategory { |
| 388 | anime.Ep.Number = selectedAnilistAnime.Progress+1 |
346 | categorySelection = SelectionOption{ |
| 389 | var animeList map[string]string |
347 | Key: "CURRENT", |
| 390 | userQuery = anime.Title.Romaji |
348 | Label: "Currently Watching", |
| 391 | |
349 | } |
| 392 | // if anime not found in database, find it in animeList |
350 | } else { |
| 393 | if animePointer == nil { |
351 | // Create category selection map |
| 394 | Log("Anime not found in database, searching in animeList...", logFile) |
352 | categories := map[string]string{ |
| 395 | // Get Anime list (All anime) |
353 | "CURRENT": "Currently Watching", |
| 396 | Log(fmt.Sprintf("Searching for anime with query: %s, SubOrDub: %s", userQuery, userCurdConfig.SubOrDub), logFile) |
354 | "PAUSED": "On Hold", |
| 397 | |
355 | "PLANNING": "Plan to Watch", |
| 398 | animeList, err = SearchAnime(string(userQuery), userCurdConfig.SubOrDub) |
356 | "COMPLETED": "Completed", |
| 399 | Log(fmt.Sprintf("SearchAnime result - animeList: %+v, err: %v", animeList, err), logFile) |
357 | "DROPPED": "Dropped", |
| 400 | if err != nil { |
358 | } |
| 401 | Log(fmt.Sprintf("Failed to select anime: %v", err), logFile) |
359 | |
| 402 | ExitCurd(fmt.Errorf("Failed to select anime")) |
360 | // Select category using DynamicSelect |
| 403 | } |
361 | var err error |
| 404 | if len(animeList) == 0 { |
362 | categorySelection, err = DynamicSelect(categories, false) |
| 405 | ExitCurd(fmt.Errorf("No results found.")) |
363 | if err != nil { |
| 406 | } |
364 | Log(fmt.Sprintf("Failed to select category: %v", err), logFile) |
| |
365 | ExitCurd(fmt.Errorf("Failed to select category")) |
| |
366 | } |
| |
367 | |
| |
368 | if categorySelection.Key == "-1" { |
| |
369 | ExitCurd(nil) |
| |
370 | } |
| |
371 | ClearScreen() |
| |
372 | } |
| 407 | |
373 | |
| 408 | // find anime in animeList |
374 | // Filter anime list based on selected category |
| 409 | anime.AllanimeId, err = FindKeyByValue(animeList, fmt.Sprintf("%v (%d episodes)", userQuery, selectedAnilistAnime.Media.Episodes)) |
375 | var animeListMap map[string]string |
| 410 | if err != nil { |
376 | var animeListMapPreview map[string]RofiSelectPreview |
| 411 | Log(fmt.Sprintf("Failed to find anime in animeList: %v", err), logFile) |
377 | |
| 412 | } |
378 | if userCurdConfig.RofiSelection && userCurdConfig.ImagePreview { |
| |
379 | animeListMapPreview = make(map[string]RofiSelectPreview) |
| |
380 | for _, entry := range getEntriesByCategory(user.AnimeList, categorySelection.Key) { |
| |
381 | title := entry.Media.Title.English |
| |
382 | if title == "" { |
| |
383 | title = entry.Media.Title.Romaji |
| |
384 | } |
| |
385 | animeListMapPreview[strconv.Itoa(entry.Media.ID)] = RofiSelectPreview{ |
| |
386 | Title: title, |
| |
387 | CoverImage: entry.CoverImage, |
| |
388 | } |
| |
389 | } |
| |
390 | } else { |
| |
391 | animeListMap = make(map[string]string) |
| |
392 | for _, entry := range getEntriesByCategory(user.AnimeList, categorySelection.Key) { |
| |
393 | title := entry.Media.Title.English |
| |
394 | if title == "" { |
| |
395 | title = entry.Media.Title.Romaji |
| |
396 | } |
| |
397 | animeListMap[strconv.Itoa(entry.Media.ID)] = title |
| |
398 | } |
| |
399 | } |
| 413 | |
400 | |
| 414 | // If unable to get Allanime id automatically get manually |
401 | var anilistSelectedOption SelectionOption |
| 415 | if anime.AllanimeId == "" { |
402 | var selectedAllanimeAnime SelectionOption |
| 416 | CurdOut("Failed to automatically select anime") |
403 | var userQuery string |
| 417 | selectedAllanimeAnime, err := DynamicSelect(animeList, false) |
404 | |
| |
405 | if anime.Ep.ContinueLast { |
| |
406 | // Get the last watched anime ID from the curd_id file |
| |
407 | curdIDPath := filepath.Join(os.ExpandEnv(userCurdConfig.StoragePath), "curd_id") |
| |
408 | curdIDBytes, err := os.ReadFile(curdIDPath) |
| |
409 | if err != nil { |
| |
410 | Log(fmt.Sprintf("Error reading curd_id file: %v", err), logFile) |
| |
411 | ExitCurd(fmt.Errorf("Error reading curd_id file")) |
| |
412 | } |
| |
413 | |
| |
414 | lastWatchedID, err := strconv.Atoi(strings.TrimSpace(string(curdIDBytes))) |
| |
415 | if err != nil { |
| |
416 | Log(fmt.Sprintf("Error converting curd_id to integer: %v", err), logFile) |
| |
417 | ExitCurd(fmt.Errorf("Error converting curd_id to integer")) |
| |
418 | } |
| |
419 | |
| |
420 | anime.AnilistId = lastWatchedID |
| |
421 | anilistSelectedOption.Key = strconv.Itoa(lastWatchedID) |
| |
422 | } else { |
| |
423 | // Select anime to watch (Anilist) |
| |
424 | var err error |
| |
425 | if userCurdConfig.RofiSelection && userCurdConfig.ImagePreview { |
| |
426 | anilistSelectedOption, err = DynamicSelectPreview(animeListMapPreview, true) |
| |
427 | } else { |
| |
428 | anilistSelectedOption, err = DynamicSelect(animeListMap, true) |
| |
429 | } |
| |
430 | if err != nil { |
| |
431 | Log(fmt.Sprintf("Error selecting anime: %v", err), logFile) |
| |
432 | ExitCurd(fmt.Errorf("Error selecting anime")) |
| |
433 | } |
| |
434 | |
| |
435 | Log(anilistSelectedOption, logFile) |
| |
436 | |
| |
437 | if anilistSelectedOption.Key == "-1" { |
| |
438 | ExitCurd(nil) |
| |
439 | } |
| |
440 | |
| |
441 | if anilistSelectedOption.Label == "add_new" || anilistSelectedOption.Key == "add_new" { |
| |
442 | anilistSelectedOption = AddNewAnime(userCurdConfig, anime, user, databaseAnimes, logFile) |
| |
443 | } |
| |
444 | |
| |
445 | anime.AnilistId, err = strconv.Atoi(anilistSelectedOption.Key) |
| |
446 | if err != nil { |
| |
447 | Log(fmt.Sprintf("Error converting Anilist ID: %v", err), logFile) |
| |
448 | ExitCurd(fmt.Errorf("Error converting Anilist ID")) |
| |
449 | } |
| |
450 | } |
| |
451 | // Find anime in Local history |
| |
452 | animePointer := LocalFindAnime(*databaseAnimes, anime.AnilistId, "") |
| |
453 | |
| |
454 | // Get anime entry |
| |
455 | selectedAnilistAnime, err := FindAnimeByAnilistID(user.AnimeList, anilistSelectedOption.Key) |
| |
456 | if err != nil { |
| |
457 | Log(fmt.Sprintf("Can not find the anime in anilist animelist: %v", err), logFile) |
| |
458 | ExitCurd(fmt.Errorf("Can not find the anime in anilist animelist")) |
| |
459 | } |
| 418 | |
460 | |
| 419 | if err != nil { |
461 | // Set anime entry |
| 420 | // fmt.Println("No anime available") |
462 | anime.Title = selectedAnilistAnime.Media.Title |
| 421 | ExitCurd(fmt.Errorf("No anime available")) |
463 | anime.TotalEpisodes = selectedAnilistAnime.Media.Episodes |
| 422 | } |
464 | anime.Ep.Number = selectedAnilistAnime.Progress+1 |
| 423 | anime.AllanimeId = selectedAllanimeAnime.Key |
465 | var animeList map[string]string |
| 424 | } |
466 | userQuery = anime.Title.Romaji |
| 425 | } else { |
467 | |
| 426 | // if anime found in database, use it |
468 | // if anime not found in database, find it in animeList |
| 427 | anime.AllanimeId = animePointer.AllanimeId |
469 | if animePointer == nil { |
| 428 | anime.Ep.Player.PlaybackTime = animePointer.Ep.Player.PlaybackTime |
470 | Log("Anime not found in database, searching in animeList...", logFile) |
| 429 | anime.Ep.Resume = true |
471 | // Get Anime list (All anime) |
| 430 | anime.Ep.Number = animePointer.Ep.Number |
472 | Log(fmt.Sprintf("Searching for anime with query: %s, SubOrDub: %s", userQuery, userCurdConfig.SubOrDub), logFile) |
| 431 | } |
473 | |
| |
474 | animeList, err = SearchAnime(string(userQuery), userCurdConfig.SubOrDub) |
| |
475 | Log(fmt.Sprintf("SearchAnime result - animeList: %+v, err: %v", animeList, err), logFile) |
| |
476 | if err != nil { |
| |
477 | Log(fmt.Sprintf("Failed to select anime: %v", err), logFile) |
| |
478 | ExitCurd(fmt.Errorf("Failed to select anime")) |
| |
479 | } |
| |
480 | if len(animeList) == 0 { |
| |
481 | ExitCurd(fmt.Errorf("No results found.")) |
| |
482 | } |
| |
483 | |
| |
484 | // find anime in animeList |
| |
485 | anime.AllanimeId, err = FindKeyByValue(animeList, fmt.Sprintf("%v (%d episodes)", userQuery, selectedAnilistAnime.Media.Episodes)) |
| |
486 | if err != nil { |
| |
487 | Log(fmt.Sprintf("Failed to find anime in animeList: %v", err), logFile) |
| |
488 | } |
| |
489 | |
| |
490 | // If unable to get Allanime id automatically get manually |
| |
491 | if anime.AllanimeId == "" { |
| |
492 | CurdOut("Failed to automatically select anime") |
| |
493 | selectedAllanimeAnime, err := DynamicSelect(animeList, false) |
| |
494 | |
| |
495 | if err != nil { |
| |
496 | // fmt.Println("No anime available") |
| |
497 | ExitCurd(fmt.Errorf("No anime available")) |
| |
498 | } |
| |
499 | anime.AllanimeId = selectedAllanimeAnime.Key |
| |
500 | } |
| |
501 | } else { |
| |
502 | // if anime found in database, use it |
| |
503 | anime.AllanimeId = animePointer.AllanimeId |
| |
504 | anime.Ep.Player.PlaybackTime = animePointer.Ep.Player.PlaybackTime |
| |
505 | anime.Ep.Resume = true |
| |
506 | anime.Ep.Number = animePointer.Ep.Number |
| |
507 | } |
| 432 | |
508 | |
| 433 | if selectedAllanimeAnime.Key == "-1" { |
509 | if selectedAllanimeAnime.Key == "-1" { |
| 434 | ExitCurd(nil) |
510 | ExitCurd(nil) |
| 435 | } |
511 | } |
| 436 | |
512 | |
| 437 | // If upstream is ahead, update the episode number |
513 | // If upstream is ahead, update the episode number |
| 438 | if temp_anime, err := FindAnimeByAnilistID(user.AnimeList, strconv.Itoa(anime.AnilistId)); err == nil { |
514 | if temp_anime, err := FindAnimeByAnilistID(user.AnimeList, strconv.Itoa(anime.AnilistId)); err == nil { |
| 439 | if temp_anime.Progress > anime.Ep.Number { |
515 | if temp_anime.Progress > anime.Ep.Number { |
| 440 | anime.Ep.Number = temp_anime.Progress |
516 | anime.Ep.Number = temp_anime.Progress |
| 441 | anime.Ep.Player.PlaybackTime = 0 |
517 | anime.Ep.Player.PlaybackTime = 0 |
| 442 | anime.Ep.Resume = false |
518 | anime.Ep.Resume = false |
| 443 | } |
519 | } |
| 444 | } |
520 | } |
| 445 | |
521 | |
| 446 | if anime.TotalEpisodes == 0 { |
522 | if anime.TotalEpisodes == 0 { |
| 447 | // Get updated anime data |
523 | // Get updated anime data |
| 448 | Log(selectedAllanimeAnime, logFile) |
524 | Log(selectedAllanimeAnime, logFile) |
| 449 | updatedAnime, err := GetAnimeDataByID(anime.AnilistId, user.Token) |
525 | updatedAnime, err := GetAnimeDataByID(anime.AnilistId, user.Token) |
| 450 | Log(updatedAnime, logFile) |
526 | Log(updatedAnime, logFile) |
| 451 | if err != nil { |
527 | if err != nil { |
| 452 | Log(fmt.Sprintf("Error getting updated anime data: %v", err), logFile) |
528 | Log(fmt.Sprintf("Error getting updated anime data: %v", err), logFile) |
| 453 | } else { |
529 | } else { |
| 454 | anime.TotalEpisodes = updatedAnime.TotalEpisodes |
530 | anime.TotalEpisodes = updatedAnime.TotalEpisodes |
| 455 | Log(fmt.Sprintf("Updated total episodes: %d", anime.TotalEpisodes), logFile) |
531 | Log(fmt.Sprintf("Updated total episodes: %d", anime.TotalEpisodes), logFile) |
| 456 | } |
532 | } |
| 457 | } |
533 | } |
| 458 | |
534 | |
| 459 | if anime.TotalEpisodes == 0 { // If failed to get anime data |
535 | if anime.TotalEpisodes == 0 { // If failed to get anime data |
| 460 | CurdOut("Failed to get anime data. Attempting to retrieve from anime list.") |
536 | CurdOut("Failed to get anime data. Attempting to retrieve from anime list.") |
| 461 | animeList, err := SearchAnime(string(userQuery), userCurdConfig.SubOrDub) |
537 | animeList, err := SearchAnime(string(userQuery), userCurdConfig.SubOrDub) |
| 462 | if err != nil { |
538 | if err != nil { |
| 463 | CurdOut(fmt.Sprintf("Failed to retrieve anime list: %v", err)) |
539 | CurdOut(fmt.Sprintf("Failed to retrieve anime list: %v", err)) |
| 464 | } else { |
540 | } else { |
| 465 | for allanimeId, label := range animeList { |
541 | for allanimeId, label := range animeList { |
| 466 | if allanimeId == anime.AllanimeId { |
542 | if allanimeId == anime.AllanimeId { |
| 467 | // Extract total episodes from the label |
543 | // Extract total episodes from the label |
| 468 | if matches := regexp.MustCompile(`\((\d+) episodes\)`).FindStringSubmatch(label); len(matches) > 1 { |
544 | if matches := regexp.MustCompile(`\((\d+) episodes\)`).FindStringSubmatch(label); len(matches) > 1 { |
| 469 | anime.TotalEpisodes, _ = strconv.Atoi(matches[1]) |
545 | anime.TotalEpisodes, _ = strconv.Atoi(matches[1]) |
| 470 | CurdOut(fmt.Sprintf("Retrieved total episodes: %d", anime.TotalEpisodes)) |
546 | CurdOut(fmt.Sprintf("Retrieved total episodes: %d", anime.TotalEpisodes)) |
| 471 | break |
547 | break |
| 472 | } |
548 | } |
| 473 | } |
549 | } |
| 474 | } |
550 | } |
| 475 | } |
551 | } |
| 476 | |
552 | |
| 477 | if anime.TotalEpisodes == 0 { |
553 | if anime.TotalEpisodes == 0 { |
| 478 | CurdOut("Still unable to determine total episodes.") |
554 | CurdOut("Still unable to determine total episodes.") |
| 479 | CurdOut(fmt.Sprintf("Your AniList progress: %d", selectedAnilistAnime.Progress)) |
555 | CurdOut(fmt.Sprintf("Your AniList progress: %d", selectedAnilistAnime.Progress)) |
| 480 | var episodeNumber int |
556 | var episodeNumber int |
| 481 | if userCurdConfig.RofiSelection { |
557 | if userCurdConfig.RofiSelection { |
| 482 | userInput, err := GetUserInputFromRofi("Enter the episode you want to start from") |
558 | userInput, err := GetUserInputFromRofi("Enter the episode you want to start from") |
| 483 | if err != nil { |
559 | if err != nil { |
| 484 | Log("Error getting user input: "+err.Error(), logFile) |
560 | Log("Error getting user input: "+err.Error(), logFile) |
| 485 | ExitCurd(fmt.Errorf("Error getting user input: "+err.Error())) |
561 | ExitCurd(fmt.Errorf("Error getting user input: "+err.Error())) |
| 486 | } |
562 | } |
| 487 | episodeNumber, err = strconv.Atoi(userInput) |
563 | episodeNumber, err = strconv.Atoi(userInput) |
| 488 | } else { |
564 | } else { |
| 489 | fmt.Print("Enter the episode you want to start from: ") |
565 | fmt.Print("Enter the episode you want to start from: ") |
| 490 | fmt.Scanln(&episodeNumber) |
566 | fmt.Scanln(&episodeNumber) |
| 491 | } |
567 | } |
| 492 | anime.Ep.Number = episodeNumber |
568 | anime.Ep.Number = episodeNumber |
| 493 | } else { |
569 | } else { |
| 494 | anime.Ep.Number = selectedAnilistAnime.Progress + 1 |
570 | anime.Ep.Number = selectedAnilistAnime.Progress + 1 |
| 495 | } |
571 | } |
| 496 | } else if anime.TotalEpisodes < anime.Ep.Number { // Handle weird cases |
572 | } else if anime.TotalEpisodes < anime.Ep.Number { // Handle weird cases |
| 497 | Log(fmt.Sprintf("Weird case: anime.TotalEpisodes < anime.Ep.Number: %v < %v", anime.TotalEpisodes, anime.Ep.Number), logFile) |
573 | Log(fmt.Sprintf("Weird case: anime.TotalEpisodes < anime.Ep.Number: %v < %v", anime.TotalEpisodes, anime.Ep.Number), logFile) |
| 498 | var answer string |
574 | var answer string |
| 499 | if userCurdConfig.RofiSelection { |
575 | if userCurdConfig.RofiSelection { |
| 500 | userInput, err := GetUserInputFromRofi("Would like to start the anime from beginning? (y/n)") |
576 | userInput, err := GetUserInputFromRofi("Would like to start the anime from beginning? (y/n)") |
| 501 | if err != nil { |
577 | if err != nil { |
| 502 | Log("Error getting user input: "+err.Error(), logFile) |
578 | Log("Error getting user input: "+err.Error(), logFile) |
| 503 | ExitCurd(fmt.Errorf("Error getting user input: "+err.Error())) |
579 | ExitCurd(fmt.Errorf("Error getting user input: "+err.Error())) |
| 504 | } |
580 | } |
| 505 | answer = userInput |
581 | answer = userInput |
| 506 | } else { |
582 | } else { |
| 507 | fmt.Printf("Would like to start the anime from beginning? (y/n)\n") |
583 | fmt.Printf("Would like to start the anime from beginning? (y/n)\n") |
| 508 | fmt.Scanln(&answer) |
584 | fmt.Scanln(&answer) |
| 509 | } |
585 | } |
| 510 | if answer == "y" { |
586 | if answer == "y" { |
| 511 | anime.Ep.Number = 1 |
587 | anime.Ep.Number = 1 |
| 512 | } else { |
588 | } else { |
| 513 | anime.Ep.Number = anime.TotalEpisodes |
589 | anime.Ep.Number = anime.TotalEpisodes |
| 514 | } |
590 | } |
| 515 | } |
591 | } |
| 516 | |
592 | |
| 517 | } |
593 | } |
| 518 | |
594 | |
| @@ -585,6 +661,30 @@ func StartCurd(userCurdConfig *CurdConfig, anime *Anime, logFile string) string |
| 585 | } |
661 | } |
| 586 | } |
662 | } |
| 587 | |
663 | |
| |
664 | // Display starting message with cover image and episode info |
| |
665 | if anime.CoverImage != "" && userCurdConfig.ImagePreview && userCurdConfig.RofiSelection { |
| |
666 | // Get the cached image path |
| |
667 | cacheDir := os.ExpandEnv("${HOME}/.cache/curd/images") |
| |
668 | filename := fmt.Sprintf("%x.jpg", md5.Sum([]byte(anime.CoverImage))) |
| |
669 | cachePath := filepath.Join(cacheDir, filename) |
| |
670 | |
| |
671 | // Display the image if it exists in cache |
| |
672 | _, err := os.Stat(cachePath) |
| |
673 | if err == nil { |
| |
674 | // File exists |
| |
675 | Log(fmt.Sprintf("Image found at %s", cachePath), logFile) |
| |
676 | CurdOut(fmt.Sprintf("-i %s \"%s - Episode %d\"", cachePath, GetAnimeName(*anime), anime.Ep.Number)) |
| |
677 | } else { |
| |
678 | // File does not exist |
| |
679 | Log(fmt.Sprintf("Image does not exist at %s", cachePath), logFile) |
| |
680 | CurdOut(fmt.Sprintf("%s - Episode %d", |
| |
681 | GetAnimeName(*anime), |
| |
682 | anime.Ep.Number)) |
| |
683 | |
| |
684 | } |
| |
685 | } else { |
| |
686 | CurdOut(fmt.Sprintf("%s - Episode %d", GetAnimeName(*anime), anime.Ep.Number)) |
| |
687 | } |
| 588 | mpvSocketPath, err := StartVideo(PrioritizeLink(anime.Ep.Links), []string{}) |
688 | mpvSocketPath, err := StartVideo(PrioritizeLink(anime.Ep.Links), []string{}) |
| 589 | |
689 | |
| 590 | if err != nil { |
690 | if err != nil { |
| @@ -637,3 +737,20 @@ func CheckAndDownloadFiles(storagePath string, filesToCheck []string) error { |
| 637 | return nil |
737 | return nil |
| 638 | } |
738 | } |
| 639 | |
739 | |
| |
740 | func getEntriesByCategory(list AnimeList, category string) []Entry { |
| |
741 | switch category { |
| |
742 | case "CURRENT": |
| |
743 | return list.Watching |
| |
744 | case "COMPLETED": |
| |
745 | return list.Completed |
| |
746 | case "PAUSED": |
| |
747 | return list.Paused |
| |
748 | case "DROPPED": |
| |
749 | return list.Dropped |
| |
750 | case "PLANNING": |
| |
751 | return list.Planning |
| |
752 | default: |
| |
753 | return []Entry{} |
| |
754 | } |
| |
755 | } |
| |
756 | |