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
221 lines
6.8 KiB
Go
Executable File
221 lines
6.8 KiB
Go
Executable File
// Package mangareader implements the MangaReader base.
|
|
// All list types via GET {base}/?page={n}&sort={val}; pages via AJAX; FlareSolverr required.
|
|
package mangareader
|
|
|
|
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
|
|
TypeParam string // "comic", "manga", "manhwa", etc.
|
|
ChapterListID string // ID of chapter list container, default "en-chapters"
|
|
}
|
|
|
|
type Source struct {
|
|
cfg Config
|
|
client *flare.Client
|
|
id int64
|
|
}
|
|
|
|
func New(cfg Config) *Source {
|
|
if cfg.ChapterListID == "" {
|
|
cfg.ChapterListID = "en-chapters"
|
|
}
|
|
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("mangareader: HTTP %d", resp.StatusCode)
|
|
}
|
|
return goquery.NewDocumentFromReader(resp.Body)
|
|
}
|
|
|
|
func (s *Source) searchURL(page int, sort, query string) string {
|
|
u := fmt.Sprintf("%s/?page=%d", s.base(), page)
|
|
if sort != "" {
|
|
u += "&sort=" + sort
|
|
}
|
|
if s.cfg.TypeParam != "" {
|
|
u += "&type=" + s.cfg.TypeParam
|
|
}
|
|
if query != "" {
|
|
u += "&keyword=" + query
|
|
}
|
|
return u
|
|
}
|
|
|
|
func (s *Source) parseMangaList(doc *goquery.Document) source.MangasPage {
|
|
var mangas []source.SManga
|
|
doc.Find(".manga_list-sbs .manga-poster, .manga_list .manga-poster").Each(func(_ int, el *goquery.Selection) {
|
|
m := source.SManga{}
|
|
el.Find("a").First().Each(func(_ int, a *goquery.Selection) {
|
|
m.URL, _ = a.Attr("href")
|
|
})
|
|
el.Find("img").First().Each(func(_ int, img *goquery.Selection) {
|
|
m.Title = img.AttrOr("alt", "")
|
|
m.ThumbnailURL = imgAttr(img, s.cfg.BaseURL)
|
|
})
|
|
if m.URL != "" {
|
|
mangas = append(mangas, m)
|
|
}
|
|
})
|
|
hasNext := doc.Find("ul.pagination > li.active + li").Length() > 0
|
|
return source.MangasPage{Mangas: mangas, HasNextPage: hasNext}
|
|
}
|
|
|
|
func (s *Source) GetPopularManga(page int) (source.MangasPage, error) {
|
|
doc, err := s.get(context.Background(), s.searchURL(page, "most-viewed", ""))
|
|
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(), s.searchURL(page, "latest-updated", ""))
|
|
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(), s.searchURL(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}
|
|
detail := doc.Find("#ani_detail")
|
|
result.Title = strings.TrimSpace(detail.Find(".manga-name").First().Text())
|
|
result.ThumbnailURL = imgAttr(detail.Find("img").First(), s.cfg.BaseURL)
|
|
var genres []string
|
|
detail.Find(".genres > a").Each(func(_ int, el *goquery.Selection) {
|
|
if t := strings.TrimSpace(el.Text()); t != "" {
|
|
genres = append(genres, t)
|
|
}
|
|
})
|
|
result.Genre = strings.Join(genres, ", ")
|
|
detail.Find(".anisc-info > .item").Each(func(_ int, el *goquery.Selection) {
|
|
head := strings.TrimSpace(el.Find(".item-head").Text())
|
|
val := strings.TrimSpace(el.Find("a, span:not(.item-head)").First().Text())
|
|
switch {
|
|
case strings.Contains(head, "Author"):
|
|
result.Author = val
|
|
case strings.Contains(head, "Status"):
|
|
result.Status = util.StatusFromString(val)
|
|
}
|
|
})
|
|
var desc strings.Builder
|
|
detail.Find(".description").Each(func(_ int, el *goquery.Selection) {
|
|
desc.WriteString(strings.TrimSpace(el.Text()))
|
|
})
|
|
result.Description = desc.String()
|
|
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("#"+s.cfg.ChapterListID+" > li.chapter-item").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.Find(".name").Text())
|
|
if ch.Name == "" {
|
|
ch.Name = strings.TrimSpace(a.Text())
|
|
}
|
|
})
|
|
el.Find(".date").First().Each(func(_ int, e *goquery.Selection) {
|
|
ch.DateUpload = util.ParseRelativeDate(e.Text())
|
|
})
|
|
if ch.URL != "" {
|
|
chapters = append(chapters, ch)
|
|
}
|
|
})
|
|
return chapters, nil
|
|
}
|
|
|
|
func (s *Source) GetPageList(chapter source.SChapter) ([]source.Page, error) {
|
|
chapterURL := util.AbsURL(s.cfg.BaseURL, chapter.URL)
|
|
doc, err := s.get(context.Background(), chapterURL)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
// extract reading ID from the page
|
|
readingID, _ := doc.Find("div[data-reading-id]").First().Attr("data-reading-id")
|
|
if readingID == "" {
|
|
readingID, _ = doc.Find("div[data-id]").First().Attr("data-id")
|
|
}
|
|
if readingID == "" {
|
|
return nil, fmt.Errorf("mangareader: could not find reading ID")
|
|
}
|
|
// double-slash is intentional — matches Kotlin source
|
|
ajaxURL := fmt.Sprintf("%s//ajax/image/list/%s?mode=vertical", s.base(), readingID)
|
|
ajaxDoc, err := s.get(context.Background(), ajaxURL)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
var pages []source.Page
|
|
ajaxDoc.Find(".container-reader-chapter > div > img, .container-reader-chapter 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 ""
|
|
}
|