b199bad30d
- Add internal/httpclient/flare package for Cloudflare-protected sites - Update 7 bases (madara, zmanga, mangaworld, mangathemesia, mangareader, libgroup, liliana) to use flare client - Remove unused internal/config/source.go
190 lines
5.9 KiB
Go
Executable File
190 lines
5.9 KiB
Go
Executable File
// Package zmanga implements the ZManga base.
|
|
// GET {base}/advanced-search/page/{n}/?order=popular; FlareSolverr required.
|
|
package zmanga
|
|
|
|
import (
|
|
"context"
|
|
"fmt"
|
|
"net/http"
|
|
"strings"
|
|
|
|
"github.com/PuerkitoBio/goquery"
|
|
|
|
"goyomi/internal/httpclient/flare"
|
|
"goyomi/internal/source"
|
|
"goyomi/sources/base/util"
|
|
)
|
|
|
|
type Config struct {
|
|
Name string
|
|
BaseURL string
|
|
Lang string
|
|
}
|
|
|
|
type Source struct {
|
|
cfg Config
|
|
client *flare.Client
|
|
id int64
|
|
}
|
|
|
|
func New(cfg Config) *Source {
|
|
opts := []flare.Option{flare.WithRateLimit(1, 2)}
|
|
c := flare.NewClient(opts...)
|
|
return &Source{cfg: cfg, client: c, id: source.GenerateSourceID(cfg.Name, cfg.Lang)}
|
|
}
|
|
|
|
func (s *Source) ID() int64 { return s.id }
|
|
func (s *Source) Name() string { return s.cfg.Name }
|
|
func (s *Source) Lang() string { return s.cfg.Lang }
|
|
func (s *Source) SupportsLatest() bool { return true }
|
|
|
|
func (s *Source) base() string { return strings.TrimRight(s.cfg.BaseURL, "/") }
|
|
|
|
func (s *Source) get(ctx context.Context, rawURL string) (*goquery.Document, error) {
|
|
req, err := http.NewRequestWithContext(ctx, http.MethodGet, rawURL, nil)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
req.Header.Set("Referer", s.cfg.BaseURL+"/")
|
|
resp, err := s.client.Do(req)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
defer resp.Body.Close()
|
|
if resp.StatusCode != http.StatusOK {
|
|
return nil, fmt.Errorf("zmanga: HTTP %d", resp.StatusCode)
|
|
}
|
|
return goquery.NewDocumentFromReader(resp.Body)
|
|
}
|
|
|
|
func (s *Source) pageSegment(page int) string {
|
|
if page > 1 {
|
|
return fmt.Sprintf("page/%d/", page)
|
|
}
|
|
return ""
|
|
}
|
|
|
|
func (s *Source) parseMangaList(doc *goquery.Document) source.MangasPage {
|
|
var mangas []source.SManga
|
|
doc.Find("div.flexbox2-item").Each(func(_ int, el *goquery.Selection) {
|
|
m := source.SManga{}
|
|
el.Find("div.flexbox2-content a").First().Each(func(_ int, a *goquery.Selection) {
|
|
m.URL, _ = a.Attr("href")
|
|
})
|
|
el.Find("div.flexbox2-title > span").First().Each(func(_ int, e *goquery.Selection) {
|
|
m.Title = strings.TrimSpace(e.Text())
|
|
})
|
|
m.ThumbnailURL = imgAttr(el.Find("img").First(), s.cfg.BaseURL)
|
|
if m.URL != "" {
|
|
mangas = append(mangas, m)
|
|
}
|
|
})
|
|
hasNext := doc.Find(".pagination .next, .wp-pagenavi a.nextpostslink").Length() > 0
|
|
return source.MangasPage{Mangas: mangas, HasNextPage: hasNext}
|
|
}
|
|
|
|
func (s *Source) GetPopularManga(page int) (source.MangasPage, error) {
|
|
doc, err := s.get(context.Background(), fmt.Sprintf("%s/advanced-search/%s?order=popular", s.base(), s.pageSegment(page)))
|
|
if err != nil {
|
|
return source.MangasPage{}, err
|
|
}
|
|
return s.parseMangaList(doc), nil
|
|
}
|
|
|
|
func (s *Source) GetLatestUpdates(page int) (source.MangasPage, error) {
|
|
doc, err := s.get(context.Background(), fmt.Sprintf("%s/advanced-search/%s?order=update", s.base(), s.pageSegment(page)))
|
|
if err != nil {
|
|
return source.MangasPage{}, err
|
|
}
|
|
return s.parseMangaList(doc), nil
|
|
}
|
|
|
|
func (s *Source) GetSearchManga(page int, query string, filters []source.Filter) (source.MangasPage, error) {
|
|
doc, err := s.get(context.Background(), fmt.Sprintf("%s/advanced-search/%s?s=%s", s.base(), s.pageSegment(page), query))
|
|
if err != nil {
|
|
return source.MangasPage{}, err
|
|
}
|
|
return s.parseMangaList(doc), nil
|
|
}
|
|
|
|
func (s *Source) GetMangaDetails(manga source.SManga) (source.SManga, error) {
|
|
doc, err := s.get(context.Background(), util.AbsURL(s.cfg.BaseURL, manga.URL))
|
|
if err != nil {
|
|
return manga, err
|
|
}
|
|
result := source.SManga{URL: manga.URL}
|
|
result.ThumbnailURL = imgAttr(doc.Find("div.series-thumb img").First(), s.cfg.BaseURL)
|
|
doc.Find(".series-infolist li").Each(func(_ int, el *goquery.Selection) {
|
|
label := strings.ToLower(strings.TrimSpace(el.Find("b, strong").Text()))
|
|
val := strings.TrimSpace(el.Find("span, a").First().Text())
|
|
switch {
|
|
case strings.Contains(label, "author"):
|
|
result.Author = val
|
|
case strings.Contains(label, "artist"):
|
|
result.Artist = val
|
|
}
|
|
})
|
|
doc.Find(".series-infoz .status").First().Each(func(_ int, el *goquery.Selection) {
|
|
result.Status = util.StatusFromString(el.Text())
|
|
})
|
|
result.Description = strings.TrimSpace(doc.Find("div.series-synops").Text())
|
|
var genres []string
|
|
doc.Find("div.series-genres a").Each(func(_ int, el *goquery.Selection) {
|
|
if t := strings.TrimSpace(el.Text()); t != "" {
|
|
genres = append(genres, t)
|
|
}
|
|
})
|
|
result.Genre = strings.Join(genres, ", ")
|
|
if t := strings.TrimSpace(doc.Find(".series-title h1, .series-titlex h1").Text()); t != "" {
|
|
result.Title = t
|
|
} else {
|
|
result.Title = manga.Title
|
|
}
|
|
return result, nil
|
|
}
|
|
|
|
func (s *Source) GetChapterList(manga source.SManga) ([]source.SChapter, error) {
|
|
doc, err := s.get(context.Background(), util.AbsURL(s.cfg.BaseURL, manga.URL))
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
var chapters []source.SChapter
|
|
doc.Find("ul.series-chapterlist div.flexch-infoz a").Each(func(_ int, a *goquery.Selection) {
|
|
ch := source.SChapter{Name: strings.TrimSpace(a.Text())}
|
|
ch.URL, _ = a.Attr("href")
|
|
a.Closest("li").Find("span.date, .chapterdate").First().Each(func(_ int, el *goquery.Selection) {
|
|
ch.DateUpload = util.ParseRelativeDate(el.Text())
|
|
})
|
|
if ch.URL != "" {
|
|
chapters = append(chapters, ch)
|
|
}
|
|
})
|
|
return chapters, nil
|
|
}
|
|
|
|
func (s *Source) GetPageList(chapter source.SChapter) ([]source.Page, error) {
|
|
doc, err := s.get(context.Background(), util.AbsURL(s.cfg.BaseURL, chapter.URL))
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
var pages []source.Page
|
|
doc.Find("div.reader-area img").Each(func(i int, img *goquery.Selection) {
|
|
if u := imgAttr(img, s.cfg.BaseURL); u != "" {
|
|
pages = append(pages, source.Page{Index: i, ImageURL: u})
|
|
}
|
|
})
|
|
return pages, nil
|
|
}
|
|
|
|
func (s *Source) GetImageURL(page source.Page) (string, error) { return page.ImageURL, nil }
|
|
func (s *Source) GetFilterList() []source.Filter { return nil }
|
|
|
|
func imgAttr(img *goquery.Selection, baseURL string) string {
|
|
for _, attr := range []string{"data-lazy-src", "data-src", "data-cfsrc", "src"} {
|
|
if v, ok := img.Attr(attr); ok && v != "" && !strings.HasPrefix(v, "data:") {
|
|
return util.AbsURL(baseURL, v)
|
|
}
|
|
}
|
|
return ""
|
|
}
|