Commit f6b128db23
Unsigned
Layout: unified · split
internal/episode_url.go +105 −11
| @@ -2,6 +2,11 @@ package internal | |||
| 2 | 2 | ||
| 3 | import ( | 3 | import ( |
| 4 | "bytes" | 4 | "bytes" |
| 5 | "crypto/aes" | ||
| 6 | "crypto/cipher" | ||
| 7 | "crypto/sha256" | ||
| 8 | "encoding/base64" | ||
| 9 | "encoding/binary" | ||
| 5 | "encoding/json" | 10 | "encoding/json" |
| 6 | "fmt" | 11 | "fmt" |
| 7 | "io" | 12 | "io" |
| @@ -14,9 +19,12 @@ import ( | |||
| 14 | 19 | ||
| 15 | type allanimeResponse struct { | 20 | type allanimeResponse struct { |
| 16 | Data struct { | 21 | Data struct { |
| 17 | Episode struct { | 22 | M string `json:"_m"` |
| 23 | Tobeparsed string `json:"tobeparsed"` | ||
| 24 | Episode struct { | ||
| 18 | SourceUrls []struct { | 25 | SourceUrls []struct { |
| 19 | SourceUrl string `json:"sourceUrl"` | 26 | SourceUrl string `json:"sourceUrl"` |
| 27 | SourceName string `json:"sourceName"` | ||
| 20 | } `json:"sourceUrls"` | 28 | } `json:"sourceUrls"` |
| 21 | } `json:"episode"` | 29 | } `json:"episode"` |
| 22 | } `json:"data"` | 30 | } `json:"data"` |
| @@ -28,6 +36,58 @@ type result struct { | |||
| 28 | err error | 36 | err error |
| 29 | } | 37 | } |
| 30 | 38 | ||
| 39 | func decodeTobeparsed(blob string) string { | ||
| 40 | key := []byte("SimtVuagFbGR2K7P") | ||
| 41 | hash := sha256.Sum256(key) | ||
| 42 | |||
| 43 | data, err := base64.StdEncoding.DecodeString(blob) | ||
| 44 | if err != nil { | ||
| 45 | Log(fmt.Sprint("Error decoding base64:", err)) | ||
| 46 | return "" | ||
| 47 | } | ||
| 48 | |||
| 49 | if len(data) < 28 { | ||
| 50 | Log(fmt.Sprint("Data too short to contain IV and ciphertext")) | ||
| 51 | return "" | ||
| 52 | } | ||
| 53 | |||
| 54 | iv := data[:12] | ||
| 55 | ct := data[12:] | ||
| 56 | |||
| 57 | ctrIV := make([]byte, 16) | ||
| 58 | copy(ctrIV, iv) | ||
| 59 | binary.BigEndian.PutUint32(ctrIV[12:], uint32(2)) | ||
| 60 | |||
| 61 | block, err := aes.NewCipher(hash[:]) | ||
| 62 | if err != nil { | ||
| 63 | Log(fmt.Sprint("Error creating cipher:", err)) | ||
| 64 | return "" | ||
| 65 | } | ||
| 66 | |||
| 67 | stream := cipher.NewCTR(block, ctrIV) | ||
| 68 | plain := make([]byte, len(ct)) | ||
| 69 | stream.XORKeyStream(plain, ct) | ||
| 70 | |||
| 71 | result := string(plain) | ||
| 72 | result = strings.ReplaceAll(result, "{", "\n") | ||
| 73 | result = strings.ReplaceAll(result, "}", "\n") | ||
| 74 | |||
| 75 | re := regexp.MustCompile(`"sourceUrl":"--([^"]+)".*"sourceName":"([^"]+)"`) | ||
| 76 | matches := re.FindAllStringSubmatch(result, -1) | ||
| 77 | |||
| 78 | var sb strings.Builder | ||
| 79 | for _, match := range matches { | ||
| 80 | if len(match) == 3 { | ||
| 81 | sb.WriteString(match[2]) | ||
| 82 | sb.WriteString(" :") | ||
| 83 | sb.WriteString(match[1]) | ||
| 84 | sb.WriteString("\n") | ||
| 85 | } | ||
| 86 | } | ||
| 87 | |||
| 88 | return sb.String() | ||
| 89 | } | ||
| 90 | |||
| 31 | func decodeProviderID(encoded string) string { | 91 | func decodeProviderID(encoded string) string { |
| 32 | // Split the string into pairs of characters (.. equivalent of 'sed s/../&\n/g') | 92 | // Split the string into pairs of characters (.. equivalent of 'sed s/../&\n/g') |
| 33 | re := regexp.MustCompile("..") | 93 | re := regexp.MustCompile("..") |
| @@ -204,7 +264,7 @@ func getEpisodeURLForMode(id, mode string, epNo int) ([]string, error) { | |||
| 204 | 264 | ||
| 205 | body, err := io.ReadAll(resp.Body) | 265 | body, err := io.ReadAll(resp.Body) |
| 206 | if err != nil { | 266 | if err != nil { |
| 207 | return nil, fmt.Errorf("failed to read response: %w", err) | 267 | return nil, fmt.Errorf("failed to read response body: %w", err) |
| 208 | } | 268 | } |
| 209 | 269 | ||
| 210 | var response allanimeResponse | 270 | var response allanimeResponse |
| @@ -214,12 +274,47 @@ func getEpisodeURLForMode(id, mode string, epNo int) ([]string, error) { | |||
| 214 | return nil, err | 274 | return nil, err |
| 215 | } | 275 | } |
| 216 | 276 | ||
| 217 | // Pre-count valid URLs and create slice to preserve order | 277 | if response.Data.Tobeparsed != "" { |
| 278 | Log("Found tobeparsed field, using decoded response") | ||
| 279 | decoded := decodeTobeparsed(response.Data.Tobeparsed) | ||
| 280 | lines := strings.Split(strings.TrimSpace(decoded), "\n") | ||
| 281 | var parsedURLs []struct { | ||
| 282 | SourceName string | ||
| 283 | SourceUrl string | ||
| 284 | } | ||
| 285 | for _, line := range lines { | ||
| 286 | if parts := strings.Split(line, " :"); len(parts) == 2 { | ||
| 287 | parsedURLs = append(parsedURLs, struct { | ||
| 288 | SourceName string | ||
| 289 | SourceUrl string | ||
| 290 | }{SourceName: parts[0], SourceUrl: "--" + parts[1]}) | ||
| 291 | } | ||
| 292 | } | ||
| 293 | |||
| 294 | validURLs := make([]string, 0) | ||
| 295 | for _, url := range parsedURLs { | ||
| 296 | validURLs = append(validURLs, url.SourceUrl) | ||
| 297 | } | ||
| 298 | |||
| 299 | if len(validURLs) == 0 { | ||
| 300 | return nil, fmt.Errorf("no valid source URLs found in decoded tobeparsed") | ||
| 301 | } | ||
| 302 | |||
| 303 | return getLinksFromURLs(validURLs) | ||
| 304 | } | ||
| 305 | |||
| 306 | return getLinksFromSourceUrls(response.Data.Episode.SourceUrls) | ||
| 307 | } | ||
| 308 | |||
| 309 | func getLinksFromSourceUrls(sourceUrls []struct { | ||
| 310 | SourceUrl string `json:"sourceUrl"` | ||
| 311 | SourceName string `json:"sourceName"` | ||
| 312 | }) ([]string, error) { | ||
| 218 | validURLs := make([]string, 0) | 313 | validURLs := make([]string, 0) |
| 219 | highestPriority := -1 | 314 | highestPriority := -1 |
| 220 | var highestPriorityURL string | 315 | var highestPriorityURL string |
| 221 | 316 | ||
| 222 | for _, url := range response.Data.Episode.SourceUrls { | 317 | for _, url := range sourceUrls { |
| 223 | if len(url.SourceUrl) > 2 && unicode.IsDigit(rune(url.SourceUrl[2])) { | 318 | if len(url.SourceUrl) > 2 && unicode.IsDigit(rune(url.SourceUrl[2])) { |
| 224 | decodedURL := decodeProviderID(url.SourceUrl[2:]) | 319 | decodedURL := decodeProviderID(url.SourceUrl[2:]) |
| 225 | if strings.Contains(decodedURL, LinkPriorities[0]) { | 320 | if strings.Contains(decodedURL, LinkPriorities[0]) { |
| @@ -234,7 +329,6 @@ func getEpisodeURLForMode(id, mode string, epNo int) ([]string, error) { | |||
| 234 | } | 329 | } |
| 235 | } | 330 | } |
| 236 | 331 | ||
| 237 | // If we found a highest priority URL, use only that | ||
| 238 | if highestPriorityURL != "" { | 332 | if highestPriorityURL != "" { |
| 239 | validURLs = []string{highestPriorityURL} | 333 | validURLs = []string{highestPriorityURL} |
| 240 | } | 334 | } |
| @@ -243,22 +337,22 @@ func getEpisodeURLForMode(id, mode string, epNo int) ([]string, error) { | |||
| 243 | return nil, fmt.Errorf("no valid source URLs found in response") | 337 | return nil, fmt.Errorf("no valid source URLs found in response") |
| 244 | } | 338 | } |
| 245 | 339 | ||
| 246 | // Create channels for results and a slice to store ordered results | 340 | return getLinksFromURLs(validURLs) |
| 341 | } | ||
| 342 | |||
| 343 | func getLinksFromURLs(validURLs []string) ([]string, error) { | ||
| 247 | results := make(chan result, len(validURLs)) | 344 | results := make(chan result, len(validURLs)) |
| 248 | orderedResults := make([][]string, len(validURLs)) | 345 | orderedResults := make([][]string, len(validURLs)) |
| 249 | 346 | ||
| 250 | // Add a channel for high priority links | ||
| 251 | highPriorityLink := make(chan []string, 1) | 347 | highPriorityLink := make(chan []string, 1) |
| 252 | 348 | ||
| 253 | // Create rate limiter | ||
| 254 | rateLimiter := time.NewTicker(50 * time.Millisecond) | 349 | rateLimiter := time.NewTicker(50 * time.Millisecond) |
| 255 | defer rateLimiter.Stop() | 350 | defer rateLimiter.Stop() |
| 256 | 351 | ||
| 257 | // Launch goroutines | ||
| 258 | remainingURLs := len(validURLs) | 352 | remainingURLs := len(validURLs) |
| 259 | for i, sourceUrl := range validURLs { | 353 | for i, sourceUrl := range validURLs { |
| 260 | go func(idx int, url string) { | 354 | go func(idx int, url string) { |
| 261 | <-rateLimiter.C // Rate limit the requests | 355 | <-rateLimiter.C |
| 262 | 356 | ||
| 263 | decodedProviderID := decodeProviderID(url[2:]) | 357 | decodedProviderID := decodeProviderID(url[2:]) |
| 264 | Log(fmt.Sprintf("Processing URL %d/%d with provider ID: %s", idx+1, len(validURLs), decodedProviderID)) | 358 | Log(fmt.Sprintf("Processing URL %d/%d with provider ID: %s", idx+1, len(validURLs), decodedProviderID)) |