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

315 lines
8.7 KiB
Go
Executable File

// Package goda implements the GoDa manga base.
// Popular: GET {base}/hots/page/{n}; Chapter list via {base}/manga/get?mid={id}&mode=all.
package goda
import (
"context"
"fmt"
"io"
"net/http"
"net/url"
"strings"
"github.com/PuerkitoBio/goquery"
"goyomi/internal/httpclient"
"goyomi/internal/source"
)
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("goda: HTTP %d", resp.StatusCode)
}
return goquery.NewDocumentFromReader(resp.Body)
}
func (s *Source) getRaw(ctx context.Context, rawURL string) ([]byte, 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()
return io.ReadAll(resp.Body)
}
// getKey extracts the manga key from a URL: strip /manga/ prefix and trailing /.
func getKey(href string) string {
u, err := url.Parse(href)
if err != nil {
return href
}
path := u.Path
if idx := strings.Index(path, "/manga/"); idx >= 0 {
path = path[idx+len("/manga/"):]
}
return strings.TrimSuffix(path, "/")
}
// getMangaID returns the manga ID from the #mangachapters element.
func getMangaID(doc *goquery.Document) string {
return doc.Find("#mangachapters").AttrOr("data-mid", "")
}
func (s *Source) parseMangaList(doc *goquery.Document) source.MangasPage {
var mangas []source.SManga
doc.Find(".container > .cardlist .pb-2 a").Each(func(_ int, el *goquery.Selection) {
m := source.SManga{}
href, _ := el.Attr("href")
m.URL = getKey(href)
m.Title = strings.TrimSpace(el.Find("h3").Text())
if img := el.Find("img").First(); img.Length() > 0 {
src := img.AttrOr("src", "")
// Some sites proxy images as ?url=...
if strings.Contains(src, "url=") {
if parsed, err := url.Parse(src); err == nil {
if u := parsed.Query().Get("url"); u != "" {
src = u
}
}
}
m.ThumbnailURL = src
}
if m.URL != "" && m.Title != "" {
mangas = append(mangas, m)
}
})
nextLabel := "NEXT"
if s.cfg.Lang == "zh" {
nextLabel = "下一頁"
}
hasNext := doc.Find(fmt.Sprintf("a[aria-label=%s] button", nextLabel)).Length() > 0
return source.MangasPage{Mangas: mangas, HasNextPage: hasNext}
}
func (s *Source) GetPopularManga(page int) (source.MangasPage, error) {
doc, err := s.get(context.Background(), fmt.Sprintf("%s/hots/page/%d", s.base(), page))
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/newss/page/%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) {
return s.GetPopularManga(page)
}
func (s *Source) GetMangaDetails(manga source.SManga) (source.SManga, error) {
doc, err := s.get(context.Background(), fmt.Sprintf("%s/manga/%s", s.base(), manga.URL))
if err != nil {
return manga, err
}
result := source.SManga{URL: manga.URL}
main := doc.Find("main").First()
titleEl := main.Find("h1").First()
result.Title = titleEl.Text()
if span := titleEl.Find("span").First(); span.Length() > 0 {
result.Title = strings.TrimSpace(strings.Replace(result.Title, span.Text(), "", 1))
}
if result.Title == "" {
result.Title = manga.Title
}
// Status badge is first child of h1
statusText := strings.TrimSpace(titleEl.Children().First().Text())
switch statusText {
case "連載中", "Ongoing":
result.Status = source.StatusOngoing
case "完結", "Completed":
result.Status = source.StatusCompleted
case "停止更新", "Cancelled":
result.Status = source.StatusCancelled
case "休刊", "On Hiatus":
result.Status = source.StatusHiatus
default:
result.Status = source.StatusUnknown
}
// Siblings structure: h1, then elements for author, genre1, tags, description
parent := titleEl.Parent()
children := parent.Children()
if children.Length() >= 2 {
var authorParts []string
children.Eq(1).Children().Each(func(i int, el *goquery.Selection) {
if i == 0 {
return // skip label
}
t := strings.TrimSuffix(strings.TrimSpace(el.Text()), " ,")
if t != "" {
authorParts = append(authorParts, t)
}
})
result.Author = strings.Join(authorParts, ", ")
}
var genres []string
if children.Length() >= 3 {
children.Eq(2).Children().Each(func(i int, el *goquery.Selection) {
if i == 0 {
return // skip label
}
t := strings.TrimSuffix(strings.TrimSpace(el.Text()), " ,")
if t != "" {
genres = append(genres, t)
}
})
}
if children.Length() >= 4 {
children.Eq(3).Children().Each(func(_ int, el *goquery.Selection) {
t := strings.TrimPrefix(strings.TrimSpace(el.Text()), "#")
if t != "" {
genres = append(genres, t)
}
})
}
result.Genre = strings.Join(genres, ", ")
mangaID := getMangaID(doc)
var descParts []string
if children.Length() >= 5 {
descParts = append(descParts, strings.TrimSpace(children.Eq(4).Text()))
}
if mangaID != "" {
descParts = append(descParts, "ID: "+mangaID)
}
result.Description = strings.Join(descParts, "\n\n")
main.Find("img.object-cover").First().Each(func(_ int, img *goquery.Selection) {
result.ThumbnailURL, _ = img.Attr("src")
})
return result, nil
}
func (s *Source) GetChapterList(manga source.SManga) ([]source.SChapter, error) {
// Extract manga ID from description or re-fetch.
mangaID := ""
if desc := manga.Description; desc != "" {
if idx := strings.LastIndex(desc, "ID: "); idx >= 0 {
mangaID = strings.TrimSpace(desc[idx+4:])
}
}
if mangaID == "" {
doc, err := s.get(context.Background(), fmt.Sprintf("%s/manga/%s", s.base(), manga.URL))
if err != nil {
return nil, err
}
mangaID = getMangaID(doc)
}
if mangaID == "" {
return nil, fmt.Errorf("goda: could not find manga ID for %s", manga.URL)
}
body, err := s.getRaw(context.Background(), fmt.Sprintf("%s/manga/get?mid=%s&mode=all", s.base(), mangaID))
if err != nil {
return nil, err
}
doc, err := goquery.NewDocumentFromReader(strings.NewReader(string(body)))
if err != nil {
return nil, err
}
var chapters []source.SChapter
doc.Find(".chapteritem").Each(func(_ int, el *goquery.Selection) {
a := el.Find("a").First()
href, _ := a.Attr("href")
key := getKey(href)
chKey := a.AttrOr("data-cs", "")
name := a.AttrOr("data-ct", "")
if key == "" {
return
}
chapters = append(chapters, source.SChapter{
URL: key + "#" + mangaID + "/" + chKey,
Name: name,
})
})
// reverse: chapters come latest-first from API
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 (s *Source) GetPageList(chapter source.SChapter) ([]source.Page, error) {
// URL format: {key}#{mangaId}/{chapterId}
id := ""
if idx := strings.LastIndex(chapter.URL, "#"); idx >= 0 {
id = chapter.URL[idx+1:]
}
mangaID := ""
chapterID := ""
if slashIdx := strings.Index(id, "/"); slashIdx >= 0 {
mangaID = id[:slashIdx]
chapterID = id[slashIdx+1:]
}
if mangaID == "" || chapterID == "" {
return nil, fmt.Errorf("goda: invalid chapter URL: %s", chapter.URL)
}
doc, err := s.get(context.Background(),
fmt.Sprintf("%s/chapter/getcontent?m=%s&c=%s", s.base(), mangaID, chapterID))
if err != nil {
return nil, err
}
var pages []source.Page
doc.Find("#chapcontent > div > img").Each(func(i int, img *goquery.Selection) {
u := img.AttrOr("data-src", "")
if u == "" {
u, _ = img.Attr("src")
}
if u != "" {
pages = append(pages, source.Page{Index: i, ImageURL: 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 }