internal/providers/anineko/scrape.go
139 lines · 3208 bytes
1package anineko
2
3import (
4 "net/url"
5 "regexp"
6 "strings"
7)
8
9var (
10 dataVideoRE = regexp.MustCompile(`data-video="([^"]+)"`)
11 subtitleRE = regexp.MustCompile(`[?&](?:sub|caption_1|c1_file)=([^&"]+)`)
12 embedSubtitleRE = regexp.MustCompile(`const subtitle = "([^"]+)"`)
13 embedTrackFileRE = regexp.MustCompile(`file:\s*"([^"]+\.(?:vtt|ass|srt))"`)
14)
15
16func extractLangEmbedURLs(html string) map[string][]string {
17 groups := map[string][]string{
18 "hsub": {},
19 "sub": {},
20 "dub": {},
21 }
22 markers := regexp.MustCompile(`data-id="(hsub|sub|dub)"`).FindAllStringSubmatchIndex(html, -1)
23 for i, marker := range markers {
24 if len(marker) < 4 {
25 continue
26 }
27 lang := html[marker[2]:marker[3]]
28 start := marker[1]
29 end := len(html)
30 if i+1 < len(markers) {
31 end = markers[i+1][0]
32 }
33 block := html[start:end]
34 seen := map[string]struct{}{}
35 for _, videoMatch := range dataVideoRE.FindAllStringSubmatch(block, -1) {
36 if len(videoMatch) < 2 {
37 continue
38 }
39 embedURL := strings.TrimSpace(videoMatch[1])
40 if embedURL == "" {
41 continue
42 }
43 if _, ok := seen[embedURL]; ok {
44 continue
45 }
46 seen[embedURL] = struct{}{}
47 groups[lang] = append(groups[lang], embedURL)
48 }
49 }
50 return groups
51}
52
53func isVibeplayerHost(host string) bool {
54 host = strings.ToLower(strings.TrimSpace(host))
55 if host == "" {
56 return false
57 }
58 if strings.Contains(host, "vibeplayer.") || strings.Contains(host, "vivibebe.") {
59 return true
60 }
61 return strings.HasPrefix(host, "vibe") && strings.Contains(host, "be.")
62}
63
64func resolveEmbedHost(embedURL string) string {
65 switch {
66 case strings.Contains(strings.ToLower(hostFromURL(embedURL)), "bibiemb."):
67 return "bibiemb"
68 case isVibeplayerHost(hostFromURL(embedURL)):
69 return "vibeplayer"
70 default:
71 return ""
72 }
73}
74
75func subtitleFromEmbedURL(embedURL string) string {
76 parsed, err := url.Parse(embedURL)
77 if err != nil {
78 return ""
79 }
80 for key, values := range parsed.Query() {
81 switch strings.ToLower(key) {
82 case "sub", "caption_1", "c1_file":
83 if len(values) > 0 {
84 if decoded, err := url.QueryUnescape(values[0]); err == nil {
85 return decoded
86 }
87 return values[0]
88 }
89 }
90 }
91 match := subtitleRE.FindStringSubmatch(embedURL)
92 if len(match) < 2 {
93 return ""
94 }
95 if decoded, err := url.QueryUnescape(match[1]); err == nil {
96 return decoded
97 }
98 return match[1]
99}
100
101func subtitleFromEmbedHTML(html string) string {
102 html = strings.TrimSpace(html)
103 if html == "" {
104 return ""
105 }
106 if match := embedSubtitleRE.FindStringSubmatch(html); len(match) >= 2 {
107 if subtitle := strings.TrimSpace(match[1]); subtitle != "" {
108 return subtitle
109 }
110 }
111 if match := embedTrackFileRE.FindStringSubmatch(html); len(match) >= 2 {
112 return strings.TrimSpace(match[1])
113 }
114 return ""
115}
116
117func resolveSubtitle(embedURL, html string) string {
118 if subtitle := subtitleFromEmbedURL(embedURL); subtitle != "" {
119 return subtitle
120 }
121 return subtitleFromEmbedHTML(html)
122}
123
124func hostFromURL(rawURL string) string {
125 parsed, err := url.Parse(rawURL)
126 if err != nil {
127 return ""
128 }
129 return parsed.Host
130}
131
132func containsString(items []string, target string) bool {
133 for _, item := range items {
134 if item == target {
135 return true
136 }
137 }
138 return false
139}