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 (
4 "fmt" 4 "fmt"
5 "os" 5 "os"
6 "time" 6 "time"
7 7 "sync"
8 "github.com/wraient/curd/internal" 8 "github.com/wraient/curd/internal"
9) 9)
10 10
@@ -48,8 +48,13 @@ func main() {
48 48
49 internal.SetupCurd(&userCurdConfig, &anime, &user, &databaseAnimes, logFile) 49 internal.SetupCurd(&userCurdConfig, &anime, &user, &databaseAnimes, logFile)
50 50
51 // Create a channel to signal when to exit the skip loop
52 var wg sync.WaitGroup
53
51 // Main loop 54 // Main loop
52 // for { 55 for {
56 skipLoopDone := make(chan struct{})
57
53 // Get MalId and CoverImage (only if discord presence is enabled) 58 // Get MalId and CoverImage (only if discord presence is enabled)
54 if userCurdConfig.DiscordPresence { 59 if userCurdConfig.DiscordPresence {
55 anime.MalId, anime.CoverImage, err = internal.GetAnimeIDAndImage(anime.AnilistId) 60 anime.MalId, anime.CoverImage, err = internal.GetAnimeIDAndImage(anime.AnilistId)
@@ -67,6 +72,12 @@ func main() {
67 } 72 }
68 } 73 }
69 74
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
70 // Get episode data 81 // Get episode data
71 go func(){ 82 go func(){
72 err = internal.GetEpisodeData(anime.MalId, anime.Ep.Number, &anime) 83 err = internal.GetEpisodeData(anime.MalId, anime.Ep.Number, &anime)
@@ -74,25 +85,33 @@ func main() {
74 internal.Log("Error getting episode data: "+err.Error(), logFile) 85 internal.Log("Error getting episode data: "+err.Error(), logFile)
75 } 86 }
76 internal.Log(anime, logFile) 87 internal.Log(anime, logFile)
77 }()
78 88
79 // Start curd 89 if anime.Ep.IsFiller {
80 mpvSocketPath := internal.StartCurd(&userCurdConfig, &anime, logFile) 90 internal.Log("Filler episode", logFile)
81 91 internal.SeekMPV(mpvSocketPath, anime.Ep.Duration)
82 internal.Log(fmt.Sprint("Playback starting time: ", anime.Ep.Player.PlaybackTime), logFile) 92 }
83 internal.Log(mpvSocketPath, logFile)
84 93
94 }()
95
96 wg.Add(1)
85 // Thread to update Discord presence 97 // Thread to update Discord presence
86 go func() { 98 go func() {
99 defer wg.Done()
87 for { 100 for {
88 err = internal.DiscordPresence(discordClientId, anime) 101 select {
89 if err != nil { 102 case <-skipLoopDone:
90 internal.Log("Error setting Discord presence: "+err.Error(), logFile) 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)
91 } 110 }
92 time.Sleep(1 * time.Second)
93 } 111 }
94 }() 112 }()
95 113
114 // Get skip times Parallel
96 go func() { 115 go func() {
97 err = internal.GetAndParseAniSkipData(anime.MalId, anime.Ep.Number, 1, &anime) 116 err = internal.GetAndParseAniSkipData(anime.MalId, anime.Ep.Number, 1, &anime)
98 if err != nil { 117 if err != nil {
@@ -101,65 +120,96 @@ func main() {
101 internal.Log(anime.Ep.SkipTimes, logFile) 120 internal.Log(anime.Ep.SkipTimes, logFile)
102 }() 121 }()
103 122
123 wg.Add(1)
104 // Thread to update playback time in database 124 // Thread to update playback time in database
105 go func() { 125 go func() {
126 defer wg.Done()
106 for { 127 for {
107 time.Sleep(1 * time.Second) 128 select {
108 129 case <-skipLoopDone:
109 // Get current playback time 130 return
110 timePos, err := internal.MPVSendCommand(mpvSocketPath, []interface{}{"get_property", "time-pos"}) 131 default:
111 if err != nil { 132 time.Sleep(1 * time.Second)
112 internal.Log("Error getting playback time: "+err.Error(), logFile) 133
113 134 // Get current playback time
114 // User closed the video 135 // internal.Log("Getting playback time"+mpvSocketPath, logFile)
115 if anime.Ep.Started { 136 timePos, err := internal.MPVSendCommand(mpvSocketPath, []interface{}{"get_property", "time-pos"})
116 fmt.Println("Have a great day!") 137 if err != nil {
117 os.Exit(0) 138 internal.Log("Error getting playback time: "+err.Error(), logFile)
118 } 139
119 } 140 // User closed the video
120 141 if anime.Ep.Started {
121 // Convert timePos to integer 142
122 if timePos != nil { 143 if int(*internal.PercentageWatched(anime.Ep.Player.PlaybackTime, anime.Ep.Duration)) >= userCurdConfig.PercentageToMarkComplete {
123 if !anime.Ep.Started { 144 anime.Ep.Number++
124 anime.Ep.Started = true 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 }
125 } 156 }
126 157
127 // If resume is true, seek to the playback time 158 // Convert timePos to integer
128 if anime.Ep.Resume { 159 if timePos != nil && err == nil {
129 internal.SeekMPV(mpvSocketPath, anime.Ep.Player.PlaybackTime) 160 if !anime.Ep.Started {
130 anime.Ep.Resume = false 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)
131 } 179 }
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)
142 } 180 }
143 } 181 }
144 }() 182 }()
145 183
146 // Main playback loop 184 // Skip OP and ED
185 skipLoop:
147 for { 186 for {
148 // Check if MPV has started 187 select {
149 currentTime, err := internal.MPVSendCommand(mpvSocketPath, []interface{}{"get_property", "time-pos"}) 188 case <-skipLoopDone:
150 if err != nil || currentTime == nil { 189 // Exit signal received, break out of the skipLoop
151 } 190 break skipLoop
152 if userCurdConfig.SkipOp { 191 default:
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 { 192 // Check if MPV has started
154 internal.SeekMPV(mpvSocketPath, anime.Ep.SkipTimes.Op.End) 193 if userCurdConfig.SkipOp {
155 } 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 {
156 } 195 internal.SeekMPV(mpvSocketPath, anime.Ep.SkipTimes.Op.End)
157 if userCurdConfig.SkipEd { 196 }
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 { 197 }
159 internal.SeekMPV(mpvSocketPath, anime.Ep.SkipTimes.Ed.End) 198 if userCurdConfig.SkipEd {
160 } 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 }
161 } 203 }
162 time.Sleep(1 * time.Second) // Wait before checking again 204 time.Sleep(1 * time.Second) // Wait before checking again
163 } 205 }
164 206
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 }
165} 215}