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 (
44 "flag"
55 "fmt"
66 "os"
7 "path/filepath"
78 "strconv"
89 "sync"
910 "time"
@@ -14,12 +15,10 @@ import (
1415func main() {
1516
1617 discordClientId := "1287457464148820089"
17 // fmt.Println(internal.LocalGetAllAnime("/home/wraient/Projects/curd/.config/curd/curd_history.txt"))
18 // internal.ExitCurd()
1918
2019 // Setup
2120
22 // internal.ClearScreen()
21 internal.ClearScreen()
2322 // defer internal.RestoreScreen()
2423
2524 var anime internal.Anime
@@ -38,7 +37,7 @@ func main() {
3837
3938 // Flags configured here cause userconfig needs to be changed.
4039 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")
4241 flag.StringVar(&userCurdConfig.SubsLanguage, "subs-lang", userCurdConfig.SubsLanguage, "Subtitles language")
4342 flag.IntVar(&userCurdConfig.PercentageToMarkComplete, "percentage-to-mark-complete", userCurdConfig.PercentageToMarkComplete, "Percentage to mark episode as complete")
4443
@@ -81,18 +80,18 @@ func main() {
8180 }
8281
8382 // 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"))
8584 if err != nil {
8685 internal.Log("Error reading token", logFile)
8786 }
8887 if user.Token == "" {
8988 fmt.Println("No token found, please generate a token from https://anilist.co/api/v2/oauth/authorize?client_id=20686&response_type=token")
9089 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"))
9291 }
9392
9493 // 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")
9695 databaseAnimes := internal.LocalGetAllAnime(databaseFile)
9796
9897 internal.SetupCurd(&userCurdConfig, &anime, &user, &databaseAnimes, logFile)
@@ -102,16 +101,18 @@ func main() {
102101 internal.Log("Error finding anime by Anilist ID: "+err.Error(), logFile)
103102 }
104103
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)
106108 internal.Log("Rewatching anime: "+internal.GetAnimeName(anime), logFile)
107 anime.Rewatching = true
109 anime.Rewatching = true
108110 }
109
110 // Create a channel to signal when to exit the skip loop
111 var wg sync.WaitGroup
112
111
113112 // Main loop
114113 for {
114 // Create a channel to signal when to exit the skip loop
115 var wg sync.WaitGroup
115116 skipLoopDone := make(chan struct{})
116117
117118 // Get MalId and CoverImage (only if discord presence is enabled)
@@ -236,7 +237,6 @@ func main() {
236237 internal.Log("Received invalid JSON response, continuing...", logFile)
237238 }
238239 }
239 continue
240240 }
241241
242242 // Convert timePos to integer
@@ -302,11 +302,27 @@ func main() {
302302
303303 // Update Anilist progress
304304 fmt.Println("Starting next episode: "+fmt.Sprint(anime.Ep.Number))
305
305306 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
307314 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 }
308326 }
309
310 continue
311327 }
312328}
internal/config.go +2 −2
@@ -13,9 +13,9 @@ import (
1313// CurdConfig struct with field names that match the config keys
1414type CurdConfig struct {
1515 Player string `config:"Player"`
16 HistoryFile string `config:"HistoryFile"`
1716 SubsLanguage string `config:"SubsLanguage"`
1817 SubOrDub string `config:"SubOrDub"`
18 StoragePath string `config:"StoragePath"`
1919 PercentageToMarkComplete int `config:"PercentageToMarkComplete"`
2020 NextEpisodePrompt bool `config:"NextEpisodePrompt"`
2121 SkipOp bool `config:"SkipOp"`
@@ -73,7 +73,7 @@ func LoadConfig(configPath string) (CurdConfig, error) {
7373func defaultConfigMap() map[string]string {
7474 return map[string]string{
7575 "Player": "mpv",
76 "HistoryFile": "$HOME/.local/share/curd/curd_history.txt",
76 "StoragePath": "$HOME/.local/share/curd",
7777 "SubsLanguage": "english",
7878 "SubOrDub": "sub",
7979 "PercentageToMarkComplete": "90",
internal/curd.go +7 −3
@@ -108,7 +108,7 @@ func RestoreScreen() {
108108}
109109
110110func ExitCurd() {
111 RestoreScreen()
111 // RestoreScreen()
112112 fmt.Println("Have a great day!")
113113 os.Exit(0)
114114}
@@ -126,7 +126,7 @@ func SetupCurd(userCurdConfig *CurdConfig, anime *Anime, user *User, databaseAni
126126
127127 if anime.Ep.ContinueLast {
128128 // 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")
130130 curdIDBytes, err := os.ReadFile(curdIDPath)
131131 if err != nil {
132132 Log(fmt.Sprintf("Error reading curd_id file: %v", err), logFile)
@@ -244,6 +244,10 @@ func SetupCurd(userCurdConfig *CurdConfig, anime *Anime, user *User, databaseAni
244244
245245 // Handel weird cases
246246 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)
247251 fmt.Printf("Would like to start the anime from beginning? (y/n)\n")
248252 var answer string
249253 fmt.Scanln(&answer)
@@ -305,7 +309,7 @@ func StartCurd(userCurdConfig *CurdConfig, anime *Anime, logFile string) string
305309 Log(anime, logFile)
306310
307311 // 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")
309313 if err := os.MkdirAll(filepath.Dir(idFilePath), 0755); err != nil {
310314 Log(fmt.Sprintf("Failed to create directory for curd_id: %v", err), logFile)
311315 } else {
internal/localTracking.go +8 −14
@@ -121,7 +121,6 @@ func LocalGetAllAnime(databaseFile string) []Anime {
121121
122122 return animeList
123123}
124
125124// Function to parse a single row of anime data
126125func parseAnimeRow(row []string) *Anime {
127126 if len(row) < 5 {
@@ -142,27 +141,22 @@ func parseAnimeRow(row []string) *Anime {
142141 PlaybackTime: playbackTime,
143142 },
144143 },
145 Title: AnimeTitle{
146 English: row[4],
147 Romaji: row[4],
148 },
149144 }
150145
151 // Check if there's an extra field for total episodes
152146 if len(row) == 6 {
153 totalEpisodes, _ := strconv.Atoi(row[5])
154 if watchingEpisode == totalEpisodes {
155 // If watching episode equals total episodes, don't include the total episodes field
156 return anime
147 anime.Title = AnimeTitle{
148 English: row[5],
149 Romaji: row[5],
150 }
151 } else if len(row) == 5 {
152 anime.Title = AnimeTitle{
153 English: row[4],
154 Romaji: row[4],
157155 }
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)
161156 }
162157
163158 return anime
164159}
165
166160// Function to get the anime name (English or Romaji) from an Anime struct
167161func GetAnimeName(anime Anime) string {
168162 if anime.Title.English != "" {
internal/structs.go +1 −1
@@ -41,7 +41,7 @@ type Episode struct {
4141 Aired string `json:"aired"`
4242 Synopsis string `json:"synopsis"`
4343 ContinueLast bool
44 IsCompleted bool
44 IsCompleted bool
4545}
4646
4747type playingVideo struct {