Commit 40d49e5135

40d49e51353e72e2b001a015fd71bedc11c7e649

parent: e5ac4913c6

Unsigned

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

Add: Wait groupsand waiting for routines to end, new episode start logic added

Layout: unified · split

cmd/curd/main.go +108 −58
@@ -4,7 +4,7 @@ import (
44 "fmt"
55 "os"
66 "time"
7
7 "sync"
88 "github.com/wraient/curd/internal"
99)
1010
@@ -48,8 +48,13 @@ func main() {
4848
4949 internal.SetupCurd(&userCurdConfig, &anime, &user, &databaseAnimes, logFile)
5050
51 // Create a channel to signal when to exit the skip loop
52 var wg sync.WaitGroup
53
5154 // Main loop
52 // for {
55 for {
56 skipLoopDone := make(chan struct{})
57
5358 // Get MalId and CoverImage (only if discord presence is enabled)
5459 if userCurdConfig.DiscordPresence {
5560 anime.MalId, anime.CoverImage, err = internal.GetAnimeIDAndImage(anime.AnilistId)
@@ -67,6 +72,12 @@ func main() {
6772 }
6873 }
6974
75 // Start curd
76 mpvSocketPath := internal.StartCurd(&userCurdConfig, &anime, logFile)
77
78 internal.Log(fmt.Sprint("Playback starting time: ", anime.Ep.Player.PlaybackTime), logFile)
79 internal.Log(mpvSocketPath, logFile)
80
7081 // Get episode data
7182 go func(){
7283 err = internal.GetEpisodeData(anime.MalId, anime.Ep.Number, &anime)
@@ -74,25 +85,33 @@ func main() {
7485 internal.Log("Error getting episode data: "+err.Error(), logFile)
7586 }
7687 internal.Log(anime, logFile)
77 }()
7888
79 // Start curd
80 mpvSocketPath := internal.StartCurd(&userCurdConfig, &anime, logFile)
81
82 internal.Log(fmt.Sprint("Playback starting time: ", anime.Ep.Player.PlaybackTime), logFile)
83 internal.Log(mpvSocketPath, logFile)
89 if anime.Ep.IsFiller {
90 internal.Log("Filler episode", logFile)
91 internal.SeekMPV(mpvSocketPath, anime.Ep.Duration)
92 }
8493
94 }()
95
96 wg.Add(1)
8597 // Thread to update Discord presence
8698 go func() {
99 defer wg.Done()
87100 for {
88 err = internal.DiscordPresence(discordClientId, anime)
89 if err != nil {
90 internal.Log("Error setting Discord presence: "+err.Error(), logFile)
101 select {
102 case <-skipLoopDone:
103 return
104 default:
105 err = internal.DiscordPresence(discordClientId, anime)
106 if err != nil {
107 internal.Log("Error setting Discord presence: "+err.Error(), logFile)
108 }
109 time.Sleep(1 * time.Second)
91110 }
92 time.Sleep(1 * time.Second)
93111 }
94112 }()
95113
114 // Get skip times Parallel
96115 go func() {
97116 err = internal.GetAndParseAniSkipData(anime.MalId, anime.Ep.Number, 1, &anime)
98117 if err != nil {
@@ -101,65 +120,96 @@ func main() {
101120 internal.Log(anime.Ep.SkipTimes, logFile)
102121 }()
103122
123 wg.Add(1)
104124 // Thread to update playback time in database
105125 go func() {
126 defer wg.Done()
106127 for {
107 time.Sleep(1 * time.Second)
108
109 // Get current playback time
110 timePos, err := internal.MPVSendCommand(mpvSocketPath, []interface{}{"get_property", "time-pos"})
111 if err != nil {
112 internal.Log("Error getting playback time: "+err.Error(), logFile)
113
114 // User closed the video
115 if anime.Ep.Started {
116 fmt.Println("Have a great day!")
117 os.Exit(0)
118 }
119 }
120
121 // Convert timePos to integer
122 if timePos != nil {
123 if !anime.Ep.Started {
124 anime.Ep.Started = true
128 select {
129 case <-skipLoopDone:
130 return
131 default:
132 time.Sleep(1 * time.Second)
133
134 // Get current playback time
135 // internal.Log("Getting playback time"+mpvSocketPath, logFile)
136 timePos, err := internal.MPVSendCommand(mpvSocketPath, []interface{}{"get_property", "time-pos"})
137 if err != nil {
138 internal.Log("Error getting playback time: "+err.Error(), logFile)
139
140 // User closed the video
141 if anime.Ep.Started {
142
143 if int(*internal.PercentageWatched(anime.Ep.Player.PlaybackTime, anime.Ep.Duration)) >= userCurdConfig.PercentageToMarkComplete {
144 anime.Ep.Number++
145 anime.Ep.Started = false
146 internal.Log("Completed episode, starting next.", logFile)
147 // Exit the skip loop
148 close(skipLoopDone)
149 // continue
150 } else{
151 fmt.Println("Have a great day!")
152 os.Exit(0)
153 }
154
155 }
125156 }
126157
127 // If resume is true, seek to the playback time
128 if anime.Ep.Resume {
129 internal.SeekMPV(mpvSocketPath, anime.Ep.Player.PlaybackTime)
130 anime.Ep.Resume = false
158 // Convert timePos to integer
159 if timePos != nil && err == nil {
160 if !anime.Ep.Started {
161 anime.Ep.Started = true
162 }
163
164 // If resume is true, seek to the playback time
165 if anime.Ep.Resume {
166 internal.SeekMPV(mpvSocketPath, anime.Ep.Player.PlaybackTime)
167 anime.Ep.Resume = false
168 }
169
170 animePosition, ok := timePos.(float64)
171 if !ok {
172 internal.Log("Error: timePos is not a float64", logFile)
173 continue
174 }
175
176 anime.Ep.Player.PlaybackTime = int(animePosition + 0.5) // Round to nearest integer
177 // Update Local Database
178 internal.LocalUpdateAnime(databaseFile, anime.AnilistId, anime.AllanimeId, anime.Ep.Number, anime.Ep.Player.PlaybackTime, anime.Title.English)
131179 }
132
133 animePosition, ok := timePos.(float64)
134 if !ok {
135 internal.Log("Error: timePos is not a float64", logFile)
136 continue
137 }
138
139 anime.Ep.Player.PlaybackTime = int(animePosition + 0.5) // Round to nearest integer
140 // Update Local Database
141 internal.LocalUpdateAnime(databaseFile, anime.AnilistId, anime.AllanimeId, anime.Ep.Number, anime.Ep.Player.PlaybackTime, anime.Title.English)
142180 }
143181 }
144182 }()
145183
146 // Main playback loop
184 // Skip OP and ED
185 skipLoop:
147186 for {
148 // Check if MPV has started
149 currentTime, err := internal.MPVSendCommand(mpvSocketPath, []interface{}{"get_property", "time-pos"})
150 if err != nil || currentTime == nil {
151 }
152 if userCurdConfig.SkipOp {
153 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 {
154 internal.SeekMPV(mpvSocketPath, anime.Ep.SkipTimes.Op.End)
155 }
156 }
157 if userCurdConfig.SkipEd {
158 if anime.Ep.Player.PlaybackTime > anime.Ep.SkipTimes.Ed.Start && anime.Ep.Player.PlaybackTime < anime.Ep.SkipTimes.Ed.Start+2 && anime.Ep.SkipTimes.Ed.Start != anime.Ep.SkipTimes.Ed.End {
159 internal.SeekMPV(mpvSocketPath, anime.Ep.SkipTimes.Ed.End)
160 }
187 select {
188 case <-skipLoopDone:
189 // Exit signal received, break out of the skipLoop
190 break skipLoop
191 default:
192 // Check if MPV has started
193 if userCurdConfig.SkipOp {
194 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 {
195 internal.SeekMPV(mpvSocketPath, anime.Ep.SkipTimes.Op.End)
196 }
197 }
198 if userCurdConfig.SkipEd {
199 if anime.Ep.Player.PlaybackTime > anime.Ep.SkipTimes.Ed.Start && anime.Ep.Player.PlaybackTime < anime.Ep.SkipTimes.Ed.Start+2 && anime.Ep.SkipTimes.Ed.Start != anime.Ep.SkipTimes.Ed.End {
200 internal.SeekMPV(mpvSocketPath, anime.Ep.SkipTimes.Ed.End)
201 }
202 }
161203 }
162204 time.Sleep(1 * time.Second) // Wait before checking again
163205 }
164206
207 // Wait for all goroutines to finish before starting the next iteration
208 wg.Wait()
209
210 // Reset the WaitGroup for the next loop
211 wg = sync.WaitGroup{}
212
213 continue
214 }
165215}