| @@ -1088,39 +1088,6 @@ func SetupCurd(userCurdConfig *CurdConfig, anime *Anime, user *User, databaseAni |
| 1088 | 1088 | anime.Ep.Number = anime.TotalEpisodes |
| 1089 | 1089 | } |
| 1090 | 1090 | } |
| 1091 | | |
| 1092 | | // Check if this is the last episode - prompt user if needed |
| 1093 | | if userCurdConfig.NextEpisodePrompt && anime.TotalEpisodes > 0 && anime.Ep.Number == anime.TotalEpisodes && !userCurdConfig.RofiSelection { |
| 1094 | | // Ensure the terminal state is clean for the prompt |
| 1095 | | fmt.Print("\r\033[K") // Carriage return and clear line |
| 1096 | | episodeNum := anime.Ep.Number |
| 1097 | | if !userCurdConfig.RofiSelection { |
| 1098 | | episodeNum++ |
| 1099 | | } |
| 1100 | | |
| 1101 | | CurdOut(fmt.Sprintf("Start next episode (%d)?", episodeNum)) |
| 1102 | | |
| 1103 | | // Create options for the selection |
| 1104 | | options := []SelectionOption{ |
| 1105 | | {Key: "yes", Label: "Yes, start next episode"}, |
| 1106 | | // "no": "No, quit", |
| 1107 | | } |
| 1108 | | |
| 1109 | | // Use DynamicSelect for both CLI and Rofi modes |
| 1110 | | selectedOption, err := DynamicSelect(options) |
| 1111 | | if err != nil { |
| 1112 | | ExitCurd(err) |
| 1113 | | } |
| 1114 | | |
| 1115 | | if selectedOption.Key == "-1" { |
| 1116 | | // User selected to quit |
| 1117 | | fmt.Print("\r\033[K") // Carriage return and clear line |
| 1118 | | ExitMPV(anime.Ep.Player.SocketPath) |
| 1119 | | CurdOut("Exiting without starting next episode") |
| 1120 | | ExitCurd(nil) |
| 1121 | | return |
| 1122 | | } |
| 1123 | | } |
| 1124 | 1091 | } |
| 1125 | 1092 | |
| 1126 | 1093 | // CreateOrWriteTokenFile creates the token file if it doesn't exist and writes the token to it |
| @@ -1338,189 +1305,196 @@ func getEntriesByCategory(list AnimeList, category string) []Entry { |
| 1338 | 1305 | } |
| 1339 | 1306 | } |
| 1340 | 1307 | |
| 1341 | | func NextEpisodePrompt(userCurdConfig *CurdConfig) { |
| 1308 | func NextEpisodePromptCLI(userCurdConfig *CurdConfig) bool { |
| 1342 | 1309 | anime := GetGlobalAnime() |
| 1343 | 1310 | |
| 1344 | | // Check if we just completed the last episode |
| 1345 | | if anime.TotalEpisodes > 0 && anime.Ep.Number == anime.TotalEpisodes { |
| 1346 | | // Handle scoring and completion for the last episode |
| 1347 | | HandleLastEpisodeCompletion(userCurdConfig, anime) |
| 1311 | // Show the next episode number that will be started |
| 1312 | nextEpisodeNum := anime.Ep.Number + 1 |
| 1313 | CurdOut(fmt.Sprintf("Start next episode (%d)?", nextEpisodeNum)) |
| 1348 | 1314 | |
| 1349 | | // Exit after completing the series |
| 1350 | | fmt.Print("\r\033[K") // Carriage return and clear line |
| 1351 | | ExitMPV(anime.Ep.Player.SocketPath) |
| 1352 | | CurdOut("Series completed!") |
| 1353 | | ExitCurd(nil) |
| 1354 | | return |
| 1355 | | } |
| 1356 | | |
| 1357 | | // If in CLI mode (not Rofi), we need to show the next episode number |
| 1358 | | episodeNum := anime.Ep.Number |
| 1359 | | if !userCurdConfig.RofiSelection { |
| 1360 | | episodeNum++ |
| 1361 | | } |
| 1362 | | |
| 1363 | | CurdOut(fmt.Sprintf("Start next episode (%d)?", episodeNum)) |
| 1364 | | |
| 1365 | | // Create options for the selection |
| 1315 | // Create options for the selection - no "quit" option since it's built into selection menu |
| 1366 | 1316 | options := []SelectionOption{ |
| 1367 | | {Key: "yes", Label: "Yes, start next episode"}, |
| 1368 | | // {Key: "-1", Label: "No, quit"}, |
| 1317 | {Key: "yes", Label: fmt.Sprintf("Yes, continue to episode %d", nextEpisodeNum)}, |
| 1369 | 1318 | } |
| 1370 | 1319 | |
| 1371 | | // Use DynamicSelect for both CLI and Rofi modes |
| 1320 | // Use DynamicSelect for CLI mode |
| 1372 | 1321 | selectedOption, err := DynamicSelect(options) |
| 1373 | 1322 | if err != nil { |
| 1374 | | Log(fmt.Sprintf("Error in next episode prompt selection: %v", err)) |
| 1375 | | ExitCurd(err) |
| 1323 | Log(fmt.Sprintf("Error in CLI next episode prompt selection: %v", err)) |
| 1324 | return false |
| 1376 | 1325 | } |
| 1377 | 1326 | |
| 1378 | | Log(fmt.Sprintf("User Selected Key: '%s', Label: '%s'", selectedOption.Key, selectedOption.Label)) |
| 1327 | Log(fmt.Sprintf("CLI User Selected Key: '%s', Label: '%s'", selectedOption.Key, selectedOption.Label)) |
| 1379 | 1328 | |
| 1380 | 1329 | if selectedOption.Key == "-1" { |
| 1381 | | // User selected to quit |
| 1382 | | fmt.Print("\r\033[K") // Carriage return and clear line |
| 1383 | | ExitMPV(anime.Ep.Player.SocketPath) |
| 1384 | | CurdOut("Exiting without starting next episode") |
| 1385 | | ExitCurd(nil) |
| 1386 | | return |
| 1387 | | } |
| 1388 | | |
| 1389 | | if selectedOption.Key != "yes" { |
| 1390 | | // User selected something other than yes (shouldn't happen, but safety check) |
| 1391 | | Log(fmt.Sprintf("Unexpected selection key: %s", selectedOption.Key)) |
| 1392 | | fmt.Print("\r\033[K") // Carriage return and clear line |
| 1393 | | ExitMPV(anime.Ep.Player.SocketPath) |
| 1394 | | CurdOut("Exiting without starting next episode") |
| 1395 | | ExitCurd(nil) |
| 1396 | | return |
| 1330 | // User selected to quit via the built-in quit option |
| 1331 | CurdOut("Exiting") |
| 1332 | return false |
| 1397 | 1333 | } |
| 1398 | 1334 | |
| 1399 | | // If yes, start the next episode |
| 1400 | | prevEpisode := anime.Ep.Number |
| 1401 | | anime.Ep.Number++ |
| 1402 | | anime.Ep.Started = false |
| 1403 | | anime.Ep.IsCompleted = false // Reset completion flag for the new episode |
| 1404 | | |
| 1405 | | // Clear current line to avoid visual glitches |
| 1406 | | fmt.Print("\r\033[K") // Carriage return and clear line |
| 1335 | return selectedOption.Key == "yes" |
| 1336 | } |
| 1407 | 1337 | |
| 1408 | | CurdOut(fmt.Sprint("Starting next episode: ", anime.Ep.Number)) |
| 1338 | // NextEpisodePromptContinuous provides a continuous next episode prompt for CLI mode |
| 1339 | // This runs throughout the episode duration and handles completion logic |
| 1340 | func NextEpisodePromptContinuous(userCurdConfig *CurdConfig, databaseFile string, userToken string) { |
| 1341 | anime := GetGlobalAnime() |
| 1342 | |
| 1343 | for { |
| 1344 | // Check if episode has started |
| 1345 | if !anime.Ep.Started { |
| 1346 | time.Sleep(1 * time.Second) |
| 1347 | continue |
| 1348 | } |
| 1409 | 1349 | |
| 1410 | | // Get database file path for StartNextEpisode |
| 1411 | | homeDir := "" |
| 1412 | | if runtime.GOOS == "windows" { |
| 1413 | | homeDir = os.Getenv("USERPROFILE") |
| 1414 | | } else { |
| 1415 | | homeDir = os.Getenv("HOME") |
| 1416 | | } |
| 1417 | | configFilePath := filepath.Join(homeDir, ".config", "curd", "curd.conf") |
| 1418 | | config, err := LoadConfig(configFilePath) |
| 1419 | | if err != nil { |
| 1420 | | Log("Error loading config for database path: " + err.Error()) |
| 1421 | | ExitCurd(err) |
| 1422 | | } |
| 1423 | | databaseFile := filepath.Join(os.ExpandEnv(config.StoragePath), "curd_history.txt") |
| 1350 | // Show the next episode number that will be started |
| 1351 | nextEpisodeNum := anime.Ep.Number + 1 |
| 1352 | CurdOut(fmt.Sprintf("Continue to next episode (%d) or quit?", nextEpisodeNum)) |
| 1424 | 1353 | |
| 1425 | | // Update local database for the new episode |
| 1426 | | err = LocalUpdateAnime(databaseFile, anime.AnilistId, anime.AllanimeId, anime.Ep.Number, 0, 0, GetAnimeName(*anime)) |
| 1427 | | if err != nil { |
| 1428 | | Log("Error updating local database: " + err.Error()) |
| 1429 | | } |
| 1354 | // Create options for the selection - no "quit" option since it's built into selection menu |
| 1355 | options := []SelectionOption{ |
| 1356 | {Key: "yes", Label: "Yes, start next episode now"}, |
| 1357 | } |
| 1430 | 1358 | |
| 1431 | | // Update Anilist progress for the previous episode if not rewatching |
| 1432 | | if !anime.Rewatching { |
| 1433 | | user := &User{} |
| 1434 | | user.Token, err = GetTokenFromFile(filepath.Join(os.ExpandEnv(config.StoragePath), "token")) |
| 1359 | // Use DynamicSelect for CLI mode |
| 1360 | selectedOption, err := DynamicSelect(options) |
| 1435 | 1361 | if err != nil { |
| 1436 | | Log("Error reading token for progress update: " + err.Error()) |
| 1437 | | } else { |
| 1438 | | go func() { |
| 1439 | | err = UpdateAnimeProgress(user.Token, anime.AnilistId, prevEpisode) |
| 1440 | | if err != nil { |
| 1441 | | Log("Error updating Anilist progress: " + err.Error()) |
| 1442 | | } else { |
| 1443 | | CurdOut(fmt.Sprintf("Anime progress updated! Latest watched episode: %d", prevEpisode)) |
| 1444 | | } |
| 1445 | | }() |
| 1362 | Log(fmt.Sprintf("Error in CLI continuous next episode prompt: %v", err)) |
| 1363 | break |
| 1446 | 1364 | } |
| 1447 | | } |
| 1448 | 1365 | |
| 1449 | | // Close current MPV instance |
| 1450 | | ExitMPV(anime.Ep.Player.SocketPath) |
| 1366 | Log(fmt.Sprintf("CLI Continuous User Selected Key: '%s', Label: '%s'", selectedOption.Key, selectedOption.Label)) |
| 1451 | 1367 | |
| 1452 | | // Check if we have prefetched links for the next episode |
| 1453 | | if anime.Ep.NextEpisode.Number == anime.Ep.Number && len(anime.Ep.NextEpisode.Links) > 0 { |
| 1454 | | Log("Using prefetched next episode link") |
| 1455 | | anime.Ep.Links = anime.Ep.NextEpisode.Links |
| 1456 | | } else { |
| 1457 | | // No prefetched links available, need to fetch them |
| 1458 | | CurdOut("Fetching links for next episode...") |
| 1368 | if selectedOption.Key == "-1" { |
| 1369 | // User selected to quit via the built-in quit option |
| 1370 | |
| 1371 | // Check completion percentage |
| 1372 | percentageWatched := PercentageWatched(anime.Ep.Player.PlaybackTime, anime.Ep.Duration) |
| 1373 | |
| 1374 | if int(percentageWatched) >= userCurdConfig.PercentageToMarkComplete { |
| 1375 | // Episode is considered completed, mark it and update progress |
| 1376 | anime.Ep.IsCompleted = true |
| 1377 | |
| 1378 | // Update local database |
| 1379 | err = LocalUpdateAnime(databaseFile, anime.AnilistId, anime.AllanimeId, anime.Ep.Number, anime.Ep.Player.PlaybackTime, ConvertSecondsToMinutes(anime.Ep.Duration), GetAnimeName(*anime)) |
| 1380 | if err != nil { |
| 1381 | Log("Error updating local database on quit: " + err.Error()) |
| 1382 | } |
| 1459 | 1383 | |
| 1460 | | // Wait for up to 5 seconds for prefetched links to become available |
| 1461 | | for i := 0; i < 5; i++ { |
| 1462 | | if anime.Ep.NextEpisode.Number == anime.Ep.Number && len(anime.Ep.NextEpisode.Links) > 0 { |
| 1463 | | CurdOut("Using newly fetched next episode link") |
| 1464 | | anime.Ep.Links = anime.Ep.NextEpisode.Links |
| 1465 | | break |
| 1384 | // Update Anilist progress if not rewatching |
| 1385 | if !anime.Rewatching { |
| 1386 | go func() { |
| 1387 | err = UpdateAnimeProgress(userToken, anime.AnilistId, anime.Ep.Number) |
| 1388 | if err != nil { |
| 1389 | Log("Error updating Anilist progress on quit: " + err.Error()) |
| 1390 | } else { |
| 1391 | CurdOut(fmt.Sprintf("Episode marked as completed! Progress updated: %d", anime.Ep.Number)) |
| 1392 | } |
| 1393 | }() |
| 1394 | } |
| 1395 | |
| 1396 | CurdOut(fmt.Sprintf("Episode completed (%.1f%% watched). Exiting.", percentageWatched)) |
| 1397 | } else { |
| 1398 | CurdOut(fmt.Sprintf("Episode not completed (%.1f%% watched). Exiting.", percentageWatched)) |
| 1466 | 1399 | } |
| 1467 | | time.Sleep(1 * time.Second) |
| 1400 | |
| 1401 | ExitMPV(anime.Ep.Player.SocketPath) |
| 1402 | ExitCurd(nil) |
| 1403 | return |
| 1468 | 1404 | } |
| 1469 | 1405 | |
| 1470 | | // If we still don't have links, get them now |
| 1471 | | if len(anime.Ep.Links) == 0 { |
| 1472 | | links, err := GetEpisodeURL(*userCurdConfig, anime.AllanimeId, anime.Ep.Number) |
| 1406 | if selectedOption.Key == "yes" { |
| 1407 | // User wants to start next episode immediately |
| 1408 | anime.Ep.IsCompleted = true |
| 1409 | |
| 1410 | // Update database with completed episode first |
| 1411 | err = LocalUpdateAnime(databaseFile, anime.AnilistId, anime.AllanimeId, anime.Ep.Number, anime.Ep.Player.PlaybackTime, ConvertSecondsToMinutes(anime.Ep.Duration), GetAnimeName(*anime)) |
| 1473 | 1412 | if err != nil { |
| 1474 | | Log("Failed to get episode links: " + err.Error()) |
| 1475 | | CurdOut("Failed to get episode links. Try again later.") |
| 1476 | | ExitCurd(fmt.Errorf("failed to get episode links: %v", err)) |
| 1477 | | return |
| 1413 | Log("Error updating local database with completed episode: " + err.Error()) |
| 1478 | 1414 | } |
| 1479 | | anime.Ep.Links = links |
| 1415 | |
| 1416 | // Update Anilist progress for the completed episode if not rewatching |
| 1417 | if !anime.Rewatching { |
| 1418 | go func() { |
| 1419 | err = UpdateAnimeProgress(userToken, anime.AnilistId, anime.Ep.Number) |
| 1420 | if err != nil { |
| 1421 | Log("Error updating Anilist progress: " + err.Error()) |
| 1422 | } else { |
| 1423 | CurdOut(fmt.Sprintf("Episode completed! Progress updated: %d", anime.Ep.Number)) |
| 1424 | } |
| 1425 | }() |
| 1426 | } |
| 1427 | |
| 1428 | // Increment to next episode and update database with next episode number and 0 playback time |
| 1429 | anime.Ep.Number++ |
| 1430 | |
| 1431 | // Use prefetched links if available for the next episode |
| 1432 | if (anime.Ep.NextEpisode.Number == anime.Ep.Number) && (len(anime.Ep.NextEpisode.Links) > 0) { |
| 1433 | anime.Ep.Links = anime.Ep.NextEpisode.Links |
| 1434 | Log(fmt.Sprintf("Using prefetched links for episode %d", anime.Ep.Number)) |
| 1435 | } else { |
| 1436 | // Clear links to force fetching new ones |
| 1437 | anime.Ep.Links = []string{} |
| 1438 | Log(fmt.Sprintf("No prefetched links available for episode %d, will fetch new ones", anime.Ep.Number)) |
| 1439 | } |
| 1440 | |
| 1441 | err = LocalUpdateAnime(databaseFile, anime.AnilistId, anime.AllanimeId, anime.Ep.Number, 0, 0, GetAnimeName(*anime)) |
| 1442 | if err != nil { |
| 1443 | Log("Error updating local database with next episode: " + err.Error()) |
| 1444 | } |
| 1445 | |
| 1446 | CurdOut("Starting next episode now...") |
| 1447 | ExitMPV(anime.Ep.Player.SocketPath) |
| 1448 | return // Exit this function, let the main loop handle next episode |
| 1480 | 1449 | } |
| 1481 | 1450 | } |
| 1451 | } |
| 1482 | 1452 | |
| 1483 | | // Verify that we have links before starting |
| 1484 | | if len(anime.Ep.Links) == 0 { |
| 1485 | | CurdOut("No episode links found. Try again later.") |
| 1486 | | ExitCurd(fmt.Errorf("no episode links found")) |
| 1487 | | return |
| 1453 | // Simple next episode prompt for Rofi mode - just asks if user wants to continue |
| 1454 | func NextEpisodePromptRofi(userCurdConfig *CurdConfig) bool { |
| 1455 | anime := GetGlobalAnime() |
| 1456 | |
| 1457 | // Show the next episode number that will be started |
| 1458 | nextEpisodeNum := anime.Ep.Number + 1 |
| 1459 | |
| 1460 | // Create options for the selection |
| 1461 | options := []SelectionOption{ |
| 1462 | {Key: "yes", Label: fmt.Sprintf("Yes, start episode %d", nextEpisodeNum)}, |
| 1463 | } |
| 1464 | |
| 1465 | // Use DynamicSelect for Rofi mode |
| 1466 | selectedOption, err := DynamicSelect(options) |
| 1467 | if err != nil { |
| 1468 | Log(fmt.Sprintf("Error in next episode prompt selection: %v", err)) |
| 1469 | return false |
| 1488 | 1470 | } |
| 1489 | 1471 | |
| 1490 | | // Actually start the next episode |
| 1491 | | anime.Ep.Player.SocketPath = StartCurd(userCurdConfig, anime) |
| 1472 | Log(fmt.Sprintf("Rofi User Selected Key: '%s', Label: '%s'", selectedOption.Key, selectedOption.Label)) |
| 1492 | 1473 | |
| 1493 | | // We don't exit, instead we let the main loop continue with the new episode |
| 1474 | return selectedOption.Key == "yes" |
| 1494 | 1475 | } |
| 1495 | 1476 | |
| 1496 | 1477 | // StartNextEpisode handles the logic for starting the next episode |
| 1497 | 1478 | // It updates the episode number, resets necessary flags, and handles database updates |
| 1498 | | func StartNextEpisode(anime *Anime, userCurdConfig *CurdConfig, databaseFile string) { |
| 1479 | func StartNextEpisode(anime *Anime, userCurdConfig *CurdConfig, databaseFile string, userToken string) { |
| 1499 | 1480 | // Save previous episode number for progress update |
| 1500 | 1481 | prevEpisode := anime.Ep.Number |
| 1501 | 1482 | |
| 1502 | 1483 | // Check if we just completed the last episode |
| 1503 | 1484 | if anime.TotalEpisodes > 0 && anime.Ep.Number == anime.TotalEpisodes { |
| 1504 | 1485 | // Handle scoring and completion for the last episode |
| 1505 | | HandleLastEpisodeCompletion(userCurdConfig, anime) |
| 1486 | HandleLastEpisodeCompletion(userCurdConfig, anime, userToken) |
| 1506 | 1487 | |
| 1507 | 1488 | // Update Anilist progress for the last episode if not rewatching |
| 1508 | 1489 | if !anime.Rewatching { |
| 1509 | | user := &User{} |
| 1510 | | var err error |
| 1511 | | user.Token, err = GetTokenFromFile(filepath.Join(os.ExpandEnv(userCurdConfig.StoragePath), "token")) |
| 1512 | | if err != nil { |
| 1513 | | Log("Error reading token for progress update: " + err.Error()) |
| 1514 | | } else { |
| 1515 | | go func() { |
| 1516 | | err = UpdateAnimeProgress(user.Token, anime.AnilistId, prevEpisode) |
| 1517 | | if err != nil { |
| 1518 | | Log("Error updating Anilist progress: " + err.Error()) |
| 1519 | | } else { |
| 1520 | | CurdOut(fmt.Sprintf("Anime progress updated! Latest watched episode: %d", prevEpisode)) |
| 1521 | | } |
| 1522 | | }() |
| 1523 | | } |
| 1490 | go func() { |
| 1491 | err := UpdateAnimeProgress(userToken, anime.AnilistId, prevEpisode) |
| 1492 | if err != nil { |
| 1493 | Log("Error updating Anilist progress: " + err.Error()) |
| 1494 | } else { |
| 1495 | CurdOut(fmt.Sprintf("Anime progress updated! Latest watched episode: %d", prevEpisode)) |
| 1496 | } |
| 1497 | }() |
| 1524 | 1498 | } |
| 1525 | 1499 | |
| 1526 | 1500 | CurdOut("Series completed!") |
| @@ -1538,28 +1512,14 @@ func StartNextEpisode(anime *Anime, userCurdConfig *CurdConfig, databaseFile str |
| 1538 | 1512 | return |
| 1539 | 1513 | } |
| 1540 | 1514 | |
| 1541 | | // Check if this is the last episode - prompt user if needed |
| 1542 | | if userCurdConfig.NextEpisodePrompt && anime.TotalEpisodes > 0 && anime.Ep.Number == anime.TotalEpisodes && !userCurdConfig.RofiSelection { |
| 1543 | | // Ensure the terminal state is clean for the prompt |
| 1544 | | fmt.Print("\r\033[K") // Carriage return and clear line |
| 1545 | | CurdOut(fmt.Sprintf("Last episode (%d). Are you sure you want to watch it?", anime.Ep.Number)) |
| 1546 | | |
| 1547 | | // Simple yes/no selection for the last episode |
| 1548 | | selectedOption, err := DynamicSelect([]SelectionOption{{Key: "yes", Label: "Yes"}}) |
| 1549 | | if err != nil { |
| 1550 | | ExitCurd(err) |
| 1551 | | } |
| 1552 | | |
| 1553 | | if selectedOption.Key != "yes" { |
| 1554 | | // User selected to quitd |
| 1555 | | fmt.Print("\r\033[K") // Carriage return and clear line |
| 1556 | | CurdOut("Exiting without watching the last episode.") |
| 1557 | | ExitCurd(nil) |
| 1558 | | return |
| 1559 | | } |
| 1560 | | |
| 1561 | | // User chose to continue to the last episode |
| 1562 | | fmt.Print("\r\033[K") // Carriage return and clear line |
| 1515 | // Use prefetched links if available for the next episode |
| 1516 | if (anime.Ep.NextEpisode.Number == anime.Ep.Number) && (len(anime.Ep.NextEpisode.Links) > 0) { |
| 1517 | anime.Ep.Links = anime.Ep.NextEpisode.Links |
| 1518 | Log(fmt.Sprintf("Using prefetched links for episode %d", anime.Ep.Number)) |
| 1519 | } else { |
| 1520 | // Clear links to force fetching new ones |
| 1521 | anime.Ep.Links = []string{} |
| 1522 | Log(fmt.Sprintf("No prefetched links available for episode %d, will fetch new ones", anime.Ep.Number)) |
| 1563 | 1523 | } |
| 1564 | 1524 | |
| 1565 | 1525 | // Reset episode flags |
| @@ -1577,20 +1537,14 @@ func StartNextEpisode(anime *Anime, userCurdConfig *CurdConfig, databaseFile str |
| 1577 | 1537 | |
| 1578 | 1538 | // Update Anilist progress for the previous episode if not rewatching |
| 1579 | 1539 | if !anime.Rewatching { |
| 1580 | | user := &User{} |
| 1581 | | user.Token, err = GetTokenFromFile(filepath.Join(os.ExpandEnv(userCurdConfig.StoragePath), "token")) |
| 1582 | | if err != nil { |
| 1583 | | Log("Error reading token for progress update: " + err.Error()) |
| 1584 | | } else { |
| 1585 | | go func() { |
| 1586 | | err = UpdateAnimeProgress(user.Token, anime.AnilistId, prevEpisode) |
| 1587 | | if err != nil { |
| 1588 | | Log("Error updating Anilist progress: " + err.Error()) |
| 1589 | | } else { |
| 1590 | | CurdOut(fmt.Sprintf("Anime progress updated! Latest watched episode: %d", prevEpisode)) |
| 1591 | | } |
| 1592 | | }() |
| 1593 | | } |
| 1540 | go func() { |
| 1541 | err = UpdateAnimeProgress(userToken, anime.AnilistId, prevEpisode) |
| 1542 | if err != nil { |
| 1543 | Log("Error updating Anilist progress: " + err.Error()) |
| 1544 | } else { |
| 1545 | CurdOut(fmt.Sprintf("Anime progress updated! Latest watched episode: %d", prevEpisode)) |
| 1546 | } |
| 1547 | }() |
| 1594 | 1548 | } |
| 1595 | 1549 | |
| 1596 | 1550 | // Output message to user |
| @@ -1598,32 +1552,9 @@ func StartNextEpisode(anime *Anime, userCurdConfig *CurdConfig, databaseFile str |
| 1598 | 1552 | } |
| 1599 | 1553 | |
| 1600 | 1554 | // HandleLastEpisodeCompletion handles scoring and completion for the last episode |
| 1601 | | func HandleLastEpisodeCompletion(userCurdConfig *CurdConfig, anime *Anime) { |
| 1555 | func HandleLastEpisodeCompletion(userCurdConfig *CurdConfig, anime *Anime, userToken string) { |
| 1602 | 1556 | // Check if this is the last episode and scoring is enabled |
| 1603 | 1557 | if userCurdConfig.ScoreOnCompletion && anime.TotalEpisodes > 0 && anime.Ep.Number == anime.TotalEpisodes { |
| 1604 | | // Get user token for scoring |
| 1605 | | user := &User{} |
| 1606 | | |
| 1607 | | // Get config for storage path |
| 1608 | | homeDir := "" |
| 1609 | | if runtime.GOOS == "windows" { |
| 1610 | | homeDir = os.Getenv("USERPROFILE") |
| 1611 | | } else { |
| 1612 | | homeDir = os.Getenv("HOME") |
| 1613 | | } |
| 1614 | | configFilePath := filepath.Join(homeDir, ".config", "curd", "curd.conf") |
| 1615 | | config, err := LoadConfig(configFilePath) |
| 1616 | | if err != nil { |
| 1617 | | Log("Error loading config for scoring: " + err.Error()) |
| 1618 | | return |
| 1619 | | } |
| 1620 | | |
| 1621 | | user.Token, err = GetTokenFromFile(filepath.Join(os.ExpandEnv(config.StoragePath), "token")) |
| 1622 | | if err != nil { |
| 1623 | | Log("Error reading token for scoring: " + err.Error()) |
| 1624 | | return |
| 1625 | | } |
| 1626 | | |
| 1627 | 1558 | // Prompt user to score the anime |
| 1628 | 1559 | CurdOut("You've completed this anime! Would you like to rate it?") |
| 1629 | 1560 | |
| @@ -1639,7 +1570,7 @@ func HandleLastEpisodeCompletion(userCurdConfig *CurdConfig, anime *Anime) { |
| 1639 | 1570 | } |
| 1640 | 1571 | |
| 1641 | 1572 | if selectedOption.Key == "yes" { |
| 1642 | | err = RateAnime(user.Token, anime.AnilistId) |
| 1573 | err = RateAnime(userToken, anime.AnilistId) |
| 1643 | 1574 | if err != nil { |
| 1644 | 1575 | Log(fmt.Sprintf("Error rating anime: %v", err)) |
| 1645 | 1576 | CurdOut("Failed to rate anime") |
| @@ -1651,7 +1582,7 @@ func HandleLastEpisodeCompletion(userCurdConfig *CurdConfig, anime *Anime) { |
| 1651 | 1582 | // Update anime status to completed on AniList |
| 1652 | 1583 | if !anime.Rewatching { |
| 1653 | 1584 | go func() { |
| 1654 | | err = UpdateAnimeStatus(user.Token, anime.AnilistId, "COMPLETED") |
| 1585 | err := UpdateAnimeStatus(userToken, anime.AnilistId, "COMPLETED") |
| 1655 | 1586 | if err != nil { |
| 1656 | 1587 | Log("Error updating anime status to completed: " + err.Error()) |
| 1657 | 1588 | } else { |