Files
goyomi/sources/base/fansubscat/fansubscat.go
T
2026-05-11 06:48:23 +00:00

199 lines
5.4 KiB
Go
Executable File

// Package fansubscat implements the FansubsCat manga base.
// JSON REST API: GET {api}/manga/popular/{page}, /recent/{page}, /search/{page}
package fansubscat
import (
"context"
"encoding/json"
"fmt"
"io"
"net/http"
"strings"
"goyomi/internal/httpclient"
"goyomi/internal/source"
"goyomi/sources/base/util"
)
type Config struct {
Name string
BaseURL string
APIURL string
Lang string
IsHentaiSite bool
}
type Source struct {
cfg Config
client *httpclient.Client
id int64
}
func New(cfg Config) *Source {
if cfg.APIURL == "" {
cfg.APIURL = 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 true }
func (s *Source) api() string { return strings.TrimRight(s.cfg.APIURL, "/") }
func (s *Source) getJSON(ctx context.Context, rawURL string, out any) error {
req, err := http.NewRequestWithContext(ctx, http.MethodGet, rawURL, nil)
if err != nil {
return err
}
req.Header.Set("Accept", "application/json")
req.Header.Set("Referer", s.cfg.BaseURL+"/")
resp, err := s.client.Do(req)
if err != nil {
return err
}
defer resp.Body.Close()
if resp.StatusCode != http.StatusOK {
return fmt.Errorf("fansubscat: HTTP %d", resp.StatusCode)
}
body, _ := io.ReadAll(resp.Body)
return json.Unmarshal(body, out)
}
// resultWrapper is the common API envelope.
type resultWrapper struct {
Result json.RawMessage `json:"result"`
}
type mangaDTO struct {
Slug string `json:"slug"`
Name string `json:"name"`
ThumbnailURL string `json:"thumbnail_url"`
Synopsis string `json:"synopsis"`
Status string `json:"status"`
Genres string `json:"genres"`
Author string `json:"author"`
}
type chapterDTO struct {
Slug string `json:"slug"`
Name string `json:"name"`
Date int64 `json:"date"`
}
type pageDTO struct {
URL string `json:"url"`
}
func toSManga(m mangaDTO) source.SManga {
return source.SManga{
URL: m.Slug,
Title: m.Name,
Author: m.Author,
Description: m.Synopsis,
Genre: m.Genres,
Status: util.StatusFromString(m.Status),
ThumbnailURL: m.ThumbnailURL,
}
}
func (s *Source) fetchList(ctx context.Context, endpoint string) (source.MangasPage, error) {
var wrap resultWrapper
if err := s.getJSON(ctx, endpoint, &wrap); err != nil {
return source.MangasPage{}, err
}
var dtos []mangaDTO
if err := json.Unmarshal(wrap.Result, &dtos); err != nil {
return source.MangasPage{}, err
}
mangas := make([]source.SManga, len(dtos))
for i, m := range dtos {
mangas[i] = toSManga(m)
}
return source.MangasPage{Mangas: mangas, HasNextPage: len(dtos) >= 20}, nil
}
func (s *Source) GetPopularManga(page int) (source.MangasPage, error) {
return s.fetchList(context.Background(), fmt.Sprintf("%s/manga/popular/%d", s.api(), page))
}
func (s *Source) GetLatestUpdates(page int) (source.MangasPage, error) {
return s.fetchList(context.Background(), fmt.Sprintf("%s/manga/recent/%d", s.api(), page))
}
func (s *Source) GetSearchManga(page int, query string, filters []source.Filter) (source.MangasPage, error) {
u := fmt.Sprintf("%s/manga/search/%d?type=all", s.api(), page)
if query != "" {
u += "&query=" + query
}
return s.fetchList(context.Background(), u)
}
func (s *Source) GetMangaDetails(manga source.SManga) (source.SManga, error) {
var wrap resultWrapper
slug := manga.URL
if idx := strings.LastIndex(slug, "/"); idx >= 0 {
slug = slug[idx+1:]
}
if err := s.getJSON(context.Background(), fmt.Sprintf("%s/manga/details/%s", s.api(), slug), &wrap); err != nil {
return manga, err
}
var dto mangaDTO
if err := json.Unmarshal(wrap.Result, &dto); err != nil {
return manga, err
}
out := toSManga(dto)
out.URL = manga.URL
return out, nil
}
func (s *Source) GetChapterList(manga source.SManga) ([]source.SChapter, error) {
slug := manga.URL
if idx := strings.LastIndex(slug, "/"); idx >= 0 {
slug = slug[idx+1:]
}
var wrap resultWrapper
if err := s.getJSON(context.Background(), fmt.Sprintf("%s/manga/chapters/%s", s.api(), slug), &wrap); err != nil {
return nil, err
}
var dtos []chapterDTO
if err := json.Unmarshal(wrap.Result, &dtos); err != nil {
return nil, err
}
chapters := make([]source.SChapter, len(dtos))
for i, ch := range dtos {
chapters[i] = source.SChapter{
URL: ch.Slug,
Name: ch.Name,
DateUpload: ch.Date * 1000,
}
}
return chapters, nil
}
func (s *Source) GetPageList(chapter source.SChapter) ([]source.Page, error) {
slug := chapter.URL
if idx := strings.LastIndex(slug, "/"); idx >= 0 {
slug = slug[idx+1:]
}
var wrap resultWrapper
if err := s.getJSON(context.Background(), fmt.Sprintf("%s/manga/pages/%s", s.api(), slug), &wrap); err != nil {
return nil, err
}
var dtos []pageDTO
if err := json.Unmarshal(wrap.Result, &dtos); err != nil {
return nil, err
}
pages := make([]source.Page, len(dtos))
for i, p := range dtos {
pages[i] = source.Page{Index: i, ImageURL: p.URL}
}
return pages, nil
}
func (s *Source) GetImageURL(page source.Page) (string, error) { return page.ImageURL, nil }
func (s *Source) GetFilterList() []source.Filter { return nil }