46f930718c
Based on Kotlin reference verification: - Batch 1: colorlibanime, eromuse, fansubscat → flare - Batch 2: fmreader, galleryadults, greenshit, grouple → flare - Batch 3: heancms, lectormoe → flare Also renamed methods appropriately: - get() for HTML scraping - getJSON() for JSON APIs
206 lines
6.2 KiB
Go
Executable File
206 lines
6.2 KiB
Go
Executable File
// Package galleryadults implements the GalleryAdults adult gallery base.
|
|
// HTML scraping; popular: GET {base}/language/{lang}/popular/?page={n}.
|
|
package galleryadults
|
|
|
|
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
|
|
MangaLang string // language path segment, e.g. "english"; empty = all
|
|
}
|
|
|
|
type Source struct {
|
|
cfg Config
|
|
client *flare.Client
|
|
id int64
|
|
}
|
|
|
|
func New(cfg Config) *Source {
|
|
c := flare.NewClient(flare.WithRateLimit(1, 2))
|
|
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) {
|
|
resp, err := s.client.Get(ctx, rawURL)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
defer resp.Body.Close()
|
|
if resp.StatusCode != http.StatusOK {
|
|
return nil, fmt.Errorf("galleryadults: HTTP %d", resp.StatusCode)
|
|
}
|
|
return goquery.NewDocumentFromReader(resp.Body)
|
|
}
|
|
|
|
var shortenTitleRe = regexp.MustCompile(`(\[[^\]]*\]|[({][^)}]*[)}])`)
|
|
var tagCountRe = regexp.MustCompile(`\s*\(\d+\)\s*$`)
|
|
|
|
func (s *Source) mangaTitle(el *goquery.Selection, selector string) string {
|
|
raw := strings.TrimSpace(el.Find(selector).Text())
|
|
return strings.TrimSpace(shortenTitleRe.ReplaceAllString(raw, ""))
|
|
}
|
|
|
|
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 ""
|
|
}
|
|
|
|
// thumbnailToFull converts thumbnail URLs to full image URLs by removing
|
|
// the "t" before the file extension (e.g. "1t.jpg" → "1.jpg").
|
|
func thumbnailToFull(u string) string {
|
|
ext := u[strings.LastIndex(u, "."):]
|
|
return strings.Replace(u, "t"+ext, ext, 1)
|
|
}
|
|
|
|
func (s *Source) mangaFromElement(el *goquery.Selection) source.SManga {
|
|
m := source.SManga{}
|
|
el.Find(".inner_thumb a").First().Each(func(_ int, a *goquery.Selection) {
|
|
m.URL, _ = a.Attr("href")
|
|
})
|
|
m.Title = s.mangaTitle(el, ".caption")
|
|
el.Find(".inner_thumb img").First().Each(func(_ int, img *goquery.Selection) {
|
|
m.ThumbnailURL = imgAttr(img, s.cfg.BaseURL)
|
|
})
|
|
return m
|
|
}
|
|
|
|
func (s *Source) parsePage(doc *goquery.Document) source.MangasPage {
|
|
var mangas []source.SManga
|
|
doc.Find("div.thumb").Each(func(_ int, el *goquery.Selection) {
|
|
m := s.mangaFromElement(el)
|
|
if m.URL != "" && m.Title != "" {
|
|
mangas = append(mangas, m)
|
|
}
|
|
})
|
|
hasNext := doc.Find(".next.page-numbers, a[aria-label=Next]").Length() > 0
|
|
return source.MangasPage{Mangas: mangas, HasNextPage: hasNext}
|
|
}
|
|
|
|
func (s *Source) langPath() string {
|
|
if s.cfg.MangaLang != "" {
|
|
return "language/" + s.cfg.MangaLang + "/"
|
|
}
|
|
return ""
|
|
}
|
|
|
|
func (s *Source) GetPopularManga(page int) (source.MangasPage, error) {
|
|
u := fmt.Sprintf("%s/%spopular/?page=%d", s.base(), s.langPath(), page)
|
|
doc, err := s.get(context.Background(), u)
|
|
if err != nil {
|
|
return source.MangasPage{}, err
|
|
}
|
|
return s.parsePage(doc), nil
|
|
}
|
|
|
|
func (s *Source) GetLatestUpdates(page int) (source.MangasPage, error) {
|
|
u := fmt.Sprintf("%s/%s?page=%d", s.base(), s.langPath(), page)
|
|
doc, err := s.get(context.Background(), u)
|
|
if err != nil {
|
|
return source.MangasPage{}, err
|
|
}
|
|
return s.parsePage(doc), nil
|
|
}
|
|
|
|
func (s *Source) GetSearchManga(page int, query string, filters []source.Filter) (source.MangasPage, error) {
|
|
u := fmt.Sprintf("%s/search/?q=%s&page=%d", s.base(), query, page)
|
|
doc, err := s.get(context.Background(), u)
|
|
if err != nil {
|
|
return source.MangasPage{}, err
|
|
}
|
|
return s.parsePage(doc), nil
|
|
}
|
|
|
|
// getInfo extracts tag links from elements matching ".tags" that contain "{tag}:".
|
|
func getInfo(el *goquery.Selection, tag string) string {
|
|
var items []string
|
|
el.Find(".tags").Each(func(_ int, tags *goquery.Selection) {
|
|
if strings.Contains(tags.Text(), tag+":") {
|
|
tags.Find("a.tag_btn").Each(func(_ int, a *goquery.Selection) {
|
|
t := tagCountRe.ReplaceAllString(strings.TrimSpace(a.Text()), "")
|
|
if t != "" {
|
|
items = append(items, t)
|
|
}
|
|
})
|
|
}
|
|
})
|
|
return strings.Join(items, ", ")
|
|
}
|
|
|
|
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, Status: source.StatusCompleted}
|
|
top := doc.Find(".gallery_top")
|
|
result.Title = s.mangaTitle(top, "h1")
|
|
if result.Title == "" {
|
|
result.Title = manga.Title
|
|
}
|
|
top.Find(".cover img").First().Each(func(_ int, img *goquery.Selection) {
|
|
result.ThumbnailURL = imgAttr(img, s.cfg.BaseURL)
|
|
})
|
|
result.Genre = getInfo(top, "Tags")
|
|
result.Author = getInfo(top, "Artists")
|
|
return result, nil
|
|
}
|
|
|
|
func (s *Source) GetChapterList(manga source.SManga) ([]source.SChapter, error) {
|
|
// Galleries have a single chapter: the gallery itself.
|
|
return []source.SChapter{{
|
|
URL: manga.URL,
|
|
Name: "Chapter",
|
|
}}, 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
|
|
// Try thumbnail → full conversion first.
|
|
doc.Find(".gallery_thumb a img").Each(func(i int, img *goquery.Selection) {
|
|
if u := imgAttr(img, s.cfg.BaseURL); u != "" {
|
|
pages = append(pages, source.Page{Index: i, ImageURL: thumbnailToFull(u)})
|
|
}
|
|
})
|
|
if len(pages) == 0 {
|
|
// Fallback: linked images directly.
|
|
doc.Find(".gallery_thumb a").Each(func(i int, a *goquery.Selection) {
|
|
if u, ok := a.Attr("href"); ok && u != "" {
|
|
pages = append(pages, source.Page{Index: i, ImageURL: util.AbsURL(s.cfg.BaseURL, 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 }
|