Commit cb4d9e20c4
Unsigned
Layout: unified · split
internal/curd.go +100 −2
| @@ -5,6 +5,7 @@ import ( | |||
| 5 | "fmt" | 5 | "fmt" |
| 6 | "os" | 6 | "os" |
| 7 | "runtime" | 7 | "runtime" |
| 8 | "strconv" | ||
| 8 | "strings" | 9 | "strings" |
| 9 | "time" | 10 | "time" |
| 10 | ) | 11 | ) |
| @@ -54,7 +55,6 @@ func Log(data interface{}, logFile string) error { | |||
| 54 | return nil | 55 | return nil |
| 55 | } | 56 | } |
| 56 | 57 | ||
| 57 | |||
| 58 | // ClearScreen clears the terminal screen and saves the state | 58 | // ClearScreen clears the terminal screen and saves the state |
| 59 | func ClearScreen() { | 59 | func ClearScreen() { |
| 60 | fmt.Print("\033[?1049h") // Switch to alternate screen buffer | 60 | fmt.Print("\033[?1049h") // Switch to alternate screen buffer |
| @@ -65,4 +65,102 @@ func ClearScreen() { | |||
| 65 | // RestoreScreen restores the original terminal state | 65 | // RestoreScreen restores the original terminal state |
| 66 | func RestoreScreen() { | 66 | func RestoreScreen() { |
| 67 | fmt.Print("\033[?1049l") // Switch back to the main screen buffer | 67 | fmt.Print("\033[?1049l") // Switch back to the main screen buffer |
| 68 | }\ No newline at end of file | 68 | } |
| 69 | |||
| 70 | func SetupCurd(userCurdConfig *CurdConfig, anime *Anime, user *User, databaseAnimes *[]Anime, logFile string) { | ||
| 71 | var err error | ||
| 72 | |||
| 73 | // Get user id, username and Anime list | ||
| 74 | user.Id, user.Username, err = GetAnilistUserID(user.Token) | ||
| 75 | anilistUserData, err := GetUserData(user.Token, user.Id) | ||
| 76 | user.AnimeList = ParseAnimeList(anilistUserData) | ||
| 77 | animeListMap := GetAnimeMap(user.AnimeList) | ||
| 78 | |||
| 79 | // Select anime to watch (Anilist) | ||
| 80 | anilistSelectedOption, err := DynamicSelect(animeListMap) | ||
| 81 | userQuery := anilistSelectedOption.Label | ||
| 82 | anime.AnilistId, err = strconv.Atoi(anilistSelectedOption.Key) | ||
| 83 | if err != nil { | ||
| 84 | fmt.Println("Error converting Anilist ID:", err) | ||
| 85 | } | ||
| 86 | |||
| 87 | // Get Anime list (All anime) | ||
| 88 | animeList, err := SearchAnime(string(userQuery), userCurdConfig.SubOrDub) | ||
| 89 | if err != nil { | ||
| 90 | fmt.Println("Failed to select anime", logFile) | ||
| 91 | os.Exit(1) | ||
| 92 | } | ||
| 93 | if len(animeList) == 0 { | ||
| 94 | fmt.Println("No results found.") | ||
| 95 | os.Exit(0) | ||
| 96 | } | ||
| 97 | |||
| 98 | // Get anime entry | ||
| 99 | selectedAnilistAnime, err := FindAnimeByAnilistID(user.AnimeList, anilistSelectedOption.Key) | ||
| 100 | if err != nil { | ||
| 101 | fmt.Println("Can not find the anime in anilist animelist") | ||
| 102 | } | ||
| 103 | |||
| 104 | anime.Title = selectedAnilistAnime.Media.Title | ||
| 105 | anime.TotalEpisodes = selectedAnilistAnime.Media.Episodes | ||
| 106 | anime.Ep.Number = selectedAnilistAnime.Progress+1 | ||
| 107 | |||
| 108 | // Find anime in Local history | ||
| 109 | animePointer := LocalFindAnime(*databaseAnimes, anime.AnilistId, "") | ||
| 110 | |||
| 111 | // if anime not found in database, find it in animeList | ||
| 112 | if animePointer == nil { | ||
| 113 | // find anime in animeList | ||
| 114 | anime.AllanimeId, err = FindKeyByValue(animeList, fmt.Sprintf("%v (%d episodes)", userQuery, selectedAnilistAnime.Media.Episodes)) | ||
| 115 | } else { | ||
| 116 | // if anime found in database, use it | ||
| 117 | anime.AllanimeId = animePointer.AllanimeId | ||
| 118 | anime.Ep.Player.PlaybackTime = animePointer.Ep.Player.PlaybackTime | ||
| 119 | anime.Ep.Resume = true | ||
| 120 | anime.Ep.Number = animePointer.Ep.Number | ||
| 121 | } | ||
| 122 | // If unable to get Allanime id automatically get manually | ||
| 123 | if anime.AllanimeId == "" { | ||
| 124 | fmt.Println("Failed to automatically select anime") | ||
| 125 | selectedAllanimeAnime, err := DynamicSelect(animeList) | ||
| 126 | if err != nil { | ||
| 127 | fmt.Println("No anime available") | ||
| 128 | os.Exit(0) | ||
| 129 | } | ||
| 130 | anime.AllanimeId = selectedAllanimeAnime.Key | ||
| 131 | } | ||
| 132 | } | ||
| 133 | |||
| 134 | func StartCurd(userCurdConfig *CurdConfig, anime *Anime, logFile string) string { | ||
| 135 | |||
| 136 | // Get episode link | ||
| 137 | link, err := GetEpisodeURL(*userCurdConfig, anime.AllanimeId, anime.Ep.Number) | ||
| 138 | if err != nil { | ||
| 139 | // If unable to get episode link automatically get manually | ||
| 140 | episodeList, err := EpisodesList(anime.AllanimeId, userCurdConfig.SubOrDub) | ||
| 141 | if err != nil { | ||
| 142 | fmt.Println("No episode list found") | ||
| 143 | RestoreScreen() | ||
| 144 | os.Exit(1) | ||
| 145 | } | ||
| 146 | fmt.Printf("Enter the episode (%v episodes)\n", episodeList[len(episodeList)-1]) | ||
| 147 | fmt.Scanln(&anime.Ep.Number) | ||
| 148 | link, err = GetEpisodeURL(*userCurdConfig, anime.AllanimeId, anime.Ep.Number) | ||
| 149 | if err != nil { | ||
| 150 | fmt.Println("Failed to get episode link") | ||
| 151 | os.Exit(1) | ||
| 152 | } | ||
| 153 | // anime.Ep.Links = link | ||
| 154 | } | ||
| 155 | anime.Ep.Links = link | ||
| 156 | Log(anime, logFile) | ||
| 157 | mpvSocketPath, err := StartVideo(anime.Ep.Links[len(anime.Ep.Links)-1], []string{}) | ||
| 158 | |||
| 159 | if err != nil { | ||
| 160 | Log("Failed to start mpv", logFile) | ||
| 161 | os.Exit(1) | ||
| 162 | } | ||
| 163 | |||
| 164 | return mpvSocketPath | ||
| 165 | } | ||
| 166 | |||