This repository has been archived by the owner on Aug 7, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 8
/
parser.go
113 lines (95 loc) · 3.37 KB
/
parser.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 main
import (
"bufio"
"errors"
"fmt"
"io/ioutil"
"net/http"
"net/url"
"os"
"path"
"path/filepath"
"strconv"
"strings"
"github.com/grafov/m3u8"
)
func (h *bahamut) askForSN() {
var sn string
sn = h.sn
for sn == "" {
fmt.Println("Please type anime sn id: ")
fmt.Scanln(&sn)
}
// extract URL input
_, err := strconv.Atoi(sn)
if err != nil {
if strings.Contains(sn, "animeVideo") {
u, err := url.Parse(sn)
isErr("Extract url failed -", err)
qStr, err := url.ParseQuery(u.RawQuery)
isErr("Parse query string failed -", err)
if qStr.Get("sn") == "" {
isErr("Extract sn id failed -", errors.New("sn id not found"))
}
sn = qStr.Get("sn")
} else {
isErr("Please try again -", errors.New("format not corret, Allow: numeric / URL"))
}
}
h.sn = sn
}
func (h *bahamut) parseMasterList() {
req, err := http.NewRequest("GET", h.mUrl, nil)
isErr("Create request failed - ", err)
req.Header.Add("cookie", h.rawCookie)
req.Header.Add("user-agent", "Mozilla/5.0 (Windows NT 6.1; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/77.0.3865.120 Safari/537.36")
req.Header.Add("referer", "https://ani.gamer.com.tw/animeVideo.php?sn="+h.sn)
req.Header.Add("origin", "https://ani.gamer.com.tw")
resp, err := http.DefaultClient.Do(req)
isErr("Get m3u8 playlist failed -", err)
defer resp.Body.Close()
p, listType, err := m3u8.DecodeFrom(bufio.NewReader(resp.Body), true)
isErr("Parse m3u8 playlist failed -", err)
switch listType {
case m3u8.MASTER:
masterpl := p.(*m3u8.MasterPlaylist)
var res_list []string
for _, list := range masterpl.Variants {
res := strings.Split(list.Resolution, "x")[1]
res_list = append(res_list, res+"p")
if h.quality == res + "p" {
h.res = strings.Split(list.URI, "?")[0]
}
}
if h.res == "" {
fmt.Println("Not supported resolution in this video: " + h.quality)
fmt.Println("Only support these resolution: ")
fmt.Println(res_list)
os.Exit(1)
}
}
fmt.Println("Get m3u8 playlist.")
}
func (h *bahamut) parseM3U8() {
f, err := os.Open(filepath.Join(h.tmp, h.plName))
isErr("Failed to read m3u8 playlist -", err)
pl, listType, err := m3u8.DecodeFrom(bufio.NewReader(f), true)
isErr("Parse m3u8 playlist failed -", err)
switch listType {
case m3u8.MEDIA:
mediapl := pl.(*m3u8.MediaPlaylist)
newPl, e := m3u8.NewMediaPlaylist(mediapl.WinSize(), mediapl.Count())
isErr("Creating new m3u8 playlist failed - ", e)
prefix := strings.Split(h.mUrl, "playlist.m3u8")[0]
newPl.SetDefaultKey(mediapl.Key.Method, h.downloadKey(mediapl.Key.URI), "", "", "")
for _, chuck := range mediapl.Segments {
if chuck != nil {
h.chuckList = append(h.chuckList, prefix+chuck.URI)
newPl.Append(strings.Split(path.Base(chuck.URI), "?")[0], chuck.Duration, "")
}
}
newPl.Close()
ioutil.WriteFile(filepath.Join(h.tmp, h.plName), newPl.Encode().Bytes(), 0755)
fmt.Println("All segments parsed! Download is starting...")
}
}