Commit 1f81a2763a
Unsigned
Layout: unified · split
internal/episode_url.go added +350
| @@ -0,0 +1,350 @@ | ||
| 1 | package main | |
| 2 | ||
| 3 | import ( | |
| 4 | "encoding/json" | |
| 5 | "fmt" | |
| 6 | "io/ioutil" | |
| 7 | "net/http" | |
| 8 | "net/url" | |
| 9 | "os" | |
| 10 | "regexp" | |
| 11 | "strings" | |
| 12 | ) | |
| 13 | ||
| 14 | type Config struct { | |
| 15 | Agent string | |
| 16 | AllanimeRefr string | |
| 17 | AllanimeBase string | |
| 18 | AllanimeAPI string | |
| 19 | SubOrDub string | |
| 20 | Mode string | |
| 21 | Quality string | |
| 22 | } | |
| 23 | ||
| 24 | func getDefaultConfig() Config { | |
| 25 | mode := os.Getenv("ANI_CLI_MODE") | |
| 26 | if mode == "" { | |
| 27 | mode = "sub" | |
| 28 | } | |
| 29 | return Config{ | |
| 30 | Agent: "Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:109.0) Gecko/20100101 Firefox/121.0", | |
| 31 | AllanimeRefr: "https://allanime.to", | |
| 32 | AllanimeBase: "allanime.day", | |
| 33 | AllanimeAPI: "https://api.allanime.day", | |
| 34 | SubOrDub: "sub", | |
| 35 | Mode: mode, | |
| 36 | Quality: getEnvOrDefault("ANI_CLI_QUALITY", "best"), | |
| 37 | } | |
| 38 | } | |
| 39 | ||
| 40 | func getEnvOrDefault(key, defaultValue string) string { | |
| 41 | if value := os.Getenv(key); value != "" { | |
| 42 | return value | |
| 43 | } | |
| 44 | return defaultValue | |
| 45 | } | |
| 46 | ||
| 47 | ||
| 48 | func decodeProviderID(encoded string) string { | |
| 49 | // Split the string into pairs of characters (.. equivalent of 'sed s/../&\n/g') | |
| 50 | re := regexp.MustCompile("..") | |
| 51 | pairs := re.FindAllString(encoded, -1) | |
| 52 | ||
| 53 | // Mapping for the replacements | |
| 54 | replacements := map[string]string{ | |
| 55 | "01": "9", "08": "0", "05": "=", "0a": "2", "0b": "3", "0c": "4", "07": "?", | |
| 56 | "00": "8", "5c": "d", "0f": "7", "5e": "f", "17": "/", "54": "l", "09": "1", | |
| 57 | "48": "p", "4f": "w", "0e": "6", "5b": "c", "5d": "e", "0d": "5", "53": "k", | |
| 58 | "1e": "&", "5a": "b", "59": "a", "4a": "r", "4c": "t", "4e": "v", "57": "o", | |
| 59 | "51": "i", | |
| 60 | } | |
| 61 | ||
| 62 | // Perform the replacement equivalent to sed 's/^../.../' | |
| 63 | for i, pair := range pairs { | |
| 64 | if val, exists := replacements[pair]; exists { | |
| 65 | pairs[i] = val | |
| 66 | } | |
| 67 | } | |
| 68 | ||
| 69 | // Join the modified pairs back into a single string | |
| 70 | result := strings.Join(pairs, "") | |
| 71 | ||
| 72 | // Replace "/clock" with "/clock.json" equivalent of sed "s/\/clock/\/clock\.json/" | |
| 73 | result = strings.ReplaceAll(result, "/clock", "/clock.json") | |
| 74 | ||
| 75 | // Print the final result | |
| 76 | return result | |
| 77 | } | |
| 78 | ||
| 79 | func getLinks(config Config, providerID string) ([]string, error) { | |
| 80 | url := fmt.Sprintf("https://%s%s", config.AllanimeBase, providerID) | |
| 81 | req, err := http.NewRequest("GET", url, nil) | |
| 82 | if err != nil { | |
| 83 | return nil, err | |
| 84 | } | |
| 85 | ||
| 86 | req.Header.Set("User-Agent", config.Agent) | |
| 87 | req.Header.Set("Referer", config.AllanimeRefr) | |
| 88 | ||
| 89 | client := &http.Client{} | |
| 90 | resp, err := client.Do(req) | |
| 91 | if err != nil { | |
| 92 | return nil, err | |
| 93 | } | |
| 94 | defer resp.Body.Close() | |
| 95 | ||
| 96 | body, err := ioutil.ReadAll(resp.Body) | |
| 97 | if err != nil { | |
| 98 | return nil, err | |
| 99 | } | |
| 100 | ||
| 101 | bodyStr := string(body) | |
| 102 | var links []string | |
| 103 | ||
| 104 | // Extract links with their qualities | |
| 105 | re := regexp.MustCompile(`link":"([^"]*)".*?"resolutionStr":"([^"]*)"`) | |
| 106 | matches := re.FindAllStringSubmatch(bodyStr, -1) | |
| 107 | for _, match := range matches { | |
| 108 | if len(match) >= 3 { | |
| 109 | link := fmt.Sprintf("%s>%s", match[2], match[1]) | |
| 110 | links = append(links, link) | |
| 111 | } | |
| 112 | } | |
| 113 | ||
| 114 | // Extract m3u8 links | |
| 115 | re = regexp.MustCompile(`hls","url":"([^"]*)".*?"hardsub_lang":"en-US"`) | |
| 116 | matches = re.FindAllStringSubmatch(bodyStr, -1) | |
| 117 | for _, match := range matches { | |
| 118 | if len(match) >= 2 { | |
| 119 | links = append(links, match[1]) | |
| 120 | } | |
| 121 | } | |
| 122 | ||
| 123 | // Process links based on provider type | |
| 124 | var processedLinks []string | |
| 125 | for _, link := range links { | |
| 126 | if strings.Contains(link, "repackager.wixmp.com") { | |
| 127 | parts := strings.Split(link, ">") | |
| 128 | if len(parts) == 2 { | |
| 129 | extractLink := parts[1] | |
| 130 | extractLink = strings.ReplaceAll(extractLink, "repackager.wixmp.com/", "") | |
| 131 | extractLink = regexp.MustCompile(`\.urlset.*`).ReplaceAllString(extractLink, "") | |
| 132 | ||
| 133 | re = regexp.MustCompile(`/,([^/]*),/mp4`) | |
| 134 | matches = re.FindAllStringSubmatch(link, -1) | |
| 135 | for _, match := range matches { | |
| 136 | if len(match) >= 2 { | |
| 137 | quality := match[1] | |
| 138 | newLink := fmt.Sprintf("%s>%s", quality, extractLink) | |
| 139 | newLink = strings.ReplaceAll(newLink, ","+quality, quality) | |
| 140 | processedLinks = append(processedLinks, newLink) | |
| 141 | } | |
| 142 | } | |
| 143 | } | |
| 144 | } else if strings.Contains(link, "vipanicdn") || strings.Contains(link, "anifastcdn") { | |
| 145 | if strings.Contains(link, "original.m3u") { | |
| 146 | processedLinks = append(processedLinks, link) | |
| 147 | } else { | |
| 148 | parts := strings.Split(link, ">") | |
| 149 | if len(parts) > 0 { | |
| 150 | extractLink := parts[0] | |
| 151 | relativeLink := regexp.MustCompile(`[^/]*$`).ReplaceAllString(extractLink, "") | |
| 152 | ||
| 153 | // Fetch m3u8 content | |
| 154 | req, err := http.NewRequest("GET", extractLink, nil) | |
| 155 | if err != nil { | |
| 156 | continue | |
| 157 | } | |
| 158 | req.Header.Set("User-Agent", config.Agent) | |
| 159 | req.Header.Set("Referer", config.AllanimeRefr) | |
| 160 | ||
| 161 | resp, err := client.Do(req) | |
| 162 | if err != nil { | |
| 163 | continue | |
| 164 | } | |
| 165 | m3u8Content, err := ioutil.ReadAll(resp.Body) | |
| 166 | resp.Body.Close() | |
| 167 | if err != nil { | |
| 168 | continue | |
| 169 | } | |
| 170 | ||
| 171 | lines := strings.Split(string(m3u8Content), "\n") | |
| 172 | for i := 0; i < len(lines); i++ { | |
| 173 | line := lines[i] | |
| 174 | if strings.HasPrefix(line, "#") { | |
| 175 | continue | |
| 176 | } | |
| 177 | quality := regexp.MustCompile(`x(\d+)`).ReplaceAllString(line, "$1") | |
| 178 | if quality != "" { | |
| 179 | processedLinks = append(processedLinks, fmt.Sprintf("%sp>%s%s", quality, relativeLink, line)) | |
| 180 | } | |
| 181 | } | |
| 182 | } | |
| 183 | } | |
| 184 | } else if link != "" { | |
| 185 | processedLinks = append(processedLinks, link) | |
| 186 | } | |
| 187 | } | |
| 188 | ||
| 189 | return processedLinks, nil | |
| 190 | } | |
| 191 | ||
| 192 | func generateLink(config Config, resp string, providerType int) ([]string, error) { | |
| 193 | var pattern string | |
| 194 | switch providerType { | |
| 195 | case 1: | |
| 196 | pattern = `/Default :([^\n]+)` | |
| 197 | case 2: | |
| 198 | pattern = `/Sak :([^\n]+)` | |
| 199 | case 3: | |
| 200 | pattern = `/Kir :([^\n]+)` | |
| 201 | case 4: | |
| 202 | pattern = `/S-mp4 :([^\n]+)` | |
| 203 | default: | |
| 204 | pattern = `/Luf-mp4 :([^\n]+)` | |
| 205 | } | |
| 206 | ||
| 207 | re := regexp.MustCompile(pattern) | |
| 208 | matches := re.FindStringSubmatch(resp) | |
| 209 | if len(matches) < 2 { | |
| 210 | return nil, fmt.Errorf("no provider ID found") | |
| 211 | } | |
| 212 | ||
| 213 | providerID := decodeProviderID(matches[1]) | |
| 214 | if strings.Contains(providerID, "/clock") { | |
| 215 | providerID = strings.Replace(providerID, "/clock", "/clock.json", 1) | |
| 216 | } | |
| 217 | ||
| 218 | return getLinks(config, providerID) | |
| 219 | } | |
| 220 | ||
| 221 | func extract_link(provider_id string) map[string]interface{} { | |
| 222 | allanime_base := "https://allanime.day" | |
| 223 | url := allanime_base+provider_id | |
| 224 | client := &http.Client{} | |
| 225 | req, err := http.NewRequest("GET", url, nil) | |
| 226 | var videoData map[string]interface{} | |
| 227 | if err != nil { | |
| 228 | fmt.Println("Error creating request:", err) | |
| 229 | return videoData | |
| 230 | } | |
| 231 | ||
| 232 | // Add the headers | |
| 233 | req.Header.Set("Referer", "https://allanime.to") | |
| 234 | req.Header.Set("User-Agent", "Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:109.0) Gecko/20100101 Firefox/121.0") | |
| 235 | ||
| 236 | // Send the request | |
| 237 | resp, err := client.Do(req) | |
| 238 | if err != nil { | |
| 239 | fmt.Println("Error sending request:", err) | |
| 240 | return videoData | |
| 241 | } | |
| 242 | defer resp.Body.Close() | |
| 243 | ||
| 244 | // Read the response body | |
| 245 | body, err := ioutil.ReadAll(resp.Body) | |
| 246 | if err != nil { | |
| 247 | fmt.Println("Error reading response:", err) | |
| 248 | return videoData | |
| 249 | } | |
| 250 | ||
| 251 | // Parse the JSON response | |
| 252 | err = json.Unmarshal(body, &videoData) | |
| 253 | if err != nil { | |
| 254 | fmt.Println("Error parsing JSON:", err) | |
| 255 | return videoData | |
| 256 | } | |
| 257 | ||
| 258 | // Process the data as needed | |
| 259 | return videoData | |
| 260 | ||
| 261 | } | |
| 262 | ||
| 263 | func getEpisodeURL(config Config, id, epNo string) ([]string, error) { | |
| 264 | query := `query($showId:String!,$translationType:VaildTranslationTypeEnumType!,$episodeString:String!){episode(showId:$showId,translationType:$translationType,episodeString:$episodeString){episodeString sourceUrls}}` | |
| 265 | ||
| 266 | variables := map[string]string{ | |
| 267 | "showId": id, | |
| 268 | "translationType": config.Mode, | |
| 269 | "episodeString": epNo, | |
| 270 | } | |
| 271 | ||
| 272 | variablesJSON, err := json.Marshal(variables) | |
| 273 | if err != nil { | |
| 274 | return nil, err | |
| 275 | } | |
| 276 | ||
| 277 | values := url.Values{} | |
| 278 | values.Set("query", query) | |
| 279 | values.Set("variables", string(variablesJSON)) | |
| 280 | ||
| 281 | reqURL := fmt.Sprintf("%s/api?%s", config.AllanimeAPI, values.Encode()) | |
| 282 | ||
| 283 | client := &http.Client{} | |
| 284 | req, err := http.NewRequest("GET", reqURL, nil) | |
| 285 | if err != nil { | |
| 286 | return nil, err | |
| 287 | } | |
| 288 | ||
| 289 | req.Header.Set("User-Agent", config.Agent) | |
| 290 | req.Header.Set("Referer", config.AllanimeRefr) | |
| 291 | ||
| 292 | resp, err := client.Do(req) | |
| 293 | if err != nil { | |
| 294 | return nil, err | |
| 295 | } | |
| 296 | defer resp.Body.Close() | |
| 297 | ||
| 298 | body, err := ioutil.ReadAll(resp.Body) | |
| 299 | if err != nil { | |
| 300 | return nil, err | |
| 301 | } | |
| 302 | ||
| 303 | // Transform response similar to the bash script | |
| 304 | responseStr := string(body) | |
| 305 | responseStr = strings.ReplaceAll(responseStr, "\\u002F", "/") | |
| 306 | responseStr = strings.ReplaceAll(responseStr, "\\", "") | |
| 307 | responseStr = strings.ReplaceAll(responseStr, "{", "\n") | |
| 308 | responseStr = strings.ReplaceAll(responseStr, "}", "\n") | |
| 309 | ||
| 310 | // fmt.Println(responseStr) | |
| 311 | ||
| 312 | re := regexp.MustCompile(`sourceUrl":"--([^"]*)".*?sourceName":"([^"]*)"`) | |
| 313 | matches := re.FindAllStringSubmatch(responseStr, -1) | |
| 314 | ||
| 315 | if len(matches) == 0 { | |
| 316 | return nil, fmt.Errorf("no source URLs found") | |
| 317 | } | |
| 318 | ||
| 319 | var allLinks []string | |
| 320 | for providerType := 1; providerType <= 5; providerType++ { | |
| 321 | links, err := generateLink(config, responseStr, providerType) | |
| 322 | if err == nil && len(links) > 0 { | |
| 323 | allLinks = append(allLinks, links...) | |
| 324 | } | |
| 325 | } | |
| 326 | ||
| 327 | return allLinks, nil | |
| 328 | } | |
| 329 | ||
| 330 | func main() { | |
| 331 | config := getDefaultConfig() | |
| 332 | ||
| 333 | id := "RezHft5pjutwWcE3B" | |
| 334 | epNo := "12" | |
| 335 | ||
| 336 | links, err := getEpisodeURL(config, id, epNo) | |
| 337 | if err != nil { | |
| 338 | fmt.Printf("Error: %v\n", err) | |
| 339 | if strings.Contains(err.Error(), "no source URLs found") { | |
| 340 | fmt.Println("Episode not released!") | |
| 341 | } | |
| 342 | os.Exit(1) | |
| 343 | } | |
| 344 | ||
| 345 | // Print all found links | |
| 346 | fmt.Println("links:", links) | |
| 347 | for _, link := range links { | |
| 348 | fmt.Println(link) | |
| 349 | } | |
| 350 | } | |
| \ No newline at end of file | ||