143 lines
4.3 KiB
Go
Executable File
143 lines
4.3 KiB
Go
Executable File
// Package mangacatalog implements the MangaCatalog manga base.
|
|
// Single-franchise site network: popular list = static sourceList; chapters via HTML scraping.
|
|
package mangacatalog
|
|
|
|
import (
|
|
"context"
|
|
"fmt"
|
|
"net/http"
|
|
"strings"
|
|
|
|
"github.com/PuerkitoBio/goquery"
|
|
|
|
"goyomi/internal/httpclient"
|
|
"goyomi/internal/source"
|
|
"goyomi/sources/base/util"
|
|
)
|
|
|
|
type SourceEntry struct {
|
|
Name string
|
|
URL string
|
|
}
|
|
|
|
type Config struct {
|
|
Name string
|
|
BaseURL string
|
|
Lang string
|
|
SourceList []SourceEntry
|
|
}
|
|
|
|
type Source struct {
|
|
cfg Config
|
|
client *httpclient.Client
|
|
id int64
|
|
}
|
|
|
|
func New(cfg Config) *Source {
|
|
if len(cfg.SourceList) == 0 {
|
|
cfg.SourceList = []SourceEntry{{Name: cfg.Name, URL: cfg.BaseURL}}
|
|
}
|
|
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 false }
|
|
|
|
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("mangacatalog: HTTP %d", resp.StatusCode)
|
|
}
|
|
return goquery.NewDocumentFromReader(resp.Body)
|
|
}
|
|
|
|
func (s *Source) GetPopularManga(page int) (source.MangasPage, error) {
|
|
mangas := make([]source.SManga, len(s.cfg.SourceList))
|
|
for i, entry := range s.cfg.SourceList {
|
|
mangas[i] = source.SManga{Title: entry.Name, URL: entry.URL}
|
|
}
|
|
return source.MangasPage{Mangas: mangas, HasNextPage: false}, nil
|
|
}
|
|
|
|
func (s *Source) GetLatestUpdates(page int) (source.MangasPage, error) {
|
|
return source.MangasPage{}, nil
|
|
}
|
|
|
|
func (s *Source) GetSearchManga(page int, query string, filters []source.Filter) (source.MangasPage, error) {
|
|
var mangas []source.SManga
|
|
for _, entry := range s.cfg.SourceList {
|
|
if query == "" || strings.Contains(strings.ToLower(entry.Name), strings.ToLower(query)) {
|
|
mangas = append(mangas, source.SManga{Title: entry.Name, URL: entry.URL})
|
|
}
|
|
}
|
|
return source.MangasPage{Mangas: mangas, HasNextPage: false}, 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, Title: manga.Title}
|
|
if thumb := doc.Find("img[itemprop=image], [itemprop=image] img").First().AttrOr("src", ""); thumb != "" {
|
|
result.ThumbnailURL = util.AbsURL(manga.URL, thumb)
|
|
}
|
|
result.Description = strings.TrimSpace(doc.Find("div.description, div#description").Text())
|
|
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
|
|
// Selector: div.w-full > div.bg-bg-secondary > div.grid
|
|
doc.Find("div.w-full > div.bg-bg-secondary > div.grid").Each(func(_ int, el *goquery.Selection) {
|
|
ch := source.SChapter{}
|
|
link := el.Find(".col-span-4 > a").First()
|
|
ch.URL = link.AttrOr("href", "")
|
|
name1 := strings.TrimSpace(link.Text())
|
|
name2 := strings.TrimSpace(el.Find(".text-xs:not(a)").Text())
|
|
if name2 == "" {
|
|
ch.Name = name1
|
|
} else {
|
|
ch.Name = name1 + " - " + name2
|
|
}
|
|
if ch.URL != "" {
|
|
chapters = append(chapters, ch)
|
|
}
|
|
})
|
|
return chapters, 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("img[data-src]").Each(func(i int, img *goquery.Selection) {
|
|
u := img.AttrOr("data-src", "")
|
|
if u != "" {
|
|
pages = append(pages, source.Page{Index: i, ImageURL: util.AbsURL(chapter.URL, 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 }
|