Commit 2b6d9e7f72

2b6d9e7f72a8f7e3f5dd097f8bad10a5749115c9

parent: 5ff0e19f3c

Unregistered key

Rushikesh Gaikwad <81632222+Wraient@users.noreply.github.com> · 2025-02-27 16:23 UTC
committer: <noreply@github.com>

Feat: Use highest priority link if extracted early (#50)

* Fix: Remove unnecessary output at end

* Feat: Use highest priority link if extracted early

Layout: unified · split

internal/curd.go +3 −2
@@ -151,8 +151,9 @@ func ExitCurd(err error) {
151 } 151 }
152 152
153 CurdOut("Have a great day!") 153 CurdOut("Have a great day!")
154 if err != nil { 154 // If the error is not about the connection refused, print the error
155 CurdOut(err) 155 if err != nil && !strings.Contains(err.Error(), "dial unix "+anime.Ep.Player.SocketPath+": connect: connection refused") {
156 CurdOut(fmt.Sprintf("Erorr: %v", err))
156 if runtime.GOOS == "windows" { 157 if runtime.GOOS == "windows" {
157 fmt.Println("Press Enter to exit") 158 fmt.Println("Press Enter to exit")
158 var wait string 159 var wait string
internal/episode_url.go +55 −5
@@ -22,6 +22,12 @@ type allanimeResponse struct {
22 } `json:"data"` 22 } `json:"data"`
23} 23}
24 24
25type result struct {
26 index int
27 links []string
28 err error
29}
30
25func decodeProviderID(encoded string) string { 31func decodeProviderID(encoded string) string {
26 // Split the string into pairs of characters (.. equivalent of 'sed s/../&\n/g') 32 // Split the string into pairs of characters (.. equivalent of 'sed s/../&\n/g')
27 re := regexp.MustCompile("..") 33 re := regexp.MustCompile("..")
@@ -152,11 +158,6 @@ func GetEpisodeURL(config CurdConfig, id string, epNo int) ([]string, error) {
152 return nil, err 158 return nil, err
153 } 159 }
154 160
155 type result struct {
156 index int
157 links []string
158 err error
159 }
160 161
161 // Pre-count valid URLs and create slice to preserve order 162 // Pre-count valid URLs and create slice to preserve order
162 validURLs := make([]string, 0) 163 validURLs := make([]string, 0)
@@ -174,11 +175,15 @@ func GetEpisodeURL(config CurdConfig, id string, epNo int) ([]string, error) {
174 results := make(chan result, len(validURLs)) 175 results := make(chan result, len(validURLs))
175 orderedResults := make([][]string, len(validURLs)) 176 orderedResults := make([][]string, len(validURLs))
176 177
178 // Add a channel for high priority links
179 highPriorityLink := make(chan []string, 1)
180
177 // Create rate limiter 181 // Create rate limiter
178 rateLimiter := time.NewTicker(50 * time.Millisecond) 182 rateLimiter := time.NewTicker(50 * time.Millisecond)
179 defer rateLimiter.Stop() 183 defer rateLimiter.Stop()
180 184
181 // Launch goroutines 185 // Launch goroutines
186 remainingURLs := len(validURLs)
182 for i, sourceUrl := range validURLs { 187 for i, sourceUrl := range validURLs {
183 go func(idx int, url string) { 188 go func(idx int, url string) {
184 <-rateLimiter.C // Rate limit the requests 189 <-rateLimiter.C // Rate limit the requests
@@ -222,6 +227,21 @@ func GetEpisodeURL(config CurdConfig, id string, epNo int) ([]string, error) {
222 links = append(links, link) 227 links = append(links, link)
223 } 228 }
224 229
230 // Check if any of the extracted links are high priority
231 for _, link := range links {
232 for _, domain := range LinkPriorities[:3] { // Check only top 3 priority domains
233 if strings.Contains(link, domain) {
234 // Found high priority link, send it immediately
235 select {
236 case highPriorityLink <- []string{link}:
237 default:
238 // Channel already has a high priority link
239 }
240 break
241 }
242 }
243 }
244
225 results <- result{ 245 results <- result{
226 index: idx, 246 index: idx,
227 links: links, 247 links: links,
@@ -234,6 +254,17 @@ func GetEpisodeURL(config CurdConfig, id string, epNo int) ([]string, error) {
234 var collectedErrors []error 254 var collectedErrors []error
235 successCount := 0 255 successCount := 0
236 256
257 // First, try to get a high priority link
258 select {
259 case links := <-highPriorityLink:
260 // Continue extracting other links in background
261 go collectRemainingResults(results, orderedResults, &successCount, &collectedErrors, remainingURLs)
262 return links, nil
263 case <-time.After(2 * time.Second): // Wait only briefly for high priority link
264 // No high priority link found quickly, proceed with normal collection
265 }
266
267 // Continue with existing result collection logic
237 // Collect results maintaining order 268 // Collect results maintaining order
238 for successCount < len(validURLs) { 269 for successCount < len(validURLs) {
239 select { 270 select {
@@ -270,6 +301,25 @@ func GetEpisodeURL(config CurdConfig, id string, epNo int) ([]string, error) {
270 return allLinks, nil 301 return allLinks, nil
271} 302}
272 303
304// Helper function to collect remaining results in background
305func collectRemainingResults(results chan result, orderedResults [][]string, successCount *int, collectedErrors *[]error, remainingURLs int) {
306 for *successCount < remainingURLs {
307 select {
308 case res := <-results:
309 if res.err != nil {
310 Log(fmt.Sprintf("Error processing URL %d: %v", res.index+1, res.err))
311 *collectedErrors = append(*collectedErrors, fmt.Errorf("URL %d: %w", res.index+1, res.err))
312 } else {
313 orderedResults[res.index] = res.links
314 *successCount++
315 Log(fmt.Sprintf("Successfully processed URL %d/%d", res.index+1, remainingURLs))
316 }
317 case <-time.After(10 * time.Second):
318 return
319 }
320 }
321}
322
273// converts the ordered slice of link slices into a single slice 323// converts the ordered slice of link slices into a single slice
274func flattenResults(results [][]string) []string { 324func flattenResults(results [][]string) []string {
275 var totalLen int 325 var totalLen int