aa697af25f
- guya: align JSON structs with current API (seriesListEntry, seriesDetail), use Cover/Author/Artist fields, fix chapter date parsing via release_date - iken: update DTO fields to match API (postTitle, featuredImage, etc.), add JSON-vs-HTML detection, map seriesStatus to source.Status constants - kemono: set Accept: text/css header for DDOS-Guard bypass - madtheme: use .book-detailed-item selector, fix pagination detection, use 'updated_at' sort for latest updates - mangahub: check cookie jar in addition to Set-Cookie headers, add retry-once logic for API key expiry (matching Kotlin interceptor)
418 lines
12 KiB
Go
Executable File
418 lines
12 KiB
Go
Executable File
// Package mangahub implements the MangaHub GraphQL base.
|
|
// Each mirror site has a per-site source identifier (MangaSource, e.g. "m01")
|
|
// passed as the `x` argument in all GraphQL queries.
|
|
// API auth requires an `mhub_access` cookie obtained by loading a chapter page
|
|
// and sent as the `x-mhub-access` request header.
|
|
package mangahub
|
|
|
|
import (
|
|
"context"
|
|
"encoding/json"
|
|
"fmt"
|
|
"io"
|
|
"net/http"
|
|
"regexp"
|
|
"strings"
|
|
"sync"
|
|
"time"
|
|
|
|
"goyomi/internal/httpclient/flare"
|
|
"goyomi/internal/source"
|
|
"goyomi/sources/base/util"
|
|
)
|
|
|
|
const (
|
|
baseCDNURL = "https://imgx.mghcdn.com"
|
|
thumbCDNURL = "https://thumb.mghcdn.com"
|
|
defaultAPIURL = "https://api.mghcdn.com"
|
|
)
|
|
|
|
var apiKeyRe = regexp.MustCompile(`mhub_access=([^;]+)`)
|
|
|
|
type Config struct {
|
|
Name string
|
|
BaseURL string
|
|
APIURL string
|
|
Lang string
|
|
MangaSource string // per-site source identifier, e.g. "m01", "mh01"
|
|
}
|
|
|
|
type Source struct {
|
|
cfg Config
|
|
client *flare.Client
|
|
id int64
|
|
mu sync.Mutex
|
|
accessKey string
|
|
refreshed int64
|
|
}
|
|
|
|
func New(cfg Config) *Source {
|
|
if cfg.APIURL == "" {
|
|
cfg.APIURL = defaultAPIURL
|
|
}
|
|
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) src() string {
|
|
if s.cfg.MangaSource == "" {
|
|
return "m01"
|
|
}
|
|
return s.cfg.MangaSource
|
|
}
|
|
|
|
// ensureAccessKey fetches the mhub_access cookie by loading a chapter page,
|
|
// caching it for 10 minutes. It checks both the response Set-Cookie header
|
|
// and the client's cookie jar (matching the Kotlin implementation which reads
|
|
// cookies from the jar via client.cookieJar.loadForRequest()).
|
|
func (s *Source) ensureAccessKey(ctx context.Context, refreshURL string) (string, error) {
|
|
s.mu.Lock()
|
|
defer s.mu.Unlock()
|
|
|
|
now := time.Now().UnixMilli()
|
|
if s.accessKey != "" && now-s.refreshed < 10*60*1000 {
|
|
return s.accessKey, nil
|
|
}
|
|
|
|
// Try the jar first — a previous request may have stored the cookie.
|
|
if v := s.client.Cookie("mhub_access", s.cfg.BaseURL); v != "" {
|
|
s.accessKey = v
|
|
s.refreshed = now
|
|
return s.accessKey, nil
|
|
}
|
|
|
|
chapterURLs := []string{
|
|
s.cfg.BaseURL + "/chapter/martial-peak/chapter-1000",
|
|
s.cfg.BaseURL + "/chapter/martial-peak/chapter-1000?reloadKey=1",
|
|
}
|
|
// If a specific refresh URL was provided (e.g. from a failing GraphQL
|
|
// request's manga URL), prefer it over the fallback.
|
|
if refreshURL != "" {
|
|
chapterURLs = append([]string{refreshURL, refreshURL + "?reloadKey=1"}, chapterURLs...)
|
|
}
|
|
|
|
referer := s.cfg.BaseURL + "/manga/martial-peak"
|
|
for _, u := range chapterURLs {
|
|
req, err := http.NewRequestWithContext(ctx, http.MethodGet, u, nil)
|
|
if err != nil {
|
|
return "", err
|
|
}
|
|
req.Header.Set("Referer", referer)
|
|
resp, err := s.client.Do(req)
|
|
if err != nil {
|
|
return "", err
|
|
}
|
|
resp.Body.Close()
|
|
|
|
// Check Set-Cookie headers (fixed by doFS() now propagating FS headers).
|
|
for _, ck := range resp.Header.Values("set-cookie") {
|
|
if m := apiKeyRe.FindStringSubmatch(ck); len(m) == 2 && m[1] != "" {
|
|
s.accessKey = m[1]
|
|
s.refreshed = now
|
|
return s.accessKey, nil
|
|
}
|
|
}
|
|
// Also check the jar — FS cookies were fed into it and may include
|
|
// mhub_access even when the response headers don't.
|
|
if v := s.client.Cookie("mhub_access", s.cfg.BaseURL); v != "" {
|
|
s.accessKey = v
|
|
s.refreshed = now
|
|
return s.accessKey, nil
|
|
}
|
|
}
|
|
return "", fmt.Errorf("mangahub: mhub_access cookie not found")
|
|
}
|
|
|
|
func (s *Source) invalidateKey() {
|
|
s.mu.Lock()
|
|
s.accessKey = ""
|
|
s.mu.Unlock()
|
|
}
|
|
|
|
// gql sends a raw GraphQL query string (no variables — uses direct interpolation
|
|
// matching the Kotlin implementation) and unmarshals data into out.
|
|
func (s *Source) gql(ctx context.Context, query string, out any) error {
|
|
return s.gqlRetryOnce(ctx, query, out, "")
|
|
}
|
|
|
|
// gqlRetryOnce is like gql but retries once on API key errors, matching the
|
|
// Kotlin interceptor pattern that refreshes the key and retries the request.
|
|
func (s *Source) gqlRetryOnce(ctx context.Context, query string, out any, refreshURL string) error {
|
|
key, err := s.ensureAccessKey(ctx, refreshURL)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
|
|
payload, _ := json.Marshal(map[string]string{"query": query})
|
|
req, err := http.NewRequestWithContext(ctx, http.MethodPost,
|
|
s.cfg.APIURL+"/graphql", strings.NewReader(string(payload)))
|
|
if err != nil {
|
|
return err
|
|
}
|
|
req.Header.Set("Content-Type", "application/json")
|
|
req.Header.Set("Accept", "application/json")
|
|
req.Header.Set("Origin", s.cfg.BaseURL)
|
|
req.Header.Set("Referer", s.cfg.BaseURL+"/")
|
|
req.Header.Set("x-mhub-access", key)
|
|
|
|
resp, err := s.client.Do(req)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
defer resp.Body.Close()
|
|
|
|
if resp.StatusCode != http.StatusOK {
|
|
return fmt.Errorf("mangahub: HTTP %d", resp.StatusCode)
|
|
}
|
|
raw, err := io.ReadAll(resp.Body)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
|
|
var wrapper struct {
|
|
Data json.RawMessage `json:"data"`
|
|
Errors []struct {
|
|
Message string `json:"message"`
|
|
} `json:"errors"`
|
|
}
|
|
if err := json.Unmarshal(raw, &wrapper); err != nil {
|
|
return err
|
|
}
|
|
if len(wrapper.Errors) > 0 {
|
|
msg := wrapper.Errors[0].Message
|
|
lower := strings.ToLower(msg)
|
|
if strings.Contains(lower, "rate") || strings.Contains(lower, "api key") {
|
|
s.invalidateKey()
|
|
// Retry once with a refreshed key, like Kotlin's interceptor.
|
|
key2, err2 := s.ensureAccessKey(ctx, refreshURL)
|
|
if err2 != nil {
|
|
return fmt.Errorf("mangahub: %s (key refresh failed: %v)", msg, err2)
|
|
}
|
|
req2, _ := http.NewRequestWithContext(ctx, http.MethodPost,
|
|
s.cfg.APIURL+"/graphql", strings.NewReader(string(payload)))
|
|
req2.Header.Set("Content-Type", "application/json")
|
|
req2.Header.Set("Accept", "application/json")
|
|
req2.Header.Set("Origin", s.cfg.BaseURL)
|
|
req2.Header.Set("Referer", s.cfg.BaseURL+"/")
|
|
req2.Header.Set("x-mhub-access", key2)
|
|
resp2, err2 := s.client.Do(req2)
|
|
if err2 != nil {
|
|
return err2
|
|
}
|
|
defer resp2.Body.Close()
|
|
if resp2.StatusCode != http.StatusOK {
|
|
return fmt.Errorf("mangahub: HTTP %d (after key refresh)", resp2.StatusCode)
|
|
}
|
|
raw2, _ := io.ReadAll(resp2.Body)
|
|
var wrapper2 struct {
|
|
Data json.RawMessage `json:"data"`
|
|
Errors []struct {
|
|
Message string `json:"message"`
|
|
} `json:"errors"`
|
|
}
|
|
if err2 := json.Unmarshal(raw2, &wrapper2); err2 != nil {
|
|
return err2
|
|
}
|
|
if len(wrapper2.Errors) > 0 {
|
|
return fmt.Errorf("mangahub: %s (after key refresh)", wrapper2.Errors[0].Message)
|
|
}
|
|
return json.Unmarshal(wrapper2.Data, out)
|
|
}
|
|
return fmt.Errorf("mangahub: %s", msg)
|
|
}
|
|
return json.Unmarshal(wrapper.Data, out)
|
|
}
|
|
|
|
type searchRow struct {
|
|
Slug string `json:"slug"`
|
|
Title string `json:"title"`
|
|
Image string `json:"image"`
|
|
}
|
|
|
|
func (s *Source) fetchList(ctx context.Context, page int, mod, query string) (source.MangasPage, error) {
|
|
gqlQuery := fmt.Sprintf(
|
|
`{search(x: %s, q: %q, genre: "all", mod: %s, offset: %d) {rows{title,slug,image}}}`,
|
|
s.src(), query, mod, (page-1)*30,
|
|
)
|
|
var result struct {
|
|
Search struct {
|
|
Rows []searchRow `json:"rows"`
|
|
} `json:"search"`
|
|
}
|
|
if err := s.gql(ctx, gqlQuery, &result); err != nil {
|
|
return source.MangasPage{}, err
|
|
}
|
|
mangas := make([]source.SManga, len(result.Search.Rows))
|
|
for i, m := range result.Search.Rows {
|
|
mangas[i] = source.SManga{
|
|
URL: "/manga/" + m.Slug,
|
|
Title: m.Title,
|
|
ThumbnailURL: thumbCDNURL + "/" + m.Image,
|
|
}
|
|
}
|
|
return source.MangasPage{Mangas: mangas, HasNextPage: len(result.Search.Rows) == 30}, nil
|
|
}
|
|
|
|
func (s *Source) GetPopularManga(page int) (source.MangasPage, error) {
|
|
return s.fetchList(context.Background(), page, "POPULAR", "")
|
|
}
|
|
|
|
func (s *Source) GetLatestUpdates(page int) (source.MangasPage, error) {
|
|
return s.fetchList(context.Background(), page, "LATEST", "")
|
|
}
|
|
|
|
func (s *Source) GetSearchManga(page int, query string, _ []source.Filter) (source.MangasPage, error) {
|
|
return s.fetchList(context.Background(), page, "POPULAR", query)
|
|
}
|
|
|
|
func (s *Source) GetMangaDetails(manga source.SManga) (source.SManga, error) {
|
|
slug := strings.TrimPrefix(manga.URL, "/manga/")
|
|
query := fmt.Sprintf(
|
|
`{manga(x: %s, slug: %q) {title,slug,status,image,author,artist,genres,description,alternativeTitle}}`,
|
|
s.src(), slug,
|
|
)
|
|
var result struct {
|
|
Manga struct {
|
|
Title string `json:"title"`
|
|
Status string `json:"status"`
|
|
Image string `json:"image"`
|
|
Author string `json:"author"`
|
|
Artist string `json:"artist"`
|
|
Genres string `json:"genres"`
|
|
Description string `json:"description"`
|
|
AlternativeTitle string `json:"alternativeTitle"`
|
|
} `json:"manga"`
|
|
}
|
|
if err := s.gql(context.Background(), query, &result); err != nil {
|
|
return manga, err
|
|
}
|
|
m := result.Manga
|
|
desc := m.Description
|
|
if m.AlternativeTitle != "" {
|
|
if desc != "" {
|
|
desc += "\n\n"
|
|
}
|
|
desc += "Alternative Name: " + m.AlternativeTitle
|
|
}
|
|
return source.SManga{
|
|
URL: manga.URL,
|
|
Title: m.Title,
|
|
Author: m.Author,
|
|
Artist: m.Artist,
|
|
Description: desc,
|
|
Genre: m.Genres,
|
|
Status: mangahubStatus(m.Status),
|
|
ThumbnailURL: thumbCDNURL + "/" + m.Image,
|
|
}, nil
|
|
}
|
|
|
|
func mangahubStatus(s string) int {
|
|
switch s {
|
|
case "ongoing":
|
|
return source.StatusOngoing
|
|
case "completed":
|
|
return source.StatusCompleted
|
|
default:
|
|
return source.StatusUnknown
|
|
}
|
|
}
|
|
|
|
func (s *Source) GetChapterList(manga source.SManga) ([]source.SChapter, error) {
|
|
slug := strings.TrimPrefix(manga.URL, "/manga/")
|
|
query := fmt.Sprintf(
|
|
`{manga(x: %s, slug: %q) {slug,chapters{number,title,date}}}`,
|
|
s.src(), slug,
|
|
)
|
|
var result struct {
|
|
Manga struct {
|
|
Slug string `json:"slug"`
|
|
Chapters []struct {
|
|
Number float32 `json:"number"`
|
|
Title string `json:"title"`
|
|
Date string `json:"date"`
|
|
} `json:"chapters"`
|
|
} `json:"manga"`
|
|
}
|
|
if err := s.gql(context.Background(), query, &result); err != nil {
|
|
return nil, err
|
|
}
|
|
chapters := make([]source.SChapter, len(result.Manga.Chapters))
|
|
for i, ch := range result.Manga.Chapters {
|
|
numStr := formatChNum(ch.Number)
|
|
chapters[i] = source.SChapter{
|
|
URL: fmt.Sprintf("/%s/chapter-%s", result.Manga.Slug, numStr),
|
|
Name: buildChapterName(ch.Title, numStr),
|
|
ChapterNumber: ch.Number,
|
|
DateUpload: util.ParseAbsoluteDate(ch.Date, "2006-01-02T15:04:05.000Z"),
|
|
}
|
|
}
|
|
// API returns ASC; reverse to newest-first.
|
|
for i, j := 0, len(chapters)-1; i < j; i, j = i+1, j-1 {
|
|
chapters[i], chapters[j] = chapters[j], chapters[i]
|
|
}
|
|
return chapters, nil
|
|
}
|
|
|
|
func formatChNum(n float32) string {
|
|
if n == float32(int(n)) {
|
|
return fmt.Sprintf("%d", int(n))
|
|
}
|
|
return fmt.Sprintf("%g", n)
|
|
}
|
|
|
|
func buildChapterName(title, number string) string {
|
|
title = strings.TrimSpace(title)
|
|
if strings.Contains(title, number) {
|
|
return title
|
|
}
|
|
if title != "" {
|
|
return "Chapter " + number + " - " + title
|
|
}
|
|
return "Chapter " + number
|
|
}
|
|
|
|
func (s *Source) GetPageList(chapter source.SChapter) ([]source.Page, error) {
|
|
// URL format: /{slug}/chapter-{number}
|
|
trimmed := strings.TrimPrefix(chapter.URL, "/")
|
|
parts := strings.SplitN(trimmed, "/", 2)
|
|
if len(parts) != 2 {
|
|
return nil, fmt.Errorf("mangahub: invalid chapter URL: %s", chapter.URL)
|
|
}
|
|
slug := parts[0]
|
|
numStr := strings.TrimPrefix(parts[1], "chapter-")
|
|
var num float64
|
|
fmt.Sscanf(numStr, "%f", &num)
|
|
|
|
query := fmt.Sprintf(`{chapter(x: %s, slug: %q, number: %g) {pages}}`, s.src(), slug, num)
|
|
var result struct {
|
|
Chapter struct {
|
|
Pages string `json:"pages"`
|
|
} `json:"chapter"`
|
|
}
|
|
if err := s.gql(context.Background(), query, &result); err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
// pages is a JSON string: {"p": "base/path/", "i": ["img1.jpg", ...]}
|
|
var pageData struct {
|
|
P string `json:"p"`
|
|
I []string `json:"i"`
|
|
}
|
|
if err := json.Unmarshal([]byte(result.Chapter.Pages), &pageData); err != nil {
|
|
return nil, fmt.Errorf("mangahub: parsing pages: %w", err)
|
|
}
|
|
pages := make([]source.Page, len(pageData.I))
|
|
for i, img := range pageData.I {
|
|
pages[i] = source.Page{Index: i, ImageURL: baseCDNURL + "/" + pageData.P + img}
|
|
}
|
|
return pages, nil
|
|
}
|
|
|
|
func (s *Source) GetImageURL(page source.Page) (string, error) { return page.ImageURL, nil }
|
|
func (s *Source) GetFilterList() []source.Filter { return nil }
|