mangasee-go/mangasee.go

147 lines
2.6 KiB
Go
Raw Normal View History

2022-06-30 11:53:35 +00:00
package mangasee
2022-06-30 08:20:29 +00:00
import (
2022-06-30 11:53:35 +00:00
"embed"
2022-06-30 08:20:29 +00:00
"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"
)
2022-06-30 11:53:35 +00:00
//go:embed underscore-min.js
//go:embed fakequery.js
var jslib embed.FS
2022-06-30 08:20:29 +00:00
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
}
2022-06-30 11:53:35 +00:00
func (app *App) GetChapterImages(uri string) (pages []string, err error) {
2024-05-08 22:19:15 +00:00
vm := v8go.NewContext(app.vm)
2022-06-30 11:53:35 +00:00
defer vm.Close()
2022-06-30 08:20:29 +00:00
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++ {
2022-06-30 11:53:35 +00:00
pages = append(pages, fmt.Sprintf("https://%s/manga/%s/%s-%.3d.png", curpathname.String(), chidx.String(), ChapterImage(ch.Chapter), i))
2022-06-30 08:20:29 +00:00
}
return
}
2022-06-30 11:53:35 +00:00
func New() (app *App, err error) {
app = new(App)
2022-06-30 08:20:29 +00:00
2022-06-30 11:53:35 +00:00
underscore, err := jslib.ReadFile("underscore-min.js")
2022-06-30 08:20:29 +00:00
if err != nil {
2022-06-30 11:53:35 +00:00
return
2022-06-30 08:20:29 +00:00
}
2022-06-30 11:53:35 +00:00
fq, err := jslib.ReadFile("fakequery.js")
2022-06-30 08:20:29 +00:00
if err != nil {
2022-06-30 11:53:35 +00:00
return
2022-06-30 08:20:29 +00:00
}
app.js = append(app.js, underscore)
app.js = append(app.js, fq)
app.http = &http.Client{}
2024-05-08 22:19:15 +00:00
app.vm = v8go.NewIsolate()
2022-06-30 08:20:29 +00:00
app.browser = surf.NewBrowser()
app.fp = rss.Parser{}
2022-06-30 11:53:35 +00:00
return
2022-06-30 08:20:29 +00:00
}