263 lines
7.4 KiB
Go
263 lines
7.4 KiB
Go
// Package multichan implements the MultiChan manga base.
|
|
// Russian manga site; pages extracted from fullimg JSON array in HTML source.
|
|
package multichan
|
|
|
|
import (
|
|
"context"
|
|
"fmt"
|
|
"io"
|
|
"net/http"
|
|
"strings"
|
|
"time"
|
|
|
|
"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(2, 3))
|
|
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("multichan: HTTP %d", resp.StatusCode)
|
|
}
|
|
return goquery.NewDocumentFromReader(resp.Body)
|
|
}
|
|
|
|
func (s *Source) getRaw(ctx context.Context, rawURL string) (string, error) {
|
|
req, err := http.NewRequestWithContext(ctx, http.MethodGet, rawURL, nil)
|
|
if err != nil {
|
|
return "", err
|
|
}
|
|
req.Header.Set("Referer", s.cfg.BaseURL)
|
|
resp, err := s.client.Do(req)
|
|
if err != nil {
|
|
return "", err
|
|
}
|
|
defer resp.Body.Close()
|
|
body, _ := io.ReadAll(resp.Body)
|
|
return string(body), nil
|
|
}
|
|
|
|
func mangaFromElement(el *goquery.Selection, baseURL string) source.SManga {
|
|
m := source.SManga{}
|
|
m.Title = strings.TrimSpace(el.AttrOr("title", ""))
|
|
el.Find("a").First().Each(func(_ int, a *goquery.Selection) {
|
|
m.URL = a.AttrOr("href", "")
|
|
if m.Title == "" {
|
|
m.Title = strings.TrimSpace(a.Text())
|
|
}
|
|
})
|
|
if img := el.Find("img").First(); img.Length() > 0 {
|
|
m.ThumbnailURL = util.AbsURL(baseURL, img.AttrOr("src", ""))
|
|
}
|
|
return m
|
|
}
|
|
|
|
func (s *Source) parseMangaList(doc *goquery.Document, nextSel string) source.MangasPage {
|
|
var mangas []source.SManga
|
|
doc.Find("div.content_row").Each(func(_ int, el *goquery.Selection) {
|
|
m := mangaFromElement(el, s.cfg.BaseURL)
|
|
if m.URL != "" && m.Title != "" {
|
|
mangas = append(mangas, m)
|
|
}
|
|
})
|
|
hasNext := doc.Find(nextSel).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/mostfavorites?offset=%d", s.base(), 20*(page-1)))
|
|
if err != nil {
|
|
return source.MangasPage{}, err
|
|
}
|
|
return s.parseMangaList(doc, "a:contains(Вперед)"), nil
|
|
}
|
|
|
|
func (s *Source) GetLatestUpdates(page int) (source.MangasPage, error) {
|
|
doc, err := s.get(context.Background(), fmt.Sprintf("%s/manga/new?offset=%d", s.base(), 20*(page-1)))
|
|
if err != nil {
|
|
return source.MangasPage{}, err
|
|
}
|
|
return s.parseMangaList(doc, "a:contains(Вперед)"), nil
|
|
}
|
|
|
|
func (s *Source) GetSearchManga(page int, query string, filters []source.Filter) (source.MangasPage, error) {
|
|
u := fmt.Sprintf("%s/?do=search&subaction=search&story=%s&search_start=%d", s.base(), query, page)
|
|
doc, err := s.get(context.Background(), u)
|
|
if err != nil {
|
|
return source.MangasPage{}, err
|
|
}
|
|
return s.parseMangaList(doc, "a:contains(Далее)"), 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 from page title (strip " » ..." suffix)
|
|
pageTitle := doc.Find("title").Text()
|
|
if idx := strings.Index(pageTitle, " »"); idx > 0 {
|
|
result.Title = strings.TrimSpace(pageTitle[:idx])
|
|
} else {
|
|
result.Title = strings.TrimSpace(pageTitle)
|
|
}
|
|
if result.Title == "" {
|
|
result.Title = manga.Title
|
|
}
|
|
|
|
infoEl := doc.Find("#info_wrap tr, #info_wrap > div")
|
|
result.Author = strings.TrimSpace(infoEl.Find(":contains(Автор) .item2").Text())
|
|
|
|
rawCat := strings.ToLower(strings.TrimSpace(infoEl.Find(":contains(Тип) a").Text()))
|
|
var tags []string
|
|
if rawCat != "" {
|
|
tags = append(tags, rawCat)
|
|
}
|
|
doc.Find(".sidetags ul a:last-child").Each(func(_ int, a *goquery.Selection) {
|
|
if t := strings.TrimSpace(a.Text()); t != "" {
|
|
tags = append(tags, t)
|
|
}
|
|
})
|
|
result.Genre = strings.Join(tags, ", ")
|
|
|
|
statusText := infoEl.Find(":contains(Загружено)").Text()
|
|
switch {
|
|
case strings.Contains(statusText, "перевод завершен"):
|
|
result.Status = source.StatusCompleted
|
|
case strings.Contains(statusText, "перевод продолжается"):
|
|
result.Status = source.StatusOngoing
|
|
default:
|
|
result.Status = source.StatusUnknown
|
|
}
|
|
|
|
if desc := doc.Find("div#description").First(); desc.Length() > 0 {
|
|
nodes := desc.Contents()
|
|
nodes.Each(func(_ int, n *goquery.Selection) {
|
|
if goquery.NodeName(n) == "#text" {
|
|
if t := strings.TrimSpace(n.Text()); t != "" {
|
|
result.Description = t
|
|
}
|
|
}
|
|
})
|
|
}
|
|
|
|
if img := doc.Find("img#cover").First(); img.Length() > 0 {
|
|
result.ThumbnailURL = util.AbsURL(s.cfg.BaseURL, img.AttrOr("src", ""))
|
|
}
|
|
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: table.table_cha tr (skip first 2 header rows)
|
|
doc.Find("table.table_cha tr").Each(func(i int, el *goquery.Selection) {
|
|
if i <= 1 {
|
|
return // skip headers
|
|
}
|
|
a := el.Find("a").First()
|
|
if a.Length() == 0 {
|
|
return
|
|
}
|
|
href := a.AttrOr("href", "")
|
|
if href == "" {
|
|
return
|
|
}
|
|
// Make URL relative
|
|
u := strings.TrimPrefix(href, s.base())
|
|
if !strings.HasPrefix(u, "/") {
|
|
u = "/" + u
|
|
}
|
|
|
|
dateStr := strings.TrimSpace(el.Find("div.date").First().Text())
|
|
chapters = append(chapters, source.SChapter{
|
|
URL: u,
|
|
Name: strings.TrimSpace(a.Text()),
|
|
DateUpload: parseDate(dateStr),
|
|
})
|
|
})
|
|
return chapters, nil
|
|
}
|
|
|
|
func parseDate(s string) int64 {
|
|
t, err := time.Parse("2006-01-02", s)
|
|
if err != nil {
|
|
return 0
|
|
}
|
|
return t.UnixMilli()
|
|
}
|
|
|
|
func (s *Source) GetPageList(chapter source.SChapter) ([]source.Page, error) {
|
|
html, err := s.getRaw(context.Background(), util.AbsURL(s.cfg.BaseURL, chapter.URL))
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
// Extract fullimg":[...] from raw HTML
|
|
const marker = `fullimg":[`
|
|
start := strings.Index(html, marker)
|
|
if start < 0 {
|
|
return nil, fmt.Errorf("multichan: fullimg array not found")
|
|
}
|
|
start += len(marker)
|
|
end := strings.Index(html[start:], ",]")
|
|
if end < 0 {
|
|
return nil, fmt.Errorf("multichan: fullimg array end not found")
|
|
}
|
|
rawURLs := html[start : start+end]
|
|
rawURLs = strings.ReplaceAll(rawURLs, `"`, "")
|
|
|
|
var pages []source.Page
|
|
for i, u := range strings.Split(rawURLs, ",") {
|
|
u = strings.TrimSpace(u)
|
|
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 }
|