Commit 02471056a4

02471056a4262d584094007c5e3fc6d8329295c7

parent: 6f20529449

Unsigned

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

Working stuff

Layout: unified · split

cmd/curd/main.go +105 −47
@@ -1,11 +1,12 @@
11package main
22
33import (
4 // "encoding/json"
45 "fmt"
5 "math/rand"
66 "os"
77 "strconv"
88 "time"
9
910 "github.com/wraient/curd/internal"
1011)
1112
@@ -16,7 +17,7 @@ type AnimeTitle struct {
1617}
1718
1819type Anime struct {
19 Tit AnimeTitle
20 Title AnimeTitle
2021 Ep Episode
2122 TotalEpisodes int
2223 MalId int
@@ -33,7 +34,6 @@ type Episode struct {
3334 Tit AnimeTitle
3435 Number int
3536 Skip_times SkipTimes
36 Mode string
3737 Links []string
3838 Is_filler bool
3939 Is_recap bool
@@ -41,6 +41,12 @@ type Episode struct {
4141 Synopsis string
4242}
4343
44type playingVideo struct {
45 url string
46 speed float64
47 playbackTime int
48}
49
4450type User struct {
4551 token string
4652 username string
@@ -48,74 +54,117 @@ type User struct {
4854 animeList internal.AnimeList
4955}
5056
51
5257func main(){
53 internal.ClearScreen()
54 defer internal.RestoreScreen()
58 // internal.ClearScreen()
59 // defer internal.RestoreScreen()
5560
56 var userQuery string
57 logFile := "debug.log"
58
59 fmt.Println("Enter anime: ")
60 fmt.Scanln(&userQuery) // Scan input until a newline
61 animeList, err := internal.SearchAnime(string(userQuery), "sub")
61 var anime Anime
62 var user User
6263
63 internal.Log(userQuery, logFile)
64 configFilePath := os.ExpandEnv("$HOME/Projects/curd/.config/curd/curd_config.txt")
65 logFile := "debug.log"
6466
67 // load curd userCurdConfig
68 userCurdConfig, err := internal.LoadConfig(configFilePath)
6569 if err != nil {
66 fmt.Println("Failed to get anime list")
67 os.Exit(1)
70 fmt.Println("Error loading config:", err)
71 return
6872 }
6973
70 if len(animeList) == 0 {
71 fmt.Println("No results found.")
72 os.Exit(0)
73 }
74
75 selectedOption, err := internal.DynamicSelect(animeList)
74 internal.Log(userCurdConfig, logFile)
7675
76 // Get the token from the token file
77 user.token, err = internal.GetTokenFromFile("/home/wraient/.local/share/curd/token")
7778 if err != nil {
78 fmt.Println("No anime Selected")
79 os.Exit(0)
79 internal.Log("Error reading token", logFile)
8080 }
8181
82 internal.Log(animeList, logFile)
83 internal.Log(selectedOption, logFile)
82 // Get user id, username and Anime list
83 user.id, user.username, err = internal.GetAnilistUserID(user.token)
84 anilistUserData, err := internal.GetUserData(user.token, user.id)
85 user.animeList = internal.ParseAnimeList(anilistUserData)
86 animeListMap := internal.GetAnimeMap(user.animeList)
8487
85 episodeList, err := internal.EpisodesList(selectedOption.Key, "sub")
88 // Select anime to watch (Anilist)
89 anilistSelectedOption, err := internal.DynamicSelect(animeListMap)
90 userQuery := anilistSelectedOption.Label
91 anime.AnilistId, err = strconv.Atoi(anilistSelectedOption.Key)
92 if err != nil {
93 fmt.Println("Error converting Anilist ID:", err)
94 return
95 }
8696
87 if err != nil{
88 fmt.Println("No episode list found")
89 internal.RestoreScreen()
97 // Get Anime list (All anime)
98 animeList, err := internal.SearchAnime(string(userQuery), userCurdConfig.SubOrDub)
99 if err != nil {
100 fmt.Println("Failed to select anime", logFile)
90101 os.Exit(1)
91102 }
92
93 if episodeList == nil { // if user selected "Add new anime option"
94 fmt.Println("Add new anime")
95 internal.RestoreScreen()
103 if len(animeList) == 0 {
104 fmt.Println("No results found.")
96105 os.Exit(0)
97106 }
107
108 // Get anime entry
109 selectedAnilistAnime, err := internal.FindAnimeByAnilistID(user.animeList, anilistSelectedOption.Key)
110 if err != nil {
111 fmt.Println("Can not find the anime in anilist animelist")
112 }
113
114 anime.Title = AnimeTitle(selectedAnilistAnime.Media.Title)
115 anime.TotalEpisodes = selectedAnilistAnime.Media.Episodes
116 anime.Ep.Number = selectedAnilistAnime.Progress
98117
99 internal.Log(episodeList, logFile)
100 var episodeNumber string
101 fmt.Printf("Enter the episode (%v episodes)", episodeList[len(episodeList)-1])
102 fmt.Scanln(&episodeNumber)
118 // Get allanime id of anime
119 anime.AllanimeId, err = internal.FindKeyByValue(animeList, fmt.Sprintf("%v (%d episodes)", userQuery, selectedAnilistAnime.Media.Episodes))
120 // If unable to get id automatically get manually
121 if err != nil {
122 fmt.Println("Failed to automatically select anime")
123 selectedAllanimeAnime, err := internal.DynamicSelect(animeList)
124 if err != nil {
125 fmt.Println("No anime available")
126 os.Exit(0)
127 }
128
129 anime.AllanimeId = selectedAllanimeAnime.Key
130 }
103131
104 link, err := internal.GetEpisodeURL(internal.GetDefaultAllanimeConfig(), selectedOption.Key, episodeNumber)
132 // internal.Log(userQuery, logFile)
105133
134 // os.Exit(0)
135 link, err := internal.GetEpisodeURL(userCurdConfig, anime.AllanimeId, anime.Ep.Number)
136 if err != nil {
137 episodeList, err := internal.EpisodesList(anime.AllanimeId, userCurdConfig.SubOrDub)
138
139 if err != nil{
140 fmt.Println("No episode list found")
141 internal.RestoreScreen()
142 os.Exit(1)
143 }
144
145 if episodeList == nil { // if user selected "Add new anime option"
146 fmt.Println("Add new anime") // TODO: add new anime implementaion
147 internal.RestoreScreen()
148 os.Exit(0)
149 }
150
151 internal.Log(episodeList, logFile)
152 fmt.Printf("Enter the episode (%v episodes)\n", episodeList[len(episodeList)-1])
153 fmt.Scanln(&anime.Ep.Number)
154 link, err := internal.GetEpisodeURL(userCurdConfig, anime.AllanimeId, anime.Ep.Number)
155 anime.Ep.Links = link
156 }
157
158 anime.Ep.Links = link
159
106160 if err != nil {
107161 fmt.Println("Failed to get episode link")
108162 os.Exit(1)
109163 }
164
165 internal.Log(anime, logFile)
110166
111 internal.Log(link, logFile)
112
113 // Generate a random number between 0 and 99
114 randomNumber := rand.Intn(100) // Change 100 to the desired range
115 fmt.Println("random number", randomNumber)
116 mpvSocketPath := "/tmp/mpvsocket"+strconv.Itoa(randomNumber)
117
118 err = internal.StartMpv(link[0], mpvSocketPath)
167 mpvSocketPath, err := internal.StartMpv(anime.Ep.Links[len(anime.Ep.Links)-1])
119168
120169 if err != nil {
121170 internal.Log("Failed to start mpv", logFile)
@@ -123,6 +172,15 @@ func main(){
123172 }
124173
125174 for { // Loop while video is playing
175 speed, err :=internal.GetMpvProperty(mpvSocketPath, "speed")
176
177 if err != nil {
178 // fmt.Println("failed to get mpv speed")
179 // internal.Log(mpvSocketPath, logFile)
180 // internal.Log(speed, logFile)
181 } else {
182 fmt.Println(speed)
183 }
184 time.Sleep(1)
126185 }
127
128186}
\ No newline at end of file