Commit c7cdf1c7d3

c7cdf1c7d3f3c972a46fabc6b479d3f3d34be05b

parent: 05a134cfc3

Unsigned

Wraient <wraient.911@gmail.com> · 2024-10-23 10:24 UTC

Fix: hard to debug stuff

Layout: unified · split

cmd/curd/main.go +48 −6
@@ -108,7 +108,9 @@ func main() {
108 internal.Log("Rewatching anime: "+internal.GetAnimeName(anime), logFile) 108 internal.Log("Rewatching anime: "+internal.GetAnimeName(anime), logFile)
109 anime.Rewatching = true 109 anime.Rewatching = true
110 } 110 }
111 111
112 anime.Ep.Player.Speed = 1.0
113
112 // Main loop 114 // Main loop
113 for { 115 for {
114 // Create a channel to signal when to exit the skip loop 116 // Create a channel to signal when to exit the skip loop
@@ -202,6 +204,29 @@ func main() {
202 internal.Log(anime.Ep.SkipTimes, logFile) 204 internal.Log(anime.Ep.SkipTimes, logFile)
203 }() 205 }()
204 206
207 go func() {
208 for {
209
210 if anime.Ep.Started {
211 if anime.Ep.Duration == 0 {
212 // Get video duration
213 durationPos, err := internal.MPVSendCommand(anime.Ep.Player.SocketPath, []interface{}{"get_property", "duration"})
214 if err != nil {
215 internal.Log("Error getting video duration: "+err.Error(), logFile)
216 } else if durationPos != nil {
217 if duration, ok := durationPos.(float64); ok {
218 anime.Ep.Duration = int(duration + 0.5) // Round to nearest integer
219 internal.Log(fmt.Sprintf("Video duration: %d seconds", anime.Ep.Duration), logFile)
220 } else {
221 internal.Log("Error: duration is not a float64", logFile)
222 }
223 }
224 break
225 }
226 }
227 }
228 }()
229
205 wg.Add(1) 230 wg.Add(1)
206 // Thread to update playback time in database 231 // Thread to update playback time in database
207 go func() { 232 go func() {
@@ -224,6 +249,10 @@ func main() {
224 if anime.Ep.Started { 249 if anime.Ep.Started {
225 percentageWatched := internal.PercentageWatched(anime.Ep.Player.PlaybackTime, anime.Ep.Duration) 250 percentageWatched := internal.PercentageWatched(anime.Ep.Player.PlaybackTime, anime.Ep.Duration)
226 // Episode is completed 251 // Episode is completed
252 internal.Log(fmt.Sprint(percentageWatched), logFile)
253 internal.Log(fmt.Sprint(anime.Ep.Player.PlaybackTime), logFile)
254 internal.Log(fmt.Sprint(anime.Ep.Duration), logFile)
255 internal.Log(fmt.Sprint(userCurdConfig.PercentageToMarkComplete), logFile)
227 if int(percentageWatched) >= userCurdConfig.PercentageToMarkComplete { 256 if int(percentageWatched) >= userCurdConfig.PercentageToMarkComplete {
228 anime.Ep.Number++ 257 anime.Ep.Number++
229 anime.Ep.Started = false 258 anime.Ep.Started = false
@@ -231,10 +260,11 @@ func main() {
231 anime.Ep.IsCompleted = true 260 anime.Ep.IsCompleted = true
232 // Exit the skip loop 261 // Exit the skip loop
233 close(skipLoopDone) 262 close(skipLoopDone)
234 } else if fmt.Sprintf("%v", err) != "invalid character '{' after top-level value" { // Episode is not completed 263 } else if fmt.Sprintf("%v", err) == "invalid character '{' after top-level value" { // Episode is not completed
235 internal.ExitCurd()
236 } else {
237 internal.Log("Received invalid JSON response, continuing...", logFile) 264 internal.Log("Received invalid JSON response, continuing...", logFile)
265 } else {
266 internal.Log("Episode is not completed, exiting", logFile)
267 internal.ExitCurd()
238 } 268 }
239 } 269 }
240 } 270 }
@@ -243,6 +273,12 @@ func main() {
243 if timePos != nil { 273 if timePos != nil {
244 if !anime.Ep.Started { 274 if !anime.Ep.Started {
245 anime.Ep.Started = true 275 anime.Ep.Started = true
276 // Set the playback speed
277 speedCmd := []interface{}{"set_property", "speed", anime.Ep.Player.Speed}
278 _, err := internal.MPVSendCommand(anime.Ep.Player.SocketPath, speedCmd)
279 if err != nil {
280 internal.Log("Error setting playback speed: "+err.Error(), logFile)
281 }
246 } 282 }
247 283
248 // If resume is true, seek to the playback time 284 // If resume is true, seek to the playback time
@@ -271,7 +307,7 @@ func main() {
271 } 307 }
272 }() 308 }()
273 309
274 // Skip OP and ED 310 // Skip OP and ED and Save MPV Speed
275 skipLoop: 311 skipLoop:
276 for { 312 for {
277 select { 313 select {
@@ -279,7 +315,6 @@ func main() {
279 // Exit signal received, break out of the skipLoop 315 // Exit signal received, break out of the skipLoop
280 break skipLoop 316 break skipLoop
281 default: 317 default:
282 // Check if MPV has started
283 if userCurdConfig.SkipOp { 318 if userCurdConfig.SkipOp {
284 if anime.Ep.Player.PlaybackTime > anime.Ep.SkipTimes.Op.Start && anime.Ep.Player.PlaybackTime < anime.Ep.SkipTimes.Op.Start+2 && anime.Ep.SkipTimes.Op.Start != anime.Ep.SkipTimes.Op.End { 319 if anime.Ep.Player.PlaybackTime > anime.Ep.SkipTimes.Op.Start && anime.Ep.Player.PlaybackTime < anime.Ep.SkipTimes.Op.Start+2 && anime.Ep.SkipTimes.Op.Start != anime.Ep.SkipTimes.Op.End {
285 internal.SeekMPV(anime.Ep.Player.SocketPath, anime.Ep.SkipTimes.Op.End) 320 internal.SeekMPV(anime.Ep.Player.SocketPath, anime.Ep.SkipTimes.Op.End)
@@ -290,7 +325,13 @@ func main() {
290 internal.SeekMPV(anime.Ep.Player.SocketPath, anime.Ep.SkipTimes.Ed.End) 325 internal.SeekMPV(anime.Ep.Player.SocketPath, anime.Ep.SkipTimes.Ed.End)
291 } 326 }
292 } 327 }
328 anime.Ep.Player.Speed, err = internal.GetMPVPlaybackSpeed(anime.Ep.Player.SocketPath)
329 if err != nil {
330 internal.Log("Failed to mpv speed"+err.Error(), logFile)
331 }
332
293 } 333 }
334
294 time.Sleep(1 * time.Second) // Wait before checking again 335 time.Sleep(1 * time.Second) // Wait before checking again
295 } 336 }
296 337
@@ -302,6 +343,7 @@ func main() {
302 343
303 // Update Anilist progress 344 // Update Anilist progress
304 fmt.Println("Starting next episode: "+fmt.Sprint(anime.Ep.Number)) 345 fmt.Println("Starting next episode: "+fmt.Sprint(anime.Ep.Number))
346 anime.Ep.Started = false
305 347
306 if anime.Ep.IsCompleted && !anime.Rewatching { 348 if anime.Ep.IsCompleted && !anime.Rewatching {
307 go func(){ 349 go func(){
curd deleted

Binary file not shown.

internal/curd.go +42 −3
@@ -6,6 +6,7 @@ import (
6 "os" 6 "os"
7 "os/exec" 7 "os/exec"
8 "path/filepath" 8 "path/filepath"
9 "regexp"
9 "runtime" 10 "runtime"
10 "strconv" 11 "strconv"
11 "strings" 12 "strings"
@@ -122,6 +123,7 @@ func SetupCurd(userCurdConfig *CurdConfig, anime *Anime, user *User, databaseAni
122 user.AnimeList = ParseAnimeList(anilistUserData) 123 user.AnimeList = ParseAnimeList(anilistUserData)
123 animeListMap := GetAnimeMap(user.AnimeList) 124 animeListMap := GetAnimeMap(user.AnimeList)
124 var anilistSelectedOption SelectionOption 125 var anilistSelectedOption SelectionOption
126 var selectedAllanimeAnime SelectionOption
125 var userQuery string 127 var userQuery string
126 128
127 if anime.Ep.ContinueLast { 129 if anime.Ep.ContinueLast {
@@ -241,12 +243,48 @@ func SetupCurd(userCurdConfig *CurdConfig, anime *Anime, user *User, databaseAni
241 anime.Ep.Number = animePointer.Ep.Number 243 anime.Ep.Number = animePointer.Ep.Number
242 } 244 }
243 245
246 if anime.TotalEpisodes == 0 {
247 // Get updated anime data
248 Log(selectedAllanimeAnime, logFile)
249 updatedAnime, err := GetAnimeDataByID(anime.AnilistId, user.Token)
250 Log(updatedAnime, logFile)
251 if err != nil {
252 Log(fmt.Sprintf("Error getting updated anime data: %v", err), logFile)
253 } else {
254 anime.TotalEpisodes = updatedAnime.TotalEpisodes
255 Log(fmt.Sprintf("Updated total episodes: %d", anime.TotalEpisodes), logFile)
256 }
257 }
244 258
245 // Handel weird cases 259 if anime.TotalEpisodes == 0 { // If failed to get anime data
246 if anime.TotalEpisodes < anime.Ep.Number { 260 fmt.Println("Failed to get anime data. Attempting to retrieve from anime list.")
261 animeList, err := SearchAnime(string(userQuery), userCurdConfig.SubOrDub)
262 if err != nil {
263 fmt.Println("Failed to retrieve anime list:", err)
264 } else {
265 for allanimeId, label := range animeList {
266 if allanimeId == anime.AllanimeId {
267 // Extract total episodes from the label
268 if matches := regexp.MustCompile(`\((\d+) episodes\)`).FindStringSubmatch(label); len(matches) > 1 {
269 anime.TotalEpisodes, _ = strconv.Atoi(matches[1])
270 fmt.Printf("Retrieved total episodes: %d\n", anime.TotalEpisodes)
271 break
272 }
273 }
274 }
275 }
276
247 if anime.TotalEpisodes == 0 { 277 if anime.TotalEpisodes == 0 {
248 time.Sleep(2 * time.Second) 278 fmt.Println("Still unable to determine total episodes.")
279 fmt.Println("Your AniList progress:", selectedAnilistAnime.Progress)
280 fmt.Print("Enter the episode you want to start from: ")
281 var episodeNumber int
282 fmt.Scanln(&episodeNumber)
283 anime.Ep.Number = episodeNumber
284 } else {
285 anime.Ep.Number = selectedAnilistAnime.Progress + 1
249 } 286 }
287 } else if anime.TotalEpisodes < anime.Ep.Number { // Handle weird cases
250 Log(fmt.Sprintf("Weird case: anime.TotalEpisodes < anime.Ep.Number: %v < %v", anime.TotalEpisodes, anime.Ep.Number), logFile) 288 Log(fmt.Sprintf("Weird case: anime.TotalEpisodes < anime.Ep.Number: %v < %v", anime.TotalEpisodes, anime.Ep.Number), logFile)
251 fmt.Printf("Would like to start the anime from beginning? (y/n)\n") 289 fmt.Printf("Would like to start the anime from beginning? (y/n)\n")
252 var answer string 290 var answer string
@@ -257,6 +295,7 @@ func SetupCurd(userCurdConfig *CurdConfig, anime *Anime, user *User, databaseAni
257 anime.Ep.Number = anime.TotalEpisodes 295 anime.Ep.Number = anime.TotalEpisodes
258 } 296 }
259 } 297 }
298
260} 299}
261 300
262// CreateOrWriteTokenFile creates the token file if it doesn't exist and writes the token to it 301// CreateOrWriteTokenFile creates the token file if it doesn't exist and writes the token to it
internal/player.go +1 −1
@@ -106,7 +106,7 @@ func GetMPVPausedStatus(ipcSocketPath string) (bool, error) {
106func GetMPVPlaybackSpeed(ipcSocketPath string) (float64, error) { 106func GetMPVPlaybackSpeed(ipcSocketPath string) (float64, error) {
107 speed, err := MPVSendCommand(ipcSocketPath, []interface{}{"get_property", "speed"}) 107 speed, err := MPVSendCommand(ipcSocketPath, []interface{}{"get_property", "speed"})
108 if err != nil || speed == nil { 108 if err != nil || speed == nil {
109 fmt.Println("Failed to get playback speed.") 109 Log("Failed to get playback speed.", logFile)
110 return 0, err 110 return 0, err
111 } 111 }
112 112