Commit 40d49e5135
Unsigned
Layout: unified · split
cmd/curd/main.go +108 −58
| @@ -4,7 +4,7 @@ import ( | ||
| 4 | 4 | "fmt" |
| 5 | 5 | "os" |
| 6 | 6 | "time" |
| 7 | ||
| 7 | "sync" | |
| 8 | 8 | "github.com/wraient/curd/internal" |
| 9 | 9 | ) |
| 10 | 10 | |
| @@ -48,8 +48,13 @@ func main() { | ||
| 48 | 48 | |
| 49 | 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 | 54 | // Main loop |
| 52 | // for { | |
| 55 | for { | |
| 56 | skipLoopDone := make(chan struct{}) | |
| 57 | ||
| 53 | 58 | // Get MalId and CoverImage (only if discord presence is enabled) |
| 54 | 59 | if userCurdConfig.DiscordPresence { |
| 55 | 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 | 81 | // Get episode data |
| 71 | 82 | go func(){ |
| 72 | 83 | err = internal.GetEpisodeData(anime.MalId, anime.Ep.Number, &anime) |
| @@ -74,25 +85,33 @@ func main() { | ||
| 74 | 85 | internal.Log("Error getting episode data: "+err.Error(), logFile) |
| 75 | 86 | } |
| 76 | 87 | internal.Log(anime, logFile) |
| 77 | }() | |
| 78 | 88 | |
| 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 | } | |
| 84 | 93 | |
| 94 | }() | |
| 95 | ||
| 96 | wg.Add(1) | |
| 85 | 97 | // Thread to update Discord presence |
| 86 | 98 | go func() { |
| 99 | defer wg.Done() | |
| 87 | 100 | 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) | |
| 91 | 110 | } |
| 92 | time.Sleep(1 * time.Second) | |
| 93 | 111 | } |
| 94 | 112 | }() |
| 95 | 113 | |
| 114 | // Get skip times Parallel | |
| 96 | 115 | go func() { |
| 97 | 116 | err = internal.GetAndParseAniSkipData(anime.MalId, anime.Ep.Number, 1, &anime) |
| 98 | 117 | if err != nil { |
| @@ -101,65 +120,96 @@ func main() { | ||
| 101 | 120 | internal.Log(anime.Ep.SkipTimes, logFile) |
| 102 | 121 | }() |
| 103 | 122 | |
| 123 | wg.Add(1) | |
| 104 | 124 | // Thread to update playback time in database |
| 105 | 125 | go func() { |
| 126 | defer wg.Done() | |
| 106 | 127 | 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 | } | |
| 125 | 156 | } |
| 126 | 157 | |
| 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) | |
| 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 | 186 | 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 | } | |
| 161 | 203 | } |
| 162 | 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 | } |