Commit d734957927

d734957927966d0edcbaf16323d25ada26a6e64e

parent: 956f85feae

Unsigned

Wraient <wraient.911@gmail.com> · 2024-10-23 09:21 UTC

Fix: Improvemnet and Rate anime added

Layout: unified · split

cmd/curd/main.go +33 −17
@@ -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 (
14func main() { 15func 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}
internal/config.go +2 −2
@@ -13,9 +13,9 @@ import (
13// CurdConfig struct with field names that match the config keys 13// CurdConfig struct with field names that match the config keys
14type CurdConfig struct { 14type CurdConfig struct {
15 Player string `config:"Player"` 15 Player string `config:"Player"`
16 HistoryFile string `config:"HistoryFile"`
17 SubsLanguage string `config:"SubsLanguage"` 16 SubsLanguage string `config:"SubsLanguage"`
18 SubOrDub string `config:"SubOrDub"` 17 SubOrDub string `config:"SubOrDub"`
18 StoragePath string `config:"StoragePath"`
19 PercentageToMarkComplete int `config:"PercentageToMarkComplete"` 19 PercentageToMarkComplete int `config:"PercentageToMarkComplete"`
20 NextEpisodePrompt bool `config:"NextEpisodePrompt"` 20 NextEpisodePrompt bool `config:"NextEpisodePrompt"`
21 SkipOp bool `config:"SkipOp"` 21 SkipOp bool `config:"SkipOp"`
@@ -73,7 +73,7 @@ func LoadConfig(configPath string) (CurdConfig, error) {
73func defaultConfigMap() map[string]string { 73func defaultConfigMap() map[string]string {
74 return map[string]string{ 74 return map[string]string{
75 "Player": "mpv", 75 "Player": "mpv",
76 "HistoryFile": "$HOME/.local/share/curd/curd_history.txt", 76 "StoragePath": "$HOME/.local/share/curd",
77 "SubsLanguage": "english", 77 "SubsLanguage": "english",
78 "SubOrDub": "sub", 78 "SubOrDub": "sub",
79 "PercentageToMarkComplete": "90", 79 "PercentageToMarkComplete": "90",
internal/curd.go +7 −3
@@ -108,7 +108,7 @@ func RestoreScreen() {
108} 108}
109 109
110func ExitCurd() { 110func ExitCurd() {
111 RestoreScreen() 111 // RestoreScreen()
112 fmt.Println("Have a great day!") 112 fmt.Println("Have a great day!")
113 os.Exit(0) 113 os.Exit(0)
114} 114}
@@ -126,7 +126,7 @@ func SetupCurd(userCurdConfig *CurdConfig, anime *Anime, user *User, databaseAni
126 126
127 if anime.Ep.ContinueLast { 127 if anime.Ep.ContinueLast {
128 // Get the last watched anime ID from the curd_id file 128 // Get the last watched anime ID from the curd_id file
129 curdIDPath := os.ExpandEnv("$HOME/Projects/curd/.local/share/curd/curd_id") 129 curdIDPath := filepath.Join(os.Getenv(userCurdConfig.StoragePath), "curd_id")
130 curdIDBytes, err := os.ReadFile(curdIDPath) 130 curdIDBytes, err := os.ReadFile(curdIDPath)
131 if err != nil { 131 if err != nil {
132 Log(fmt.Sprintf("Error reading curd_id file: %v", err), logFile) 132 Log(fmt.Sprintf("Error reading curd_id file: %v", err), logFile)
@@ -244,6 +244,10 @@ func SetupCurd(userCurdConfig *CurdConfig, anime *Anime, user *User, databaseAni
244 244
245 // Handel weird cases 245 // Handel weird cases
246 if anime.TotalEpisodes < anime.Ep.Number { 246 if anime.TotalEpisodes < anime.Ep.Number {
247 if anime.TotalEpisodes == 0 {
248 time.Sleep(2 * time.Second)
249 }
250 Log(fmt.Sprintf("Weird case: anime.TotalEpisodes < anime.Ep.Number: %v < %v", anime.TotalEpisodes, anime.Ep.Number), logFile)
247 fmt.Printf("Would like to start the anime from beginning? (y/n)\n") 251 fmt.Printf("Would like to start the anime from beginning? (y/n)\n")
248 var answer string 252 var answer string
249 fmt.Scanln(&answer) 253 fmt.Scanln(&answer)
@@ -305,7 +309,7 @@ func StartCurd(userCurdConfig *CurdConfig, anime *Anime, logFile string) string
305 Log(anime, logFile) 309 Log(anime, logFile)
306 310
307 // Write anime.AnilistId to .local/share/curd/curd_id 311 // Write anime.AnilistId to .local/share/curd/curd_id
308 idFilePath := filepath.Join(os.Getenv("HOME"), "Projects", "curd", ".local", "share", "curd", "curd_id") 312 idFilePath := filepath.Join(os.Getenv(userCurdConfig.StoragePath), "curd_id")
309 if err := os.MkdirAll(filepath.Dir(idFilePath), 0755); err != nil { 313 if err := os.MkdirAll(filepath.Dir(idFilePath), 0755); err != nil {
310 Log(fmt.Sprintf("Failed to create directory for curd_id: %v", err), logFile) 314 Log(fmt.Sprintf("Failed to create directory for curd_id: %v", err), logFile)
311 } else { 315 } else {
internal/localTracking.go +8 −14
@@ -121,7 +121,6 @@ func LocalGetAllAnime(databaseFile string) []Anime {
121 121
122 return animeList 122 return animeList
123} 123}
124
125// Function to parse a single row of anime data 124// Function to parse a single row of anime data
126func parseAnimeRow(row []string) *Anime { 125func parseAnimeRow(row []string) *Anime {
127 if len(row) < 5 { 126 if len(row) < 5 {
@@ -142,27 +141,22 @@ func parseAnimeRow(row []string) *Anime {
142 PlaybackTime: playbackTime, 141 PlaybackTime: playbackTime,
143 }, 142 },
144 }, 143 },
145 Title: AnimeTitle{
146 English: row[4],
147 Romaji: row[4],
148 },
149 } 144 }
150 145
151 // Check if there's an extra field for total episodes
152 if len(row) == 6 { 146 if len(row) == 6 {
153 totalEpisodes, _ := strconv.Atoi(row[5]) 147 anime.Title = AnimeTitle{
154 if watchingEpisode == totalEpisodes { 148 English: row[5],
155 // If watching episode equals total episodes, don't include the total episodes field 149 Romaji: row[5],
156 return anime 150 }
151 } else if len(row) == 5 {
152 anime.Title = AnimeTitle{
153 English: row[4],
154 Romaji: row[4],
157 } 155 }
158 // If not equal, you might want to store this information somewhere
159 // For now, we'll just print it
160 // fmt.Printf("Anime %s has %d total episodes\n", anime.Title.English, totalEpisodes)
161 } 156 }
162 157
163 return anime 158 return anime
164} 159}
165
166// Function to get the anime name (English or Romaji) from an Anime struct 160// Function to get the anime name (English or Romaji) from an Anime struct
167func GetAnimeName(anime Anime) string { 161func GetAnimeName(anime Anime) string {
168 if anime.Title.English != "" { 162 if anime.Title.English != "" {
internal/structs.go +1 −1
@@ -41,7 +41,7 @@ type Episode struct {
41 Aired string `json:"aired"` 41 Aired string `json:"aired"`
42 Synopsis string `json:"synopsis"` 42 Synopsis string `json:"synopsis"`
43 ContinueLast bool 43 ContinueLast bool
44 IsCompleted bool 44 IsCompleted bool
45} 45}
46 46
47type playingVideo struct { 47type playingVideo struct {