-
Notifications
You must be signed in to change notification settings - Fork 6
/
hentaihd.go
131 lines (109 loc) · 2.94 KB
/
hentaihd.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
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
package hentaihd
import (
"encoding/json"
"fmt"
"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 videoInfo struct {
Success bool `json:"success"`
Data []struct {
File string `json:"file"`
Label string `json:"label"`
Type string `json:"type"`
} `json:"data"`
Captions []interface{} `json:"captions"`
IsVr bool `json:"is_vr"`
}
const site = "https://v2.hentaihd.net/"
const videoProvider = "https://amhentai.com/"
const videoProviderAPI = videoProvider + "api/source/"
var reEpisodeURL = regexp.MustCompile(site + `\d+/.+/`)
var reParseURLShow = regexp.MustCompile(site + `anime/[\w-%]+/`)
var reVideoURL = regexp.MustCompile(videoProvider + `v/([^"]+)`)
type extractor struct{}
// New returns a hentaihd extractor.
func New() static.Extractor {
return &extractor{}
}
// Extract data from URL
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 {
if ok := reEpisodeURL.MatchString(URL); ok {
return []string{URL}
}
htmlString, err := request.Get(URL)
if err != nil {
return nil
}
if strings.Contains(URL, "/anime/") {
htmlString = strings.Split(htmlString, `<div class="bixbox"`)[0]
return utils.RemoveAdjDuplicates(reEpisodeURL.FindAllString(htmlString, -1))
}
// contains list of show that need to be derefenced to episode level
htmlString = strings.Split(htmlString, `<div id="sidebar">`)[0]
out := []string{}
for _, anime := range reParseURLShow.FindAllString(htmlString, -1) {
out = append(out, parseURL(anime)...)
}
return out
}
func extractData(URL string) (*static.Data, error) {
htmlString, err := request.Get(URL)
if err != nil {
return nil, err
}
matchedVideo := reVideoURL.FindStringSubmatch(htmlString) // 0=URL 1=ID
if len(matchedVideo) < 2 {
return nil, static.ErrDataSourceParseFailed
}
videoJSON, err := request.PostAsBytesWithHeaders(videoProviderAPI+matchedVideo[1], map[string]string{"Referer": matchedVideo[0]})
if err != nil {
return nil, err
}
video := videoInfo{}
err = json.Unmarshal(videoJSON, &video)
if err != nil {
return nil, err
}
streams := map[string]*static.Stream{}
for i, s := range video.Data {
size, _ := request.Size(s.File, site)
streams[fmt.Sprint(len(video.Data)-i-1)] = &static.Stream{
Type: static.DataTypeVideo,
URLs: []*static.URL{
{
URL: s.File,
Ext: s.Type,
},
},
Quality: s.Label,
Size: size,
}
}
return &static.Data{
Site: site,
Title: html.UnescapeString(utils.GetH1(&htmlString, -1)),
Type: static.DataTypeVideo,
Streams: streams,
URL: URL,
}, nil
}