-
Notifications
You must be signed in to change notification settings - Fork 6
/
kvsplayer.go
237 lines (201 loc) · 6.03 KB
/
kvsplayer.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
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
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
package kvsplayer
import (
"cmp"
"errors"
"fmt"
"math"
"regexp"
"slices"
"strconv"
"strings"
"github.com/gan-of-culture/get-sauce/request"
"github.com/gan-of-culture/get-sauce/static"
)
/*
Special Thanks to rigstot https://github.com/ytdl-patched/ytdl-patched/commits?author=rigstot for the python based template https://github.com/ytdl-patched/ytdl-patched/commit/a318f59d14792d25b2206c3f50181e03e8716db7
Further documentation on the kvsplayer can be found here: https://www.kernel-scripts.com/en/documentation/player/
*/
var reHasKVSPlayer = regexp.MustCompile(`<script [^>]*?src="https://.+?/kt_player\.js\?v=(?P<ver>(?P<maj_ver>\d+)(\.\d+)+)".*?>`)
var reFlashVars = regexp.MustCompile(`var\s+(?:flashvars|[\w]{11})\s*=[\s\S]*?};`)
var reFlashVarsValues = regexp.MustCompile(`(\w+): ['"](.*?)['"],`)
var reTitle = regexp.MustCompile(`<link href="https?://[^"]+/(.+?)/?" rel="canonical"\s*/?>`)
type extractor struct{}
// New returns a kvsplayer extractor.
func New() static.Extractor {
return &extractor{}
}
// Extract from URL
func (e *extractor) Extract(URL string) ([]*static.Data, error) {
htmlString, err := request.Get(URL)
if err != nil {
return nil, err
}
data, err := ExtractFromHTML(&htmlString)
if err != nil {
return nil, err
}
data[0].URL = URL
return data, nil
}
// ExtractFromHTML as usable utility for other scrapers
func ExtractFromHTML(htmlString *string) ([]*static.Data, error) {
matchedKVSPlayer := reHasKVSPlayer.FindAllStringSubmatch(*htmlString, -1) // 1=Vers 2=Major Version 3=Sub Version
if len(matchedKVSPlayer) < 1 {
return nil, static.ErrDataSourceParseFailed
}
switch matchedKVSPlayer[0][2] {
case "4", "5", "6", "8", "9", "11", "12", "13", "15":
break
default:
fmt.Printf("Untested major version (%s) in player engine--Download may fail.", matchedKVSPlayer[0][2])
}
flashvars, err := parseFlashVars(htmlString)
if err != nil {
return nil, err
}
matchedTitle := reTitle.FindStringSubmatch(*htmlString)
if len(matchedTitle) < 1 {
return nil, errors.New("no title found in 'cronical' URL link")
}
ext := "mp4"
if val, ok := flashvars["postfix"]; ok {
splitVal := strings.Split(val, ".") // post.mp4 to mp4
if len(splitVal) > 1 {
ext = splitVal[1]
}
}
quality := ""
streams := map[string]*static.Stream{}
dataLen := 0 //number of possible streams
var flashvarsVideoURL = []string{"video_url", "video_alt_url", "video_alt_url2", "video_alt_url3", "video_alt_url4"}
for _, key := range flashvarsVideoURL {
if _, ok := flashvars[key]; ok {
dataLen += 1
}
}
for i, key := range flashvarsVideoURL {
if _, ok := flashvars[key]; !ok {
continue
}
if !strings.Contains(flashvars[key], "/get_file") {
continue
}
rURL, err := getRealURL(flashvars[key], flashvars["license_code"])
if err != nil {
return nil, err
}
if val, ok := flashvars[key+"_text"]; ok {
quality = val
}
size, _ := request.Size(rURL, rURL)
streams[fmt.Sprint(dataLen-i-1)] = &static.Stream{
Type: static.DataTypeVideo,
URLs: []*static.URL{
{
URL: rURL,
Ext: ext,
},
},
Quality: quality,
Size: size,
}
}
return []*static.Data{
{
Site: "https://kvsplayer.com/",
Title: matchedTitle[1],
Type: static.DataTypeVideo,
Streams: streams,
},
}, nil
}
func getRealURL(videoURL, licenseCode string) (string, error) {
if !strings.HasPrefix(videoURL, "function/0/") {
return videoURL, nil //not obfuscated
}
splitURL := strings.Split(videoURL, "?")
lenSplitURL := len(splitURL)
URLPath := splitURL[0]
URLQuery := ""
if lenSplitURL > 1 {
URLQuery = splitURL[1]
}
URLParts := strings.Split(URLPath, "/")[2:]
license := getLicenseToken(licenseCode)
newMagic := URLParts[5][:32]
for o := len(newMagic) - 1; o > -1; o-- {
new := ""
lNum := 0
nAsInt := 0
for _, n := range license[o:] {
nAsInt, _ = strconv.Atoi(string(n)) //conv to single digit string : warn OK
lNum += nAsInt
}
l := (o + lNum) % 32
for i := 0; i < len(newMagic); i++ {
if i == o {
new += string(newMagic[l])
} else if i == l {
new += string(newMagic[o])
} else {
new += string(newMagic[i])
}
}
newMagic = new
}
URLParts[5] = newMagic + URLParts[5][32:]
return fmt.Sprintf("%s?%s", strings.Join(URLParts, "/"), URLQuery), nil //add ending "?" to ignore the rnd param
}
func getLicenseToken(license string) string {
modlicense := strings.ReplaceAll(strings.ReplaceAll(license, "$", ""), "0", "1")
center := len(modlicense) / 2
fronthalf, err := strconv.Atoi(modlicense[:center+1])
if err != nil {
return ""
}
backhalf, err := strconv.Atoi(modlicense[center:])
if err != nil {
return ""
}
modlicense = fmt.Sprint(4 * int(math.Abs(float64(fronthalf-backhalf))))
retVal := ""
val1 := 0
val2 := 0
for o := 0; o < center+1; o++ {
for i := 1; i < 5; i++ {
val1, _ = strconv.Atoi(string(license[o+i]))
val2, _ = strconv.Atoi(string(modlicense[o]))
retVal += fmt.Sprint((val1 + val2) % 10)
}
}
return retVal
}
func parseFlashVars(htmlString *string) (map[string]string, error) {
matchedHtmlFlashvars := reFlashVars.FindAllString(*htmlString, -1)
slices.SortFunc(matchedHtmlFlashvars, func(a, b string) int {
return cmp.Compare(len(a), len(b))
})
slices.Reverse(matchedHtmlFlashvars)
idxFlashVars := slices.IndexFunc(matchedHtmlFlashvars, func(matchedString string) bool {
return strings.HasPrefix(matchedString, "var flashvars")
})
// if one of the matches starts with flashvars -> take that one
// if said match is already at idx 0 you don't need to reassign
// if idx == -1 move on normally
if idxFlashVars > 0 {
matchedHtmlFlashvars[0] = matchedHtmlFlashvars[idxFlashVars]
}
htmlFlashvars := matchedHtmlFlashvars[0]
if htmlFlashvars == "" {
return nil, static.ErrDataSourceParseFailed
}
matchedFlashVarsValues := reFlashVarsValues.FindAllStringSubmatch(htmlFlashvars, -1) //1=key 2=val
if len(matchedFlashVarsValues) < 1 {
return nil, static.ErrDataSourceParseFailed
}
flashvars := map[string]string{}
for _, val := range matchedFlashVarsValues {
flashvars[val[1]] = val[2]
}
return flashvars, nil
}