| @@ -4,6 +4,7 @@ import ( |
| 4 | "flag" |
4 | "flag" |
| 5 | "fmt" |
5 | "fmt" |
| 6 | "os" |
6 | "os" |
| |
7 | "path/filepath" |
| 7 | "strconv" |
8 | "strconv" |
| 8 | "sync" |
9 | "sync" |
| 9 | "time" |
10 | "time" |
| @@ -14,12 +15,10 @@ import ( |
| 14 | func main() { |
15 | func main() { |
| 15 | |
16 | |
| 16 | discordClientId := "1287457464148820089" |
17 | discordClientId := "1287457464148820089" |
| 17 | // fmt.Println(internal.LocalGetAllAnime("/home/wraient/Projects/curd/.config/curd/curd_history.txt")) |
| |
| 18 | // internal.ExitCurd() |
| |
| 19 | |
18 | |
| 20 | // Setup |
19 | // Setup |
| 21 | |
20 | |
| 22 | // internal.ClearScreen() |
21 | internal.ClearScreen() |
| 23 | // defer internal.RestoreScreen() |
22 | // defer internal.RestoreScreen() |
| 24 | |
23 | |
| 25 | var anime internal.Anime |
24 | var anime internal.Anime |
| @@ -38,7 +37,7 @@ func main() { |
| 38 | |
37 | |
| 39 | // Flags configured here cause userconfig needs to be changed. |
38 | // Flags configured here cause userconfig needs to be changed. |
| 40 | flag.StringVar(&userCurdConfig.Player, "player", userCurdConfig.Player, "Player to use for playback (Only mpv supported currently)") |
39 | flag.StringVar(&userCurdConfig.Player, "player", userCurdConfig.Player, "Player to use for playback (Only mpv supported currently)") |
| 41 | flag.StringVar(&userCurdConfig.HistoryFile, "history-file", userCurdConfig.HistoryFile, "Path to the history file") |
40 | flag.StringVar(&userCurdConfig.StoragePath, "storage-path", userCurdConfig.StoragePath, "Path to the storage directory") |
| 42 | flag.StringVar(&userCurdConfig.SubsLanguage, "subs-lang", userCurdConfig.SubsLanguage, "Subtitles language") |
41 | flag.StringVar(&userCurdConfig.SubsLanguage, "subs-lang", userCurdConfig.SubsLanguage, "Subtitles language") |
| 43 | flag.IntVar(&userCurdConfig.PercentageToMarkComplete, "percentage-to-mark-complete", userCurdConfig.PercentageToMarkComplete, "Percentage to mark episode as complete") |
42 | flag.IntVar(&userCurdConfig.PercentageToMarkComplete, "percentage-to-mark-complete", userCurdConfig.PercentageToMarkComplete, "Percentage to mark episode as complete") |
| 44 | |
43 | |
| @@ -81,18 +80,18 @@ func main() { |
| 81 | } |
80 | } |
| 82 | |
81 | |
| 83 | // Get the token from the token file |
82 | // Get the token from the token file |
| 84 | user.Token, err = internal.GetTokenFromFile(os.ExpandEnv("$HOME/Projects/curd/.local/share/curd/token")) |
83 | user.Token, err = internal.GetTokenFromFile(filepath.Join(os.ExpandEnv(userCurdConfig.StoragePath), "token")) |
| 85 | if err != nil { |
84 | if err != nil { |
| 86 | internal.Log("Error reading token", logFile) |
85 | internal.Log("Error reading token", logFile) |
| 87 | } |
86 | } |
| 88 | if user.Token == "" { |
87 | if user.Token == "" { |
| 89 | fmt.Println("No token found, please generate a token from https://anilist.co/api/v2/oauth/authorize?client_id=20686&response_type=token") |
88 | fmt.Println("No token found, please generate a token from https://anilist.co/api/v2/oauth/authorize?client_id=20686&response_type=token") |
| 90 | fmt.Scanln(&user.Token) |
89 | fmt.Scanln(&user.Token) |
| 91 | internal.WriteTokenToFile(user.Token, os.ExpandEnv("$HOME/Projects/curd/.local/share/curd/token")) |
90 | internal.WriteTokenToFile(user.Token, filepath.Join(os.ExpandEnv(userCurdConfig.StoragePath), "token")) |
| 92 | } |
91 | } |
| 93 | |
92 | |
| 94 | // Load animes in database |
93 | // Load animes in database |
| 95 | databaseFile := os.ExpandEnv("$HOME/Projects/curd/.config/curd/curd_history.txt") |
94 | databaseFile := filepath.Join(os.ExpandEnv(userCurdConfig.StoragePath), "curd_history.txt") |
| 96 | databaseAnimes := internal.LocalGetAllAnime(databaseFile) |
95 | databaseAnimes := internal.LocalGetAllAnime(databaseFile) |
| 97 | |
96 | |
| 98 | internal.SetupCurd(&userCurdConfig, &anime, &user, &databaseAnimes, logFile) |
97 | internal.SetupCurd(&userCurdConfig, &anime, &user, &databaseAnimes, logFile) |
| @@ -102,16 +101,18 @@ func main() { |
| 102 | internal.Log("Error finding anime by Anilist ID: "+err.Error(), logFile) |
101 | internal.Log("Error finding anime by Anilist ID: "+err.Error(), logFile) |
| 103 | } |
102 | } |
| 104 | |
103 | |
| 105 | if anime.TotalEpisodes == temp_anime.Media.Episodes { |
104 | if anime.TotalEpisodes == temp_anime.Progress { |
| |
105 | internal.Log(temp_anime.Progress, logFile) |
| |
106 | internal.Log(anime.TotalEpisodes, logFile) |
| |
107 | internal.Log(user.AnimeList, logFile) |
| 106 | internal.Log("Rewatching anime: "+internal.GetAnimeName(anime), logFile) |
108 | internal.Log("Rewatching anime: "+internal.GetAnimeName(anime), logFile) |
| 107 | anime.Rewatching = true |
109 | anime.Rewatching = true |
| 108 | } |
110 | } |
| 109 | |
111 | |
| 110 | // Create a channel to signal when to exit the skip loop |
| |
| 111 | var wg sync.WaitGroup |
| |
| 112 | |
| |
| 113 | // Main loop |
112 | // Main loop |
| 114 | for { |
113 | for { |
| |
114 | // Create a channel to signal when to exit the skip loop |
| |
115 | var wg sync.WaitGroup |
| 115 | skipLoopDone := make(chan struct{}) |
116 | skipLoopDone := make(chan struct{}) |
| 116 | |
117 | |
| 117 | // Get MalId and CoverImage (only if discord presence is enabled) |
118 | // Get MalId and CoverImage (only if discord presence is enabled) |
| @@ -236,7 +237,6 @@ func main() { |
| 236 | internal.Log("Received invalid JSON response, continuing...", logFile) |
237 | internal.Log("Received invalid JSON response, continuing...", logFile) |
| 237 | } |
238 | } |
| 238 | } |
239 | } |
| 239 | continue |
| |
| 240 | } |
240 | } |
| 241 | |
241 | |
| 242 | // Convert timePos to integer |
242 | // Convert timePos to integer |
| @@ -302,11 +302,27 @@ func main() { |
| 302 | |
302 | |
| 303 | // Update Anilist progress |
303 | // Update Anilist progress |
| 304 | fmt.Println("Starting next episode: "+fmt.Sprint(anime.Ep.Number)) |
304 | fmt.Println("Starting next episode: "+fmt.Sprint(anime.Ep.Number)) |
| |
305 | |
| 305 | if anime.Ep.IsCompleted && !anime.Rewatching { |
306 | if anime.Ep.IsCompleted && !anime.Rewatching { |
| 306 | go internal.UpdateAnimeProgress(user.Token, anime.AnilistId, anime.Ep.Number) |
307 | go func(){ |
| |
308 | err = internal.UpdateAnimeProgress(user.Token, anime.AnilistId, anime.Ep.Number) |
| |
309 | if err != nil { |
| |
310 | internal.Log("Error updating Anilist progress: "+err.Error(), logFile) |
| |
311 | } |
| |
312 | }() |
| |
313 | |
| 307 | anime.Ep.IsCompleted = false |
314 | anime.Ep.IsCompleted = false |
| |
315 | // fmt.Println(anime.Ep.Number, anime.TotalEpisodes) |
| |
316 | if anime.Ep.Number-1 == anime.TotalEpisodes && userCurdConfig.ScoreOnCompletion { |
| |
317 | anime.Ep.Number = anime.Ep.Number - 1 |
| |
318 | var userScore float64 |
| |
319 | fmt.Println("Completed anime.") |
| |
320 | fmt.Println("Rate this anime: ") |
| |
321 | fmt.Scanln(&userScore) |
| |
322 | internal.RateAnime(user.Token, anime.AnilistId, userScore) |
| |
323 | internal.LocalDeleteAnime(databaseFile, anime.AnilistId, anime.AllanimeId) |
| |
324 | internal.ExitCurd() |
| |
325 | } |
| 308 | } |
326 | } |
| 309 | |
| |
| 310 | continue |
| |
| 311 | } |
327 | } |
| 312 | } |
328 | } |