-
Notifications
You must be signed in to change notification settings - Fork 7
/
hentaidude.go
executable file
·147 lines (123 loc) · 3.24 KB
/
hentaidude.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
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
package hentaidude
import (
"encoding/json"
"errors"
"fmt"
"io"
"net/http"
"net/url"
"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 streams struct {
Success bool `json:"success"`
Sources map[string]string `json:"sources"`
}
const site = "https://hentaidude.com/"
const api = "https://hentaidude.com/wp-admin/admin-ajax.php"
const apiPost = "https://hentaidude.com/?p="
var reJSONParams = regexp.MustCompile(`id: '(\d*)',\s*nonce: '([^']*)`)
type extractor struct{}
// New returns a hentaidude 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 {
if ok, _ := regexp.MatchString(`hentaidude\.com/.*(?:(?:/tag/)|(?:/3d-hentai[^/]*/)|(?:page/\d/)|(?:\?*orderby=)|(?:\?*tid=))`, URL); ok || URL == site {
htmlString, err := request.Get(URL)
if err != nil {
return []string{}
}
re := regexp.MustCompile(`post-([^"]*)`)
URLs := []string{}
for _, v := range re.FindAllStringSubmatch(htmlString, -1) {
URLs = append(URLs, apiPost+v[1])
}
return URLs
}
return []string{URL}
}
func extractData(URL string) (*static.Data, error) {
htmlString, err := request.Get(URL)
if err != nil {
return nil, err
}
title := utils.GetMeta(&htmlString, "og:title")
title = strings.TrimSuffix(title, " - Hentaidude.com")
matchedSourceReq := reJSONParams.FindStringSubmatch(htmlString) // 1=id 2=nonce
if len(matchedSourceReq) < 3 {
return nil, errors.New("can't locate json params in URL")
}
headers := map[string]string{
"Accept": "*/*",
"Content-Type": "application/x-www-form-urlencoded",
"Referer": site,
}
params := url.Values{}
params.Add("action", "msv-get-sources")
params.Add("id", matchedSourceReq[1])
params.Add("nonce", matchedSourceReq[2])
res, err := request.Request(http.MethodPost, api, headers, strings.NewReader(params.Encode()))
if err != nil {
return nil, err
}
defer res.Body.Close()
body, err := io.ReadAll(res.Body)
if err != nil {
return nil, err
}
sources := &streams{}
err = json.Unmarshal(body, &sources)
if err != nil {
return nil, err
}
if !sources.Success {
return nil, errors.New("the api request for the streams did not return successful for")
}
streams := make(map[string]*static.Stream)
for _, source := range sources.Sources {
headers, err := request.Headers(source, source)
if err != nil {
return nil, err
}
size, err := request.GetSizeFromHeaders(&headers)
if err != nil {
return nil, err
}
streams[fmt.Sprint(len(streams))] = &static.Stream{
Type: static.DataTypeVideo,
URLs: []*static.URL{
{
URL: source,
Ext: strings.Split(headers.Get("content-type"), "/")[1],
},
},
Size: size,
}
}
return &static.Data{
Site: site,
Title: title,
Type: static.DataTypeVideo,
Streams: streams,
URL: URL,
}, nil
}