Commit f9505924b1

f9505924b1ab2df89eff0187a9d80cc96f6e050a

parent: 34f5709242

Unsigned

Wraient <wraient.911@gmail.com> · 2025-07-23 18:14 UTC

Fix: Handle last episode scoring and completion

Layout: unified · split

internal/curd.go +108
@@ -1256,6 +1256,20 @@ func getEntriesByCategory(list AnimeList, category string) []Entry {
12561256
12571257func NextEpisodePrompt(userCurdConfig *CurdConfig) {
12581258 anime := GetGlobalAnime()
1259
1260 // Check if we just completed the last episode
1261 if anime.TotalEpisodes > 0 && anime.Ep.Number == anime.TotalEpisodes {
1262 // Handle scoring and completion for the last episode
1263 HandleLastEpisodeCompletion(userCurdConfig, anime)
1264
1265 // Exit after completing the series
1266 fmt.Print("\r\033[K") // Carriage return and clear line
1267 ExitMPV(anime.Ep.Player.SocketPath)
1268 CurdOut("Series completed!")
1269 ExitCurd(nil)
1270 return
1271 }
1272
12591273 // If in CLI mode (not Rofi), we need to show the next episode number
12601274 episodeNum := anime.Ep.Number
12611275 if !userCurdConfig.RofiSelection {
@@ -1389,6 +1403,35 @@ func StartNextEpisode(anime *Anime, userCurdConfig *CurdConfig, databaseFile str
13891403 // Save previous episode number for progress update
13901404 prevEpisode := anime.Ep.Number
13911405
1406 // Check if we just completed the last episode
1407 if anime.TotalEpisodes > 0 && anime.Ep.Number == anime.TotalEpisodes {
1408 // Handle scoring and completion for the last episode
1409 HandleLastEpisodeCompletion(userCurdConfig, anime)
1410
1411 // Update Anilist progress for the last episode if not rewatching
1412 if !anime.Rewatching {
1413 user := &User{}
1414 var err error
1415 user.Token, err = GetTokenFromFile(filepath.Join(os.ExpandEnv(userCurdConfig.StoragePath), "token"))
1416 if err != nil {
1417 Log("Error reading token for progress update: " + err.Error())
1418 } else {
1419 go func() {
1420 err = UpdateAnimeProgress(user.Token, anime.AnilistId, prevEpisode)
1421 if err != nil {
1422 Log("Error updating Anilist progress: " + err.Error())
1423 } else {
1424 CurdOut(fmt.Sprintf("Anime progress updated! Latest watched episode: %d", prevEpisode))
1425 }
1426 }()
1427 }
1428 }
1429
1430 CurdOut("Series completed!")
1431 ExitCurd(nil)
1432 return
1433 }
1434
13921435 // Increment episode number
13931436 anime.Ep.Number++
13941437
@@ -1457,3 +1500,68 @@ func StartNextEpisode(anime *Anime, userCurdConfig *CurdConfig, databaseFile str
14571500 // Output message to user
14581501 CurdOut(fmt.Sprint("Starting next episode: ", anime.Ep.Number))
14591502}
1503
1504// HandleLastEpisodeCompletion handles scoring and completion for the last episode
1505func HandleLastEpisodeCompletion(userCurdConfig *CurdConfig, anime *Anime) {
1506 // Check if this is the last episode and scoring is enabled
1507 if userCurdConfig.ScoreOnCompletion && anime.TotalEpisodes > 0 && anime.Ep.Number == anime.TotalEpisodes {
1508 // Get user token for scoring
1509 user := &User{}
1510
1511 // Get config for storage path
1512 homeDir := ""
1513 if runtime.GOOS == "windows" {
1514 homeDir = os.Getenv("USERPROFILE")
1515 } else {
1516 homeDir = os.Getenv("HOME")
1517 }
1518 configFilePath := filepath.Join(homeDir, ".config", "curd", "curd.conf")
1519 config, err := LoadConfig(configFilePath)
1520 if err != nil {
1521 Log("Error loading config for scoring: " + err.Error())
1522 return
1523 }
1524
1525 user.Token, err = GetTokenFromFile(filepath.Join(os.ExpandEnv(config.StoragePath), "token"))
1526 if err != nil {
1527 Log("Error reading token for scoring: " + err.Error())
1528 return
1529 }
1530
1531 // Prompt user to score the anime
1532 CurdOut("You've completed this anime! Would you like to rate it?")
1533
1534 scoreOptions := []SelectionOption{
1535 {Key: "yes", Label: "Yes, rate this anime"},
1536 {Key: "no", Label: "No, skip rating"},
1537 }
1538
1539 selectedOption, err := DynamicSelect(scoreOptions)
1540 if err != nil {
1541 Log(fmt.Sprintf("Error in score prompt selection: %v", err))
1542 return
1543 }
1544
1545 if selectedOption.Key == "yes" {
1546 err = RateAnime(user.Token, anime.AnilistId)
1547 if err != nil {
1548 Log(fmt.Sprintf("Error rating anime: %v", err))
1549 CurdOut("Failed to rate anime")
1550 } else {
1551 CurdOut("Anime rated successfully!")
1552 }
1553 }
1554
1555 // Update anime status to completed on AniList
1556 if !anime.Rewatching {
1557 go func() {
1558 err = UpdateAnimeStatus(user.Token, anime.AnilistId, "COMPLETED")
1559 if err != nil {
1560 Log("Error updating anime status to completed: " + err.Error())
1561 } else {
1562 CurdOut("Anime status updated to completed!")
1563 }
1564 }()
1565 }
1566 }
1567}