183 lines
5.3 KiB
Go
Executable File
183 lines
5.3 KiB
Go
Executable File
// Package masonry implements the Masonry manga base.
|
|
// Gallery-style site: each entry is a single-chapter gallery; pages via .list-gallery CDN links.
|
|
package masonry
|
|
|
|
import (
|
|
"context"
|
|
"fmt"
|
|
"net/http"
|
|
"strings"
|
|
|
|
"github.com/PuerkitoBio/goquery"
|
|
|
|
"goyomi/internal/httpclient"
|
|
"goyomi/internal/source"
|
|
"goyomi/sources/base/util"
|
|
)
|
|
|
|
type Config struct {
|
|
Name string
|
|
BaseURL string
|
|
Lang string
|
|
}
|
|
|
|
type Source struct {
|
|
cfg Config
|
|
client *httpclient.Client
|
|
id int64
|
|
}
|
|
|
|
func New(cfg Config) *Source {
|
|
c := httpclient.NewClient(httpclient.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) {
|
|
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("masonry: HTTP %d", resp.StatusCode)
|
|
}
|
|
return goquery.NewDocumentFromReader(resp.Body)
|
|
}
|
|
|
|
func imgAttr(img *goquery.Selection) string {
|
|
if v, ok := img.Attr("srcset"); ok && v != "" {
|
|
return strings.Fields(v)[0]
|
|
}
|
|
for _, attr := range []string{"data-cfsrc", "data-src", "data-lazy-src", "src"} {
|
|
if v, ok := img.Attr(attr); ok && v != "" {
|
|
return v
|
|
}
|
|
}
|
|
return ""
|
|
}
|
|
|
|
func (s *Source) parseMangaList(doc *goquery.Document) source.MangasPage {
|
|
// Exclude static galleries and broken entries
|
|
const sel = ".list-gallery:not(.static) figure:not(:has(a[href*=cdn.]))"
|
|
var mangas []source.SManga
|
|
doc.Find(sel).Each(func(_ int, el *goquery.Selection) {
|
|
a := el.Find("a").First()
|
|
if a.Length() == 0 {
|
|
return
|
|
}
|
|
m := source.SManga{}
|
|
m.URL = a.AttrOr("href", "")
|
|
m.Title = strings.TrimSpace(a.AttrOr("title", ""))
|
|
if m.Title == "" {
|
|
m.Title = strings.TrimSpace(a.Text())
|
|
}
|
|
if img := el.Find("img").First(); img.Length() > 0 {
|
|
m.ThumbnailURL = util.AbsURL(s.cfg.BaseURL, imgAttr(img))
|
|
}
|
|
if m.URL != "" && m.Title != "" {
|
|
mangas = append(mangas, m)
|
|
}
|
|
})
|
|
hasNext := doc.Find(".pagination .next, a.next-page, a[rel=next]").Length() > 0
|
|
return source.MangasPage{Mangas: mangas, HasNextPage: hasNext}
|
|
}
|
|
|
|
func (s *Source) GetPopularManga(page int) (source.MangasPage, error) {
|
|
var u string
|
|
switch page {
|
|
case 1:
|
|
u = s.base()
|
|
case 2:
|
|
u = s.base() + "/archive/"
|
|
default:
|
|
u = fmt.Sprintf("%s/archive/page/%d/", s.base(), page-1)
|
|
}
|
|
doc, err := s.get(context.Background(), u)
|
|
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/updates/sort/newest/mpage/%d/", 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) {
|
|
u := fmt.Sprintf("%s/updates/mpage/%d/?s=%s", s.base(), page, query)
|
|
doc, err := s.get(context.Background(), u)
|
|
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("h1.entry-title, h1.post-title").First().Text())
|
|
if result.Title == "" {
|
|
result.Title = manga.Title
|
|
}
|
|
result.Description = strings.TrimSpace(doc.Find("div.entry-content p").First().Text())
|
|
if img := doc.Find("img.attachment-post-thumbnail, img.wp-post-image").First(); img.Length() > 0 {
|
|
result.ThumbnailURL = util.AbsURL(s.cfg.BaseURL, imgAttr(img))
|
|
}
|
|
return result, nil
|
|
}
|
|
|
|
func (s *Source) GetChapterList(manga source.SManga) ([]source.SChapter, error) {
|
|
// Each masonry entry IS a single chapter (the gallery itself)
|
|
return []source.SChapter{{
|
|
URL: manga.URL,
|
|
Name: "Gallery",
|
|
}}, 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(".list-gallery a").Each(func(i int, a *goquery.Selection) {
|
|
href := a.AttrOr("href", "")
|
|
// Only CDN image links
|
|
if strings.HasPrefix(href, "https://cdn.") || strings.Contains(href, "/cdn.") {
|
|
pages = append(pages, source.Page{Index: i, ImageURL: href})
|
|
}
|
|
})
|
|
// Fallback: any direct image links in gallery
|
|
if len(pages) == 0 {
|
|
doc.Find(".list-gallery img").Each(func(i int, img *goquery.Selection) {
|
|
u := imgAttr(img)
|
|
if 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 }
|