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 @@
1package main 1package main
2 2
3import ( 3import (
4 // "encoding/json"
4 "fmt" 5 "fmt"
5 "math/rand"
6 "os" 6 "os"
7 "strconv" 7 "strconv"
8 "time" 8 "time"
9
9 "github.com/wraient/curd/internal" 10 "github.com/wraient/curd/internal"
10) 11)
11 12
@@ -16,7 +17,7 @@ type AnimeTitle struct {
16} 17}
17 18
18type Anime struct { 19type Anime struct {
19 Tit AnimeTitle 20 Title AnimeTitle
20 Ep Episode 21 Ep Episode
21 TotalEpisodes int 22 TotalEpisodes int
22 MalId int 23 MalId int
@@ -33,7 +34,6 @@ type Episode struct {
33 Tit AnimeTitle 34 Tit AnimeTitle
34 Number int 35 Number int
35 Skip_times SkipTimes 36 Skip_times SkipTimes
36 Mode string
37 Links []string 37 Links []string
38 Is_filler bool 38 Is_filler bool
39 Is_recap bool 39 Is_recap bool
@@ -41,6 +41,12 @@ type Episode struct {
41 Synopsis string 41 Synopsis string
42} 42}
43 43
44type playingVideo struct {
45 url string
46 speed float64
47 playbackTime int
48}
49
44type User struct { 50type User struct {
45 token string 51 token string
46 username string 52 username string
@@ -48,74 +54,117 @@ type User struct {
48 animeList internal.AnimeList 54 animeList internal.AnimeList
49} 55}
50 56
51
52func main(){ 57func main(){
53 internal.ClearScreen() 58 // internal.ClearScreen()
54 defer internal.RestoreScreen() 59 // defer internal.RestoreScreen()
55 60
56 var userQuery string 61 var anime Anime
57 logFile := "debug.log" 62 var user User
58
59 fmt.Println("Enter anime: ")
60 fmt.Scanln(&userQuery) // Scan input until a newline
61 animeList, err := internal.SearchAnime(string(userQuery), "sub")
62 63
63 internal.Log(userQuery, logFile) 64 configFilePath := os.ExpandEnv("$HOME/Projects/curd/.config/curd/curd_config.txt")
65 logFile := "debug.log"
64 66
67 // load curd userCurdConfig
68 userCurdConfig, err := internal.LoadConfig(configFilePath)
65 if err != nil { 69 if err != nil {
66 fmt.Println("Failed to get anime list") 70 fmt.Println("Error loading config:", err)
67 os.Exit(1) 71 return
68 } 72 }
69 73
70 if len(animeList) == 0 { 74 internal.Log(userCurdConfig, logFile)
71 fmt.Println("No results found.")
72 os.Exit(0)
73 }
74
75 selectedOption, err := internal.DynamicSelect(animeList)
76 75
76 // Get the token from the token file
77 user.token, err = internal.GetTokenFromFile("/home/wraient/.local/share/curd/token")
77 if err != nil { 78 if err != nil {
78 fmt.Println("No anime Selected") 79 internal.Log("Error reading token", logFile)
79 os.Exit(0)
80 } 80 }
81 81
82 internal.Log(animeList, logFile) 82 // Get user id, username and Anime list
83 internal.Log(selectedOption, logFile) 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)
84 87
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 }
86 96
87 if err != nil{ 97 // Get Anime list (All anime)
88 fmt.Println("No episode list found") 98 animeList, err := internal.SearchAnime(string(userQuery), userCurdConfig.SubOrDub)
89 internal.RestoreScreen() 99 if err != nil {
100 fmt.Println("Failed to select anime", logFile)
90 os.Exit(1) 101 os.Exit(1)
91 } 102 }
92 103 if len(animeList) == 0 {
93 if episodeList == nil { // if user selected "Add new anime option" 104 fmt.Println("No results found.")
94 fmt.Println("Add new anime")
95 internal.RestoreScreen()
96 os.Exit(0) 105 os.Exit(0)
97 } 106 }
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
98 117
99 internal.Log(episodeList, logFile) 118 // Get allanime id of anime
100 var episodeNumber string 119 anime.AllanimeId, err = internal.FindKeyByValue(animeList, fmt.Sprintf("%v (%d episodes)", userQuery, selectedAnilistAnime.Media.Episodes))
101 fmt.Printf("Enter the episode (%v episodes)", episodeList[len(episodeList)-1]) 120 // If unable to get id automatically get manually
102 fmt.Scanln(&episodeNumber) 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 }
103 131
104 link, err := internal.GetEpisodeURL(internal.GetDefaultAllanimeConfig(), selectedOption.Key, episodeNumber) 132 // internal.Log(userQuery, logFile)
105 133
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
106 if err != nil { 160 if err != nil {
107 fmt.Println("Failed to get episode link") 161 fmt.Println("Failed to get episode link")
108 os.Exit(1) 162 os.Exit(1)
109 } 163 }
164
165 internal.Log(anime, logFile)
110 166
111 internal.Log(link, logFile) 167 mpvSocketPath, err := internal.StartMpv(anime.Ep.Links[len(anime.Ep.Links)-1])
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)
119 168
120 if err != nil { 169 if err != nil {
121 internal.Log("Failed to start mpv", logFile) 170 internal.Log("Failed to start mpv", logFile)
@@ -123,6 +172,15 @@ func main(){
123 } 172 }
124 173
125 for { // Loop while video is playing 174 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)
126 } 185 }
127
128}\ No newline at end of file 186}\ No newline at end of file