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
203 lines
6.5 KiB
Go
Executable File
203 lines
6.5 KiB
Go
Executable File
// Package liliana implements the Liliana manga base.
|
|
// GET {base}/ranking/week/{page} for popular; FlareSolverr required.
|
|
package liliana
|
|
|
|
import (
|
|
"context"
|
|
"fmt"
|
|
"net/http"
|
|
"regexp"
|
|
"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)}
|
|
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("liliana: HTTP %d", resp.StatusCode)
|
|
}
|
|
return goquery.NewDocumentFromReader(resp.Body)
|
|
}
|
|
|
|
func (s *Source) parseMangaList(doc *goquery.Document) source.MangasPage {
|
|
var mangas []source.SManga
|
|
doc.Find("div#main div.grid > div, div.listmanga div.col").Each(func(_ int, el *goquery.Selection) {
|
|
m := source.SManga{}
|
|
el.Find(".text-center a, .manga-title a").First().Each(func(_ int, a *goquery.Selection) {
|
|
m.URL, _ = a.Attr("href")
|
|
m.Title = strings.TrimSpace(a.Text())
|
|
})
|
|
m.ThumbnailURL = imgAttr(el.Find("img").First(), s.cfg.BaseURL)
|
|
if m.URL != "" {
|
|
mangas = append(mangas, m)
|
|
}
|
|
})
|
|
hasNext := doc.Find(".blog-pager > span.pagecurrent + span, .pagination .next").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/ranking/week/%d", s.base(), 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/all-manga/%d/?sort=last_update&status=0", s.base(), 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/all-manga/%d/?s=%s", s.base(), 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.Title = strings.TrimSpace(doc.Find(".a2 header h1").Text())
|
|
if result.Title == "" {
|
|
result.Title = manga.Title
|
|
}
|
|
result.ThumbnailURL = imgAttr(doc.Find(".a1 > figure img").First(), s.cfg.BaseURL)
|
|
result.Description = strings.TrimSpace(doc.Find("div#syn-target").Text())
|
|
result.Author = strings.TrimSpace(doc.Find("div.y6x11p i.fas.fa-user + span.dt").Text())
|
|
result.Status = util.StatusFromString(doc.Find("div.y6x11p i.fas.fa-rss + span.dt").Text())
|
|
var genres []string
|
|
doc.Find("a[rel='tag'].label").Each(func(_ int, el *goquery.Selection) {
|
|
if t := strings.TrimSpace(el.Text()); t != "" {
|
|
genres = append(genres, t)
|
|
}
|
|
})
|
|
result.Genre = strings.Join(genres, ", ")
|
|
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 > li.chapter").Each(func(_ int, el *goquery.Selection) {
|
|
ch := source.SChapter{}
|
|
el.Find("a").First().Each(func(_ int, a *goquery.Selection) {
|
|
ch.URL, _ = a.Attr("href")
|
|
ch.Name = strings.TrimSpace(a.Text())
|
|
})
|
|
if dt, ok := el.Find("time[datetime]").First().Attr("datetime"); ok {
|
|
ch.DateUpload = util.ParseAbsoluteDate(dt, "2006-01-02")
|
|
if ch.DateUpload == 0 {
|
|
ch.DateUpload = util.ParseRelativeDate(dt)
|
|
}
|
|
}
|
|
if ch.URL != "" {
|
|
chapters = append(chapters, ch)
|
|
}
|
|
})
|
|
return chapters, nil
|
|
}
|
|
|
|
var chapterIDRe = regexp.MustCompile(`const\s+CHAPTER_ID\s*=\s*(\d+)`)
|
|
|
|
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
|
|
}
|
|
// extract CHAPTER_ID from inline script
|
|
chapterID := ""
|
|
doc.Find("script").Each(func(_ int, el *goquery.Selection) {
|
|
if m := chapterIDRe.FindStringSubmatch(el.Text()); len(m) == 2 {
|
|
chapterID = m[1]
|
|
}
|
|
})
|
|
if chapterID == "" {
|
|
return nil, fmt.Errorf("liliana: could not find CHAPTER_ID")
|
|
}
|
|
ajaxDoc, err := s.get(context.Background(), fmt.Sprintf("%s/ajax/image/list/chap/%s", s.base(), chapterID))
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
var pages []source.Page
|
|
// two formats: div.separator[data-index] or plain div.separator > a
|
|
ajaxDoc.Find("div.separator[data-index]").Each(func(_ int, el *goquery.Selection) {
|
|
idx := len(pages)
|
|
if href, ok := el.Find("a").First().Attr("href"); ok && href != "" {
|
|
pages = append(pages, source.Page{Index: idx, ImageURL: util.AbsURL(s.cfg.BaseURL, href)})
|
|
}
|
|
})
|
|
if len(pages) == 0 {
|
|
ajaxDoc.Find("div.separator").Each(func(_ int, el *goquery.Selection) {
|
|
idx := len(pages)
|
|
if href, ok := el.Find("a").First().Attr("href"); ok && href != "" {
|
|
pages = append(pages, source.Page{Index: idx, ImageURL: util.AbsURL(s.cfg.BaseURL, href)})
|
|
}
|
|
})
|
|
}
|
|
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 ""
|
|
}
|