-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscraper.go
More file actions
402 lines (327 loc) · 8.99 KB
/
Copy pathscraper.go
File metadata and controls
402 lines (327 loc) · 8.99 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
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
package mangascraper
import (
"fmt"
"strings"
"sync"
)
// Scraper is the main entry point for the manga scraper library
type Scraper struct {
sources map[string]Source
order []string
cache *Cache
config *Config
mu sync.RWMutex
}
// New creates a new Scraper with default sources and configuration
func New() *Scraper {
return NewWithConfig(nil)
}
// NewWithConfig creates a new Scraper with custom configuration
func NewWithConfig(config *Config) *Scraper {
if config == nil {
config = DefaultConfig()
}
var cache *Cache
if config.EnableCache {
cache = NewCache(config.CacheDir)
} else {
cache = NewDisabledCache()
}
s := &Scraper{
sources: make(map[string]Source),
order: []string{},
cache: cache,
config: config,
}
// Register default sources
s.RegisterSource(NewMangaLivreToSource(config))
s.RegisterSource(NewMangaLivreBlogSource(config))
return s
}
// RegisterSource adds a new source to the scraper
func (s *Scraper) RegisterSource(source Source) {
s.mu.Lock()
defer s.mu.Unlock()
name := source.Name()
s.sources[name] = source
s.order = append(s.order, name)
}
// GetSources returns the list of available source names
func (s *Scraper) GetSources() []string {
s.mu.RLock()
defer s.mu.RUnlock()
return append([]string{}, s.order...)
}
// GetSourceInfo returns information about all available sources
func (s *Scraper) GetSourceInfo() []SourceInfo {
s.mu.RLock()
defer s.mu.RUnlock()
infos := make([]SourceInfo, 0, len(s.order))
for _, name := range s.order {
source := s.sources[name]
infos = append(infos, SourceInfo{
Name: source.Name(),
DisplayName: source.DisplayName(),
BaseURL: source.BaseURL(),
Language: "pt-BR",
NSFW: false,
})
}
return infos
}
// GetSource returns a specific source by name
func (s *Scraper) GetSource(name string) (Source, bool) {
s.mu.RLock()
defer s.mu.RUnlock()
source, ok := s.sources[name]
return source, ok
}
// DetectSourceFromURL determines the source based on URL
func (s *Scraper) DetectSourceFromURL(url string) string {
lowerURL := strings.ToLower(url)
if strings.Contains(lowerURL, "mangalivre.blog") {
return "mangalivre.blog"
}
if strings.Contains(lowerURL, "mangalivre.to") {
return "mangalivre.to"
}
// Default to first source
if len(s.order) > 0 {
return s.order[0]
}
return ""
}
// GetAllMangas returns mangas from a specific source with pagination
func (s *Scraper) GetAllMangas(sourceName string, page int) ([]Manga, int, error) {
source, ok := s.GetSource(sourceName)
if !ok {
return nil, 0, fmt.Errorf("source not found: %s", sourceName)
}
// Check cache
cacheKey := fmt.Sprintf("mangas:%s:page:%d", sourceName, page)
if mangas, ok := s.cache.GetMangas(cacheKey); ok {
return mangas, 0, nil // totalPages not cached, but that's OK
}
mangas, totalPages, err := source.GetAllMangas(page)
if err != nil {
return nil, 0, err
}
// Store in cache
s.cache.SetMangas(cacheKey, mangas, TTLMangaList)
return mangas, totalPages, nil
}
// GetAllMangasFromAllSources returns mangas from all sources
func (s *Scraper) GetAllMangasFromAllSources(page int) ([]Manga, int, error) {
s.mu.RLock()
sources := s.order
s.mu.RUnlock()
var allMangas []Manga
maxPages := 0
var lastErr error
var wg sync.WaitGroup
var mu sync.Mutex
for _, sourceName := range sources {
source, ok := s.GetSource(sourceName)
if !ok {
continue
}
wg.Add(1)
go func(sName string, src Source) {
defer wg.Done()
mangas, totalPages, err := src.GetAllMangas(page)
if err != nil {
mu.Lock()
lastErr = err
mu.Unlock()
return
}
mu.Lock()
allMangas = append(allMangas, mangas...)
if totalPages > maxPages {
maxPages = totalPages
}
mu.Unlock()
}(sourceName, source)
}
wg.Wait()
if len(allMangas) == 0 && lastErr != nil {
return nil, 0, lastErr
}
return allMangas, maxPages, nil
}
// SearchManga searches for mangas in a specific source
func (s *Scraper) SearchManga(sourceName, query string) ([]Manga, error) {
source, ok := s.GetSource(sourceName)
if !ok {
return nil, fmt.Errorf("source not found: %s", sourceName)
}
// Check cache
cacheKey := fmt.Sprintf("search:%s:%s", sourceName, query)
if mangas, ok := s.cache.GetMangas(cacheKey); ok {
return mangas, nil
}
mangas, err := source.SearchManga(query)
if err != nil {
return nil, err
}
// Store in cache
s.cache.SetMangas(cacheKey, mangas, TTLMangaSearch)
return mangas, nil
}
// SearchAllSources searches for mangas across all sources
func (s *Scraper) SearchAllSources(query string) ([]SearchResult, error) {
s.mu.RLock()
sources := s.order
s.mu.RUnlock()
results := make([]SearchResult, 0, len(sources))
var wg sync.WaitGroup
var mu sync.Mutex
for _, sourceName := range sources {
source, ok := s.GetSource(sourceName)
if !ok {
continue
}
wg.Add(1)
go func(sName string, src Source) {
defer wg.Done()
mangas, err := src.SearchManga(query)
mu.Lock()
results = append(results, SearchResult{
Mangas: mangas,
Source: sName,
Error: err,
})
mu.Unlock()
}(sourceName, source)
}
wg.Wait()
return results, nil
}
// GetMangaDetails returns detailed information about a manga
// The source is automatically detected from the URL
func (s *Scraper) GetMangaDetails(mangaURL string) (*Manga, error) {
sourceName := s.DetectSourceFromURL(mangaURL)
source, ok := s.GetSource(sourceName)
if !ok {
return nil, fmt.Errorf("source not found for URL: %s", mangaURL)
}
// Check cache
cacheKey := fmt.Sprintf("details:%s", mangaURL)
if mangas, ok := s.cache.GetMangas(cacheKey); ok && len(mangas) > 0 {
return &mangas[0], nil
}
manga, err := source.GetMangaDetails(mangaURL)
if err != nil {
return nil, err
}
// Store in cache
s.cache.SetMangas(cacheKey, []Manga{*manga}, TTLMangaDetails)
return manga, nil
}
// GetChapters returns all chapters of a manga
// The source is automatically detected from the URL
func (s *Scraper) GetChapters(mangaURL string) ([]Chapter, error) {
sourceName := s.DetectSourceFromURL(mangaURL)
source, ok := s.GetSource(sourceName)
if !ok {
return nil, fmt.Errorf("source not found for URL: %s", mangaURL)
}
// Check cache
cacheKey := fmt.Sprintf("chapters:%s", mangaURL)
if chapters, ok := s.cache.GetChapters(cacheKey); ok {
return chapters, nil
}
chapters, err := source.GetChapters(mangaURL)
if err != nil {
return nil, err
}
// Store in cache
s.cache.SetChapters(cacheKey, chapters, TTLMangaChapters)
return chapters, nil
}
// GetChapterPages returns all pages/images of a chapter
// The source is automatically detected from the URL
func (s *Scraper) GetChapterPages(chapterURL string) ([]Page, error) {
sourceName := s.DetectSourceFromURL(chapterURL)
source, ok := s.GetSource(sourceName)
if !ok {
return nil, fmt.Errorf("source not found for URL: %s", chapterURL)
}
// Check cache
cacheKey := fmt.Sprintf("pages:%s", chapterURL)
if pages, ok := s.cache.GetPages(cacheKey); ok {
return pages, nil
}
pages, err := source.GetChapterPages(chapterURL)
if err != nil {
return nil, err
}
// Store in cache
s.cache.SetPages(cacheKey, pages, TTLMangaPages)
return pages, nil
}
// GetPopularMangas returns popular mangas from a specific source
func (s *Scraper) GetPopularMangas(sourceName string) ([]Manga, error) {
source, ok := s.GetSource(sourceName)
if !ok {
return nil, fmt.Errorf("source not found: %s", sourceName)
}
return source.GetPopularMangas()
}
// GetLatestUpdates returns recently updated mangas from a specific source
func (s *Scraper) GetLatestUpdates(sourceName string) ([]Manga, error) {
source, ok := s.GetSource(sourceName)
if !ok {
return nil, fmt.Errorf("source not found: %s", sourceName)
}
return source.GetLatestUpdates()
}
// GetMangasByGenre returns mangas filtered by genre from a specific source
func (s *Scraper) GetMangasByGenre(sourceName, genre string) ([]Manga, error) {
source, ok := s.GetSource(sourceName)
if !ok {
return nil, fmt.Errorf("source not found: %s", sourceName)
}
return source.GetMangasByGenre(genre)
}
// GetGenres returns available genres from a specific source
func (s *Scraper) GetGenres(sourceName string) ([]string, error) {
source, ok := s.GetSource(sourceName)
if !ok {
return nil, fmt.Errorf("source not found: %s", sourceName)
}
return source.GetGenres()
}
// GetAllGenres returns genres from all sources (deduplicated)
func (s *Scraper) GetAllGenres() ([]string, error) {
s.mu.RLock()
sources := s.order
s.mu.RUnlock()
seenGenres := make(map[string]bool)
var allGenres []string
for _, sourceName := range sources {
source, ok := s.GetSource(sourceName)
if !ok {
continue
}
genres, err := source.GetGenres()
if err != nil {
continue
}
for _, genre := range genres {
if !seenGenres[genre] {
seenGenres[genre] = true
allGenres = append(allGenres, genre)
}
}
}
return allGenres, nil
}
// ClearCache clears all cached data
func (s *Scraper) ClearCache() {
s.cache.Clear()
}
// GetCache returns the cache instance for advanced usage
func (s *Scraper) GetCache() *Cache {
return s.cache
}