| @@ -82,6 +82,7 @@ func RestoreScreen() { |
| 82 | 82 | |
| 83 | 83 | func ExitCurd() { |
| 84 | 84 | RestoreScreen() |
| 85 | fmt.Println("Have a great day!") |
| 85 | 86 | os.Exit(0) |
| 86 | 87 | } |
| 87 | 88 | |
| @@ -93,47 +94,78 @@ func SetupCurd(userCurdConfig *CurdConfig, anime *Anime, user *User, databaseAni |
| 93 | 94 | anilistUserData, err := GetUserData(user.Token, user.Id) |
| 94 | 95 | user.AnimeList = ParseAnimeList(anilistUserData) |
| 95 | 96 | animeListMap := GetAnimeMap(user.AnimeList) |
| 97 | var anilistSelectedOption SelectionOption |
| 98 | var userQuery string |
| 96 | 99 | |
| 97 | | // Select anime to watch (Anilist) |
| 98 | | anilistSelectedOption, err := DynamicSelect(animeListMap) |
| 99 | | if anilistSelectedOption.Key == "-1" || anilistSelectedOption.Label == "quit" { |
| 100 | | ExitCurd() |
| 101 | | } |
| 102 | | if anilistSelectedOption.Label == "add_new" { // Todo: Add new anime |
| 103 | | fmt.Println("Enter the anime name:") |
| 104 | | var query string |
| 105 | | fmt.Scanln(&query) |
| 106 | | animeMap, err := SearchAnimeAnilist(query, user.Token) |
| 100 | if anime.Ep.ContinueLast { |
| 101 | // Get the last watched anime ID from the curd_id file |
| 102 | curdIDPath := os.ExpandEnv("$HOME/Projects/curd/.local/share/curd/curd_id") |
| 103 | curdIDBytes, err := os.ReadFile(curdIDPath) |
| 107 | 104 | if err != nil { |
| 108 | | fmt.Println("Failed to search anime") |
| 105 | Log(fmt.Sprintf("Error reading curd_id file: %v", err), logFile) |
| 109 | 106 | ExitCurd() |
| 110 | 107 | } |
| 111 | | anilistSelectedOption, err = DynamicSelect(animeMap) |
| 108 | |
| 109 | lastWatchedID, err := strconv.Atoi(strings.TrimSpace(string(curdIDBytes))) |
| 112 | 110 | if err != nil { |
| 113 | | fmt.Println("No anime available") |
| 111 | Log(fmt.Sprintf("Error converting curd_id to integer: %v", err), logFile) |
| 114 | 112 | ExitCurd() |
| 115 | 113 | } |
| 116 | | animeID, err := strconv.Atoi(anilistSelectedOption.Key) |
| 114 | |
| 115 | anime.AnilistId = lastWatchedID |
| 116 | anilistSelectedOption.Key = strconv.Itoa(lastWatchedID) |
| 117 | } else { |
| 118 | // Select anime to watch (Anilist) |
| 119 | var err error |
| 120 | anilistSelectedOption, err = DynamicSelect(animeListMap) |
| 117 | 121 | if err != nil { |
| 118 | | fmt.Println("Failed to convert anime ID to integer") |
| 122 | Log(fmt.Sprintf("Error selecting anime: %v", err), logFile) |
| 119 | 123 | ExitCurd() |
| 120 | 124 | } |
| 121 | | err = AddAnimeToWatchingList(animeID, user.Token) |
| 125 | |
| 126 | if anilistSelectedOption.Key == "-1" || anilistSelectedOption.Label == "quit" { |
| 127 | ExitCurd() |
| 128 | } |
| 129 | |
| 130 | if anilistSelectedOption.Label == "add_new" { |
| 131 | fmt.Println("Enter the anime name:") |
| 132 | var query string |
| 133 | fmt.Scanln(&query) |
| 134 | animeMap, err := SearchAnimeAnilist(query, user.Token) |
| 135 | if err != nil { |
| 136 | Log(fmt.Sprintf("Failed to search anime: %v", err), logFile) |
| 137 | ExitCurd() |
| 138 | } |
| 139 | anilistSelectedOption, err = DynamicSelect(animeMap) |
| 140 | if err != nil { |
| 141 | Log(fmt.Sprintf("No anime available: %v", err), logFile) |
| 142 | ExitCurd() |
| 143 | } |
| 144 | animeID, err := strconv.Atoi(anilistSelectedOption.Key) |
| 145 | if err != nil { |
| 146 | Log(fmt.Sprintf("Failed to convert anime ID to integer: %v", err), logFile) |
| 147 | ExitCurd() |
| 148 | } |
| 149 | err = AddAnimeToWatchingList(animeID, user.Token) |
| 150 | if err != nil { |
| 151 | Log(fmt.Sprintf("Failed to add anime to watching list: %v", err), logFile) |
| 152 | ExitCurd() |
| 153 | } |
| 154 | anilistUserData, err := GetUserData(user.Token, user.Id) |
| 155 | if err != nil { |
| 156 | Log(fmt.Sprintf("Failed to get user data: %v", err), logFile) |
| 157 | ExitCurd() |
| 158 | } |
| 159 | user.AnimeList = ParseAnimeList(anilistUserData) |
| 160 | } |
| 161 | |
| 162 | userQuery = anilistSelectedOption.Label |
| 163 | anime.AnilistId, err = strconv.Atoi(anilistSelectedOption.Key) |
| 122 | 164 | if err != nil { |
| 123 | | fmt.Println("Failed to add anime to watching list") |
| 165 | Log(fmt.Sprintf("Error converting Anilist ID: %v", err), logFile) |
| 124 | 166 | ExitCurd() |
| 125 | 167 | } |
| 126 | | anilistUserData, err := GetUserData(user.Token, user.Id) |
| 127 | | user.AnimeList = ParseAnimeList(anilistUserData) |
| 128 | | // animeListMap := GetAnimeMap(user.AnimeList) |
| 129 | 168 | } |
| 130 | | userQuery := anilistSelectedOption.Label |
| 131 | | anime.AnilistId, err = strconv.Atoi(anilistSelectedOption.Key) |
| 132 | | if err != nil { |
| 133 | | fmt.Println("Error converting Anilist ID:", err) |
| 134 | | os.Exit(1) |
| 135 | | } |
| 136 | | |
| 137 | 169 | // Find anime in Local history |
| 138 | 170 | animePointer := LocalFindAnime(*databaseAnimes, anime.AnilistId, "") |
| 139 | 171 | |
| @@ -244,6 +276,17 @@ func StartCurd(userCurdConfig *CurdConfig, anime *Anime, logFile string) string |
| 244 | 276 | } |
| 245 | 277 | |
| 246 | 278 | Log(anime, logFile) |
| 279 | |
| 280 | // Write anime.AnilistId to .local/share/curd/curd_id |
| 281 | idFilePath := filepath.Join(os.Getenv("HOME"), "Projects", "curd", ".local", "share", "curd", "curd_id") |
| 282 | if err := os.MkdirAll(filepath.Dir(idFilePath), 0755); err != nil { |
| 283 | Log(fmt.Sprintf("Failed to create directory for curd_id: %v", err), logFile) |
| 284 | } else { |
| 285 | if err := os.WriteFile(idFilePath, []byte(fmt.Sprintf("%d", anime.AnilistId)), 0644); err != nil { |
| 286 | Log(fmt.Sprintf("Failed to write AnilistId to file: %v", err), logFile) |
| 287 | } |
| 288 | } |
| 289 | |
| 247 | 290 | mpvSocketPath, err := StartVideo(PrioritizeLink(anime.Ep.Links), []string{}) |
| 248 | 291 | |
| 249 | 292 | if err != nil { |