-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathhanime.go
More file actions
203 lines (177 loc) · 4.69 KB
/
Copy pathhanime.go
File metadata and controls
203 lines (177 loc) · 4.69 KB
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
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
package main
import (
"bytes"
"flag"
"io/ioutil"
"net/http"
"net/url"
"os"
"path/filepath"
"regexp"
"strings"
ss "strings"
"time"
"github.com/lilacre/hanime/downloader"
"github.com/shiena/ansicolor"
"github.com/sirupsen/logrus"
m3u8 "github.com/grafov/m3u8"
)
const (
iv = "0123456701234567"
host = "https://hanime.tv"
)
var (
targetURL string
proxyURL string
outputFILE string
storagePATH string
tmpPATH string
outputFILEPATH string
logger *logrus.Logger
)
func makeDirIfNotExists(path string) error {
if _, err := os.Stat(path); os.IsNotExist(err) {
return os.Mkdir(path, os.ModeDir|0755)
}
return nil
}
func fileExists(outputPATH string) bool {
info, err := os.Stat(outputPATH)
if os.IsNotExist(err) {
return false
}
return !info.IsDir()
}
func init() {
flag.StringVar(&targetURL, "url", "", "hanime hentai url")
flag.StringVar(&outputFILE, "output", "", "output file name")
flag.StringVar(&proxyURL, "proxy", "", "proxy forwarding")
flag.StringVar(&storagePATH, "dir", "", "dir to store file")
flag.Parse()
// Config logger
logger = logrus.New()
logger.SetLevel(logrus.InfoLevel)
logger.SetFormatter(&logrus.TextFormatter{
ForceColors: true,
})
logger.SetOutput(ansicolor.NewAnsiColorWriter(os.Stdout))
if targetURL == "" {
logger.Panicln("you must specify a url to download hentai from hanime.tv")
}
if storagePATH == "" {
logger.Panicln("you must specify a directory path to save file")
}
if i := strings.Index(targetURL, "?"); i > -1 {
targetURL = targetURL[:i]
}
if outputFILE == "" {
strLIST := ss.Split(targetURL, "/")
outputFILE = strLIST[len(strLIST)-1]
}
// if outputFILE == "" {
// strLIST := make([]string, 2, 2)
// if i := strings.Index(targetURL, "?"); i > -1 {
// strLIST = ss.Split(targetURL[:i], "/")
// } else {
// strLIST = ss.Split(targetURL, "/")
// }
// outputFILE = strLIST[len(strLIST)-1]
// }
outputFILEPATH = filepath.Join(storagePATH, outputFILE+".mp4")
if i := fileExists(outputFILEPATH); i == true {
logger.Panicln("file " + outputFILEPATH + " exists")
}
tmpPATH = filepath.Join(storagePATH, outputFILE+"_tmp")
makeDirIfNotExists(storagePATH)
makeDirIfNotExists(tmpPATH)
}
func getM3U8URL(client *http.Client, targetURL string) (string, error) {
request, err := http.NewRequest("GET", targetURL, nil)
request.Header.Add("User-Agent", `Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/84.0.4147.105 Safari/537.36`)
request.Header.Add("Origin", `https://hanime.tv`)
request.Header.Add("Accept", `text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,image/apng,*/*;q=0.8,application/signed-exchange;v=b3;q=0.9`)
request.Header.Add("upgrade-insecure-requests", `1`)
if err != nil {
return "", err
}
resp, err := client.Do(request)
defer func() {
if err := resp.Body.Close(); err != nil {
logger.Panicln(err)
}
}()
body, err := ioutil.ReadAll(resp.Body)
if err != nil {
return "", err
}
re := regexp.MustCompile(`[0-9]+\.m3u8`)
result := re.FindString(string(body))
logger.Infoln(string(result))
return `https://weeb.hanime.tv/weeb-api-cache/api/v8/m3u8s/` + string(result), nil
}
func main() {
logger.Infoln("start")
tr := &http.Transport{
IdleConnTimeout: 30 * time.Second,
DisableCompression: true,
}
if proxyURL != "" {
proxy, err := url.Parse(proxyURL)
if err != nil {
logger.Panicln(err)
}
tr.Proxy = http.ProxyURL(proxy)
}
client := &http.Client{Transport: tr}
// get m3u8 url
m3u8URL, err := getM3U8URL(client, targetURL)
if err != nil {
logger.Panicln(err)
}
// end get m3u8 url
// get m3u8
resp, err := client.Get(m3u8URL)
if err != nil {
logger.Panicln(err)
}
body, err := ioutil.ReadAll(resp.Body)
if err != nil {
logger.Panicln(err)
}
resp.Body.Close()
// end get m3u8
raw := bytes.NewBuffer(body)
p, listType, err := m3u8.Decode(*raw, true)
if err != nil {
logger.Panicln(err)
}
// var playlists []string
switch listType {
case m3u8.MEDIA:
mediapl := p.(*m3u8.MediaPlaylist)
// log.Printf("%+v\n", mediapl)
segmentsURLRAW := mediapl.Segments
segmentsURL := make([]string, 0, len(segmentsURLRAW))
for _, v := range segmentsURLRAW {
if v != nil {
segmentsURL = append(segmentsURL, v.URI)
}
}
filelistsPATH := filepath.Join(tmpPATH, "filelists.txt")
dl := downloader.Downloader{
Client: client,
SegmentURL: segmentsURL,
StoragePath: storagePATH,
FilelistsPath: filelistsPATH,
OutputFilePath: outputFILEPATH,
TmpPATH: tmpPATH,
MaxPROCS: 100,
IV: iv,
Logger: logger,
}
dl.Download()
case m3u8.MASTER:
masterpl := p.(*m3u8.MasterPlaylist)
logger.Infof("%+v\n", masterpl)
}
}