246 lines
7.3 KiB
Go
Executable File
246 lines
7.3 KiB
Go
Executable File
// Package manga18 implements the Manga18 manga base.
|
|
// HTML scraping; popular: GET {base}/list-manga/{page}?order_by=views; pages via Base64-encoded URLs in inline JS.
|
|
package manga18
|
|
|
|
import (
|
|
"context"
|
|
"encoding/base64"
|
|
"fmt"
|
|
"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(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("manga18: HTTP %d", resp.StatusCode)
|
|
}
|
|
return goquery.NewDocumentFromReader(resp.Body)
|
|
}
|
|
|
|
func (s *Source) mangaFromElement(el *goquery.Selection) source.SManga {
|
|
m := source.SManga{}
|
|
el.Find("a").First().Each(func(_ int, a *goquery.Selection) {
|
|
m.URL = a.AttrOr("href", "")
|
|
})
|
|
m.Title = strings.TrimSpace(el.Find("div.mg_info > div.mg_name a").Text())
|
|
if thumb := el.Find("img").First().AttrOr("src", ""); thumb != "" {
|
|
m.ThumbnailURL = util.AbsURL(s.cfg.BaseURL, thumb)
|
|
}
|
|
return m
|
|
}
|
|
|
|
func (s *Source) parseMangaList(doc *goquery.Document) source.MangasPage {
|
|
var mangas []source.SManga
|
|
doc.Find("div.story_item").Each(func(_ int, el *goquery.Selection) {
|
|
m := s.mangaFromElement(el)
|
|
if m.URL != "" && m.Title != "" {
|
|
mangas = append(mangas, m)
|
|
}
|
|
})
|
|
hasNext := doc.Find(".pagination > li:last-child:not(.active)").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/list-manga/%d?order_by=views", 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/list-manga/%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) {
|
|
u := fmt.Sprintf("%s/list-manga/%d?search=%s", s.base(), page, query)
|
|
doc, err := s.get(context.Background(), u)
|
|
if err != nil {
|
|
return source.MangasPage{}, err
|
|
}
|
|
return s.parseMangaList(doc), 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}
|
|
result.Title = strings.TrimSpace(doc.Find("div.detail_name > h1").Text())
|
|
if result.Title == "" {
|
|
result.Title = manga.Title
|
|
}
|
|
if thumb := doc.Find("div.detail_avatar > img").AttrOr("src", ""); thumb != "" {
|
|
result.ThumbnailURL = util.AbsURL(s.cfg.BaseURL, thumb)
|
|
}
|
|
result.Description = strings.TrimSpace(doc.Find("div.detail_reviewContent").Text())
|
|
|
|
info := doc.Find("div.detail_listInfo")
|
|
statusText := strings.TrimSpace(info.Find("div.item div.info_value").FilterFunction(func(_ int, el *goquery.Selection) bool {
|
|
return strings.Contains(el.Parent().Text(), "Status")
|
|
}).Text())
|
|
switch {
|
|
case strings.Contains(statusText, "Ongoing"):
|
|
result.Status = source.StatusOngoing
|
|
case strings.Contains(statusText, "Completed"):
|
|
result.Status = source.StatusCompleted
|
|
default:
|
|
result.Status = source.StatusUnknown
|
|
}
|
|
|
|
author := strings.TrimSpace(info.Find("div.info_value").FilterFunction(func(_ int, el *goquery.Selection) bool {
|
|
prev := el.Prev()
|
|
t := strings.ToLower(prev.Text())
|
|
return strings.Contains(t, "author") || strings.Contains(t, "autor")
|
|
}).Text())
|
|
if author != "Updating" {
|
|
result.Author = author
|
|
}
|
|
artist := strings.TrimSpace(info.Find("div.info_value").FilterFunction(func(_ int, el *goquery.Selection) bool {
|
|
return strings.Contains(strings.ToLower(el.Prev().Text()), "artist")
|
|
}).Text())
|
|
if artist != "Updating" {
|
|
result.Artist = artist
|
|
}
|
|
|
|
var genres []string
|
|
info.Find("div.info_value > a[href*=/manga-list/]").Each(func(_ int, a *goquery.Selection) {
|
|
if t := strings.TrimSpace(a.Text()); t != "" {
|
|
genres = append(genres, t)
|
|
}
|
|
})
|
|
result.Genre = strings.Join(genres, ", ")
|
|
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
|
|
doc.Find("div.chapter_box .item").Each(func(_ int, el *goquery.Selection) {
|
|
ch := source.SChapter{}
|
|
el.Find("a").First().Each(func(_ int, a *goquery.Selection) {
|
|
ch.URL = a.AttrOr("href", "")
|
|
ch.Name = strings.TrimSpace(a.Text())
|
|
})
|
|
if dateEl := el.Find("p").First(); dateEl.Length() > 0 {
|
|
ch.DateUpload = parseDate(strings.TrimSpace(dateEl.Text()))
|
|
}
|
|
if ch.URL != "" {
|
|
chapters = append(chapters, ch)
|
|
}
|
|
})
|
|
return chapters, nil
|
|
}
|
|
|
|
func parseDate(s string) int64 {
|
|
t, err := time.Parse("02-01-2006", s)
|
|
if err != nil {
|
|
return 0
|
|
}
|
|
return t.UnixMilli()
|
|
}
|
|
|
|
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 scriptData string
|
|
doc.Find("script").Each(func(_ int, el *goquery.Selection) {
|
|
if scriptData != "" {
|
|
return
|
|
}
|
|
html, _ := el.Html()
|
|
if strings.Contains(html, "slides_p_path") {
|
|
scriptData = html
|
|
}
|
|
})
|
|
if scriptData == "" {
|
|
return nil, fmt.Errorf("manga18: slides_p_path script not found")
|
|
}
|
|
|
|
start := strings.Index(scriptData, "[")
|
|
end := strings.LastIndex(scriptData, "]")
|
|
if start < 0 || end <= start {
|
|
return nil, fmt.Errorf("manga18: image array not found")
|
|
}
|
|
// Trim trailing comma before closing bracket
|
|
inner := strings.TrimRight(strings.TrimSpace(scriptData[start+1:end]), ",")
|
|
parts := strings.Split(inner, ",")
|
|
|
|
var pages []source.Page
|
|
for i, part := range parts {
|
|
encoded := strings.Trim(strings.TrimSpace(part), `"`)
|
|
if encoded == "" {
|
|
continue
|
|
}
|
|
decoded, err := base64.StdEncoding.DecodeString(encoded)
|
|
if err != nil {
|
|
decoded, err = base64.URLEncoding.DecodeString(encoded)
|
|
if err != nil {
|
|
continue
|
|
}
|
|
}
|
|
imgURL := string(decoded)
|
|
if strings.HasPrefix(imgURL, "/") {
|
|
imgURL = s.base() + imgURL
|
|
}
|
|
pages = append(pages, source.Page{Index: i, ImageURL: imgURL})
|
|
}
|
|
return pages, nil
|
|
}
|
|
|
|
func (s *Source) GetImageURL(page source.Page) (string, error) { return page.ImageURL, nil }
|
|
func (s *Source) GetFilterList() []source.Filter { return nil }
|