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() {
108108 internal.Log("Rewatching anime: "+internal.GetAnimeName(anime), logFile)
109109 anime.Rewatching = true
110110 }
111
111
112 anime.Ep.Player.Speed = 1.0
113
112114 // Main loop
113115 for {
114116 // Create a channel to signal when to exit the skip loop
@@ -202,6 +204,29 @@ func main() {
202204 internal.Log(anime.Ep.SkipTimes, logFile)
203205 }()
204206
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
205230 wg.Add(1)
206231 // Thread to update playback time in database
207232 go func() {
@@ -224,6 +249,10 @@ func main() {
224249 if anime.Ep.Started {
225250 percentageWatched := internal.PercentageWatched(anime.Ep.Player.PlaybackTime, anime.Ep.Duration)
226251 // 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)
227256 if int(percentageWatched) >= userCurdConfig.PercentageToMarkComplete {
228257 anime.Ep.Number++
229258 anime.Ep.Started = false
@@ -231,10 +260,11 @@ func main() {
231260 anime.Ep.IsCompleted = true
232261 // Exit the skip loop
233262 close(skipLoopDone)
234 } else if fmt.Sprintf("%v", err) != "invalid character '{' after top-level value" { // Episode is not completed
235 internal.ExitCurd()
236 } else {
263 } else if fmt.Sprintf("%v", err) == "invalid character '{' after top-level value" { // Episode is not completed
237264 internal.Log("Received invalid JSON response, continuing...", logFile)
265 } else {
266 internal.Log("Episode is not completed, exiting", logFile)
267 internal.ExitCurd()
238268 }
239269 }
240270 }
@@ -243,6 +273,12 @@ func main() {
243273 if timePos != nil {
244274 if !anime.Ep.Started {
245275 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 }
246282 }
247283
248284 // If resume is true, seek to the playback time
@@ -271,7 +307,7 @@ func main() {
271307 }
272308 }()
273309
274 // Skip OP and ED
310 // Skip OP and ED and Save MPV Speed
275311 skipLoop:
276312 for {
277313 select {
@@ -279,7 +315,6 @@ func main() {
279315 // Exit signal received, break out of the skipLoop
280316 break skipLoop
281317 default:
282 // Check if MPV has started
283318 if userCurdConfig.SkipOp {
284319 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 {
285320 internal.SeekMPV(anime.Ep.Player.SocketPath, anime.Ep.SkipTimes.Op.End)
@@ -290,7 +325,13 @@ func main() {
290325 internal.SeekMPV(anime.Ep.Player.SocketPath, anime.Ep.SkipTimes.Ed.End)
291326 }
292327 }
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
293333 }
334
294335 time.Sleep(1 * time.Second) // Wait before checking again
295336 }
296337
@@ -302,6 +343,7 @@ func main() {
302343
303344 // Update Anilist progress
304345 fmt.Println("Starting next episode: "+fmt.Sprint(anime.Ep.Number))
346 anime.Ep.Started = false
305347
306348 if anime.Ep.IsCompleted && !anime.Rewatching {
307349 go func(){
curd deleted

Binary file not shown.

internal/curd.go +42 −3
@@ -6,6 +6,7 @@ import (
66 "os"
77 "os/exec"
88 "path/filepath"
9 "regexp"
910 "runtime"
1011 "strconv"
1112 "strings"
@@ -122,6 +123,7 @@ func SetupCurd(userCurdConfig *CurdConfig, anime *Anime, user *User, databaseAni
122123 user.AnimeList = ParseAnimeList(anilistUserData)
123124 animeListMap := GetAnimeMap(user.AnimeList)
124125 var anilistSelectedOption SelectionOption
126 var selectedAllanimeAnime SelectionOption
125127 var userQuery string
126128
127129 if anime.Ep.ContinueLast {
@@ -241,12 +243,48 @@ func SetupCurd(userCurdConfig *CurdConfig, anime *Anime, user *User, databaseAni
241243 anime.Ep.Number = animePointer.Ep.Number
242244 }
243245
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 }
244258
245 // Handel weird cases
246 if anime.TotalEpisodes < anime.Ep.Number {
259 if anime.TotalEpisodes == 0 { // If failed to get anime data
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
247277 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
249286 }
287 } else if anime.TotalEpisodes < anime.Ep.Number { // Handle weird cases
250288 Log(fmt.Sprintf("Weird case: anime.TotalEpisodes < anime.Ep.Number: %v < %v", anime.TotalEpisodes, anime.Ep.Number), logFile)
251289 fmt.Printf("Would like to start the anime from beginning? (y/n)\n")
252290 var answer string
@@ -257,6 +295,7 @@ func SetupCurd(userCurdConfig *CurdConfig, anime *Anime, user *User, databaseAni
257295 anime.Ep.Number = anime.TotalEpisodes
258296 }
259297 }
298
260299}
261300
262301// 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) {
106106func GetMPVPlaybackSpeed(ipcSocketPath string) (float64, error) {
107107 speed, err := MPVSendCommand(ipcSocketPath, []interface{}{"get_property", "speed"})
108108 if err != nil || speed == nil {
109 fmt.Println("Failed to get playback speed.")
109 Log("Failed to get playback speed.", logFile)
110110 return 0, err
111111 }
112112