-
Notifications
You must be signed in to change notification settings - Fork 10
/
hentai2read.go
executable file
·113 lines (93 loc) · 2.27 KB
/
hentai2read.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
package hentai2read
import (
"encoding/json"
"html"
"regexp"
"strings"
"github.com/gan-of-culture/get-sauce/request"
"github.com/gan-of-culture/get-sauce/static"
"github.com/gan-of-culture/get-sauce/utils"
)
type gData struct {
Title string
Index int
Images []string
PreloadLimit int
MainURL string
}
const site = "https://hentai2read.com/"
const cdn = "https://static.hentaicdn.com/hentai"
var reJSONString = regexp.MustCompile(`{\s*'title'[\s\S]*?}`)
var reTitle = regexp.MustCompile(`[^[(|]*`)
type extractor struct{}
// New returns a hentai2read extractor.
func New() static.Extractor {
return &extractor{}
}
func (e *extractor) Extract(URL string) ([]*static.Data, error) {
URLs := parseURL(URL)
if len(URLs) == 0 {
return nil, static.ErrURLParseFailed
}
data := []*static.Data{}
for _, u := range URLs {
d, err := extractData(u)
if err != nil {
return nil, utils.Wrap(err, u)
}
data = append(data, d)
}
return data, nil
}
func parseURL(URL string) []string {
URL = strings.Split(URL, "#")[0]
if strings.Contains(URL, "_") {
return []string{URL}
}
htmlString, err := request.Get(URL)
if err != nil {
return []string{}
}
re := regexp.MustCompile(`([^/]*/)" class="title"`)
URLs := []string{}
for _, u := range re.FindAllStringSubmatch(htmlString, -1) {
URLs = append(URLs, site+u[1])
}
return URLs
}
func extractData(URL string) (*static.Data, error) {
htmlString, err := request.Get(URL + "1/")
if err != nil {
return nil, err
}
jsonString := strings.ReplaceAll(reJSONString.FindString(htmlString), "'", `"`)
galleryData := gData{}
err = json.Unmarshal([]byte(jsonString), &galleryData)
if err != nil {
return nil, err
}
title := html.UnescapeString(strings.TrimSpace(reTitle.FindString(galleryData.Title)))
return &static.Data{
Site: site,
Title: title,
Type: "image",
Streams: map[string]*static.Stream{
"0": {
Type: static.DataTypeImage,
URLs: buildFullImgURL(galleryData.Images),
Size: 0,
},
},
URL: URL,
}, nil
}
func buildFullImgURL(URIParts []string) []*static.URL {
out := []*static.URL{}
for _, URIPart := range URIParts {
out = append(out, &static.URL{
URL: cdn + URIPart,
Ext: utils.GetLastItemString(strings.Split(URIPart, ".")),
})
}
return out
}