-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbase.go
More file actions
204 lines (169 loc) · 4.84 KB
/
Copy pathbase.go
File metadata and controls
204 lines (169 loc) · 4.84 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
package mangascraper
import (
"fmt"
"net/http"
"regexp"
"strconv"
"strings"
"time"
"github.com/PuerkitoBio/goquery"
)
// baseSource provides common functionality for all sources
type baseSource struct {
name string
displayName string
baseURL string
config *Config
httpClient *http.Client
}
func newBaseSource(name, displayName, baseURL string, config *Config) *baseSource {
if config == nil {
config = DefaultConfig()
}
httpClient := config.HTTPClient
if httpClient == nil {
httpClient = &http.Client{
Timeout: config.Timeout,
Transport: &http.Transport{
MaxIdleConns: 10,
IdleConnTimeout: 30 * time.Second,
DisableCompression: false,
},
}
}
return &baseSource{
name: name,
displayName: displayName,
baseURL: baseURL,
config: config,
httpClient: httpClient,
}
}
func (s *baseSource) Name() string { return s.name }
func (s *baseSource) DisplayName() string { return s.displayName }
func (s *baseSource) BaseURL() string { return s.baseURL }
// makeRequest makes an HTTP request with appropriate headers
func (s *baseSource) makeRequest(urlStr string) (*http.Response, error) {
var lastErr error
for attempt := 0; attempt <= s.config.MaxRetries; attempt++ {
if attempt > 0 {
time.Sleep(s.config.RetryDelay)
}
req, err := http.NewRequest("GET", urlStr, nil)
if err != nil {
return nil, err
}
req.Header.Set("User-Agent", s.config.UserAgent)
req.Header.Set("Accept", "text/html,application/xhtml+xml,application/xml;q=0.9,image/avif,image/webp,*/*;q=0.8")
req.Header.Set("Accept-Language", "pt-BR,pt;q=0.9,en-US;q=0.8,en;q=0.7")
req.Header.Set("Referer", s.baseURL)
req.Header.Set("Cache-Control", "no-cache")
resp, err := s.httpClient.Do(req)
if err != nil {
lastErr = err
continue
}
if resp.StatusCode >= 200 && resp.StatusCode < 300 {
return resp, nil
}
resp.Body.Close()
lastErr = fmt.Errorf("HTTP %d: %s", resp.StatusCode, resp.Status)
}
return nil, fmt.Errorf("max retries exceeded: %v", lastErr)
}
// fetchDocument fetches and parses HTML document
func (s *baseSource) fetchDocument(urlStr string) (*goquery.Document, error) {
resp, err := s.makeRequest(urlStr)
if err != nil {
return nil, err
}
defer resp.Body.Close()
return goquery.NewDocumentFromReader(resp.Body)
}
// Helper functions
// normalizeURL ensures URL is absolute
func normalizeURL(urlStr, baseURL string) string {
urlStr = strings.TrimSpace(urlStr)
if urlStr == "" {
return ""
}
if strings.HasPrefix(urlStr, "http://") || strings.HasPrefix(urlStr, "https://") {
return urlStr
}
if strings.HasPrefix(urlStr, "//") {
return "https:" + urlStr
}
if strings.HasPrefix(urlStr, "/") {
return strings.TrimSuffix(baseURL, "/") + urlStr
}
return baseURL + "/" + urlStr
}
// normalizeImageURL normalizes image URLs
func normalizeImageURL(imgURL, baseURL string) string {
if imgURL == "" {
return ""
}
imgURL = strings.TrimSpace(imgURL)
// Remove data: URLs
if strings.HasPrefix(imgURL, "data:") {
return ""
}
return normalizeURL(imgURL, baseURL)
}
// getImageSrc extracts image source from multiple attributes
func getImageSrc(img *goquery.Selection) string {
// Priority: data-src, data-lazy-src, src
for _, attr := range []string{"data-src", "data-lazy-src", "data-original", "src"} {
if src, exists := img.Attr(attr); exists {
src = strings.TrimSpace(src)
if src != "" && !strings.HasPrefix(src, "data:") {
return src
}
}
}
return ""
}
// extractMangaID extracts manga ID from URL
func extractMangaID(mangaURL string) string {
// Remove trailing slash and query params
urlStr := strings.Split(mangaURL, "?")[0]
urlStr = strings.TrimSuffix(urlStr, "/")
// Get last part of path
parts := strings.Split(urlStr, "/")
for i := len(parts) - 1; i >= 0; i-- {
part := parts[i]
if part != "" && part != "manga" {
return part
}
}
return ""
}
// parseChapterNumber extracts float number from chapter string
func parseChapterNumber(chapterStr string) float64 {
// Remove common prefixes
chapterStr = strings.TrimSpace(chapterStr)
chapterStr = strings.TrimPrefix(strings.ToLower(chapterStr), "capítulo")
chapterStr = strings.TrimPrefix(strings.ToLower(chapterStr), "capitulo")
chapterStr = strings.TrimPrefix(strings.ToLower(chapterStr), "cap.")
chapterStr = strings.TrimPrefix(strings.ToLower(chapterStr), "cap")
chapterStr = strings.TrimPrefix(chapterStr, ".")
chapterStr = strings.TrimSpace(chapterStr)
// Extract number
re := regexp.MustCompile(`(\d+(?:\.\d+)?)`)
matches := re.FindStringSubmatch(chapterStr)
if len(matches) > 1 {
if num, err := strconv.ParseFloat(matches[1], 64); err == nil {
return num
}
}
return 0
}
// containsString checks if slice contains a string
func containsString(slice []string, s string) bool {
for _, item := range slice {
if item == s {
return true
}
}
return false
}