9a42dd2ab1
- Remove global ProxyEnabled() logic from httpclient - Each source now explicitly chooses client at import time: - flare client: for JS-rendering/cloudflare sources - normal httpclient: for REST API sources - Updated 29 base sources based on Kotlin reference (network.cloudflareClient)
188 lines
5.8 KiB
Go
Executable File
188 lines
5.8 KiB
Go
Executable File
// Package gattsu implements the Gattsu Brazilian adult manga base.
|
|
// Popular = Latest: GET {base}/page/{n}; no separate popular endpoint.
|
|
package gattsu
|
|
|
|
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 {
|
|
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("gattsu: HTTP %d", resp.StatusCode)
|
|
}
|
|
return goquery.NewDocumentFromReader(resp.Body)
|
|
}
|
|
|
|
// thumbSizeRe matches WordPress size suffix like "-150x150." and replaces with ".".
|
|
var thumbSizeRe = regexp.MustCompile(`-\d+x\d+\.`)
|
|
|
|
func withoutSize(u string) string {
|
|
return thumbSizeRe.ReplaceAllString(u, ".")
|
|
}
|
|
|
|
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 ""
|
|
}
|
|
|
|
func (s *Source) mangaFromElement(el *goquery.Selection) source.SManga {
|
|
m := source.SManga{}
|
|
m.URL, _ = el.Attr("href")
|
|
m.Title = strings.TrimSpace(el.Find("span.thumb-titulo").Text())
|
|
el.Find("span.thumb-imagem img.wp-post-image").First().Each(func(_ int, img *goquery.Selection) {
|
|
if src, ok := img.Attr("src"); ok {
|
|
m.ThumbnailURL = withoutSize(util.AbsURL(s.cfg.BaseURL, src))
|
|
}
|
|
})
|
|
return m
|
|
}
|
|
|
|
func (s *Source) parseList(doc *goquery.Document) source.MangasPage {
|
|
var mangas []source.SManga
|
|
prefix := s.base()
|
|
sel := fmt.Sprintf("div.meio div.lista ul li a[href^=%s]", prefix)
|
|
doc.Find(sel).Each(func(_ int, el *goquery.Selection) {
|
|
m := s.mangaFromElement(el)
|
|
if m.URL != "" {
|
|
mangas = append(mangas, m)
|
|
}
|
|
})
|
|
hasNext := doc.Find(".next.page-numbers, a.next").Length() > 0
|
|
return source.MangasPage{Mangas: mangas, HasNextPage: hasNext}
|
|
}
|
|
|
|
func (s *Source) fetchPage(page int) (source.MangasPage, error) {
|
|
var u string
|
|
if page == 1 {
|
|
u = s.base() + "/"
|
|
} else {
|
|
u = fmt.Sprintf("%s/page/%d", s.base(), page)
|
|
}
|
|
doc, err := s.get(context.Background(), u)
|
|
if err != nil {
|
|
return source.MangasPage{}, err
|
|
}
|
|
return s.parseList(doc), nil
|
|
}
|
|
|
|
func (s *Source) GetPopularManga(page int) (source.MangasPage, error) { return s.fetchPage(page) }
|
|
func (s *Source) GetLatestUpdates(page int) (source.MangasPage, error) { return s.fetchPage(page) }
|
|
|
|
func (s *Source) GetSearchManga(page int, query string, filters []source.Filter) (source.MangasPage, error) {
|
|
u := fmt.Sprintf("%s/?s=%s&post_type=post", s.base(), query)
|
|
if page > 1 {
|
|
u = fmt.Sprintf("%s/page/%d/?s=%s&post_type=post", s.base(), page, query)
|
|
}
|
|
doc, err := s.get(context.Background(), u)
|
|
if err != nil {
|
|
return source.MangasPage{}, err
|
|
}
|
|
return s.parseList(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, Status: source.StatusCompleted}
|
|
postBox := doc.Find("div.meio div.post-box").First()
|
|
result.Title = strings.TrimSpace(postBox.Find("h1.post-titulo").Text())
|
|
if result.Title == "" {
|
|
result.Title = manga.Title
|
|
}
|
|
result.Author = strings.TrimSpace(postBox.Find("ul.post-itens li:contains(Artista) a").First().Text())
|
|
var genres []string
|
|
postBox.Find("ul.post-itens li:contains(Tags) a").Each(func(_ int, a *goquery.Selection) {
|
|
if t := strings.TrimSpace(a.Text()); t != "" {
|
|
genres = append(genres, t)
|
|
}
|
|
})
|
|
result.Genre = strings.Join(genres, ", ")
|
|
var descParts []string
|
|
postBox.Find("div.post-texto p").Each(func(_ int, p *goquery.Selection) {
|
|
t := strings.TrimSpace(p.Text())
|
|
t = strings.TrimPrefix(t, "Sinopse :")
|
|
if t = strings.TrimSpace(t); t != "" {
|
|
descParts = append(descParts, t)
|
|
}
|
|
})
|
|
result.Description = strings.Join(descParts, "\n\n")
|
|
postBox.Find("div.post-capa > img.wp-post-image").First().Each(func(_ int, img *goquery.Selection) {
|
|
if src, ok := img.Attr("src"); ok {
|
|
result.ThumbnailURL = withoutSize(util.AbsURL(s.cfg.BaseURL, src))
|
|
}
|
|
})
|
|
return result, nil
|
|
}
|
|
|
|
func (s *Source) GetChapterList(manga source.SManga) ([]source.SChapter, error) {
|
|
// Gattsu pages are single-chapter galleries; the manga page is the chapter.
|
|
return []source.SChapter{{
|
|
URL: manga.URL,
|
|
Name: manga.Title,
|
|
}}, 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
|
|
}
|
|
sel := "div.meio div.post-box ul.post-fotos li a > img, " +
|
|
"div.meio div.post-box.listaImagens div.galeriaHtml img"
|
|
var pages []source.Page
|
|
doc.Find(sel).Each(func(i int, img *goquery.Selection) {
|
|
if u := imgAttr(img, s.cfg.BaseURL); u != "" {
|
|
pages = append(pages, source.Page{Index: i, ImageURL: withoutSize(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 }
|