mangasee-go/mangasee.go

147 lines
2.6 KiB
Go

package mangasee
import (
"embed"
"encoding/json"
"fmt"
"net/http"
"strconv"
"strings"
"github.com/PuerkitoBio/goquery"
"github.com/headzoo/surf"
"github.com/headzoo/surf/browser"
"github.com/mmcdole/gofeed/rss"
"rogchap.com/v8go"
)
//go:embed underscore-min.js
//go:embed fakequery.js
var jslib embed.FS
type App struct {
vm *v8go.Isolate
browser *browser.Browser
fp rss.Parser
http *http.Client
js [][]byte
}
type MangaSeeChapter struct {
Chapter string
Type string
Page string
Date string
ChapterName string
Directory string
}
func ChapterImage(chstr string) string {
ch := chstr[1 : len(chstr)-1]
odd := chstr[len(chstr)-1:]
if odd == "0" {
return ch
} else {
return fmt.Sprintf("%s.%s", ch, odd)
}
}
func (app *App) GetFeed(uri string) (feed *rss.Feed, err error) {
resp, err := app.http.Get(uri)
if err != nil {
return
}
feed, err = app.fp.Parse(resp.Body)
if err != nil {
return
}
return
}
func (app *App) GetChapterImages(uri string) (pages []string, err error) {
vm := v8go.NewContext(app.vm)
defer vm.Close()
err = app.browser.Open(uri)
if err != nil {
return
}
for _, f := range app.js {
_, err = vm.RunScript(string(f), "app.js")
if err != nil {
return
}
}
app.browser.Dom().Find("script").Each(func(_ int, s *goquery.Selection) {
if strings.Contains(s.Text(), "function MainFunction($http, $timeout){") {
_, err := vm.RunScript(s.Text(), "main.js")
if err != nil {
return
}
}
})
_, err = vm.RunScript("MainFunction()", "main.js")
if err != nil {
return
}
curpathname, err := vm.RunScript("CurPathName", "main.js")
if err != nil {
return
}
chidx, err := vm.RunScript("IndexName", "main.js")
if err != nil {
return
}
chjson, err := vm.RunScript("JSON.stringify(CurChapter)", "main.js")
if err != nil {
return
}
var ch MangaSeeChapter
err = json.Unmarshal([]byte(chjson.String()), &ch)
page, err := strconv.Atoi(ch.Page)
if err != nil {
return
}
for i := 1; i <= page; i++ {
pages = append(pages, fmt.Sprintf("https://%s/manga/%s/%s-%.3d.png", curpathname.String(), chidx.String(), ChapterImage(ch.Chapter), i))
}
return
}
func New() (app *App, err error) {
app = new(App)
underscore, err := jslib.ReadFile("underscore-min.js")
if err != nil {
return
}
fq, err := jslib.ReadFile("fakequery.js")
if err != nil {
return
}
app.js = append(app.js, underscore)
app.js = append(app.js, fq)
app.http = &http.Client{}
app.vm = v8go.NewIsolate()
app.browser = surf.NewBrowser()
app.fp = rss.Parser{}
return
}