-
-
Notifications
You must be signed in to change notification settings - Fork 1.2k
Expand file tree
/
Copy pathvk.py
More file actions
148 lines (126 loc) · 4.98 KB
/
Copy pathvk.py
File metadata and controls
148 lines (126 loc) · 4.98 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
"""
$description Russian live-streaming and video hosting social platform.
$url vk.com
$url vk.ru
$type live, vod
$metadata id
$metadata author
$metadata title
"""
import re
from hashlib import md5
from urllib.parse import parse_qsl, unquote, urlparse, urlunparse
from streamlink.exceptions import NoStreamsError
from streamlink.logger import getLogger
from streamlink.plugin import Plugin, PluginError, pluginmatcher
from streamlink.plugin.api import validate
from streamlink.stream.dash import DASHStream
from streamlink.stream.hls import HLSStream
from streamlink.utils.url import update_qsd
log = getLogger(__name__)
@pluginmatcher(
name="video",
pattern=re.compile(r"https?://(?:\w+\.)?vk\.(?:com|ru)/video(?P<id>-?\d+_\d+)"),
)
@pluginmatcher(
name="default",
pattern=re.compile(r"https?://(\w+\.)?vk\.(?:com|ru)/(?!video-?\d+_\d+).+"),
)
class VK(Plugin):
API_URL = "https://vk.com/al_video.php"
HASH_COOKIE = "hash429"
def _get_cookies(self):
def on_response(res, **__):
if res.headers.get("x-waf-redirect") == "1":
if not res.headers.get("X-WAF-Backend-Status"):
log.debug("Getting WAF cookie")
cookie = res.cookies.get(self.HASH_COOKIE)
key = md5(cookie.encode("utf-8")).hexdigest()
res.headers["Location"] = update_qsd(res.headers["Location"], qsd={"key": key})
return res
elif res.headers.get("X-WAF-Backend-Status") == "challenge_success":
self.session.http.cookies.update(res.cookies)
return res
url = urlunparse(urlparse(self.url)._replace(path="", query="", fragment=""))
self.session.http.get(url, hooks={"response": on_response})
def follow_vk_redirect(self):
if self.matches["video"]:
return
try:
parsed_url = urlparse(self.url)
true_path = next(unquote(v).split("/")[0] for k, v in parse_qsl(parsed_url.query) if k == "z" and len(v) > 0)
self.url = f"{parsed_url.scheme}://{parsed_url.netloc}/{true_path}"
if self.matches["video"]:
return
except StopIteration:
pass
try:
self.url = self.session.http.get(
self.url,
schema=validate.Schema(
validate.parse_html(),
validate.xml_xpath_string(".//head/meta[@property='og:url'][@content]/@content"),
str,
),
)
except PluginError:
pass
if self.matches["video"]:
return
raise NoStreamsError
def _get_streams(self):
self._get_cookies()
self.follow_vk_redirect()
self.id = self.match["id"]
if not self.id:
return
qsd = dict(parse_qsl(urlparse(self.url).query or ""))
playlist = qsd.get("list", "")
log.debug(f"{self.id=}, {playlist=}")
data = self.session.http.post(
self.API_URL,
params={"act": "show"},
data={
"act": "show",
"al": "1",
"list": playlist,
"video": self.id,
},
headers={
"Referer": self.url,
"X-Requested-With": "XMLHttpRequest",
},
schema=validate.Schema(
validate.parse_json(),
{"payload": list},
validate.get(("payload", -1)),
list,
validate.get(-1),
validate.any(
str,
validate.all(
{"player": {"params": list}},
validate.get(("player", "params", 0)),
{
validate.optional("hls"): validate.any(None, validate.url()),
validate.optional("hls_live"): validate.any(None, validate.url()),
validate.optional("hls_ondemand"): validate.any(None, validate.url()),
validate.optional("dash_live"): validate.any(None, validate.url()),
validate.optional("dash_ondemand"): validate.any(None, validate.url()),
validate.optional("md_author"): validate.any(None, str),
validate.optional("md_title"): validate.any(None, str),
},
),
),
),
)
if type(data) is not dict:
log.error("Video is inaccessible")
return
self.author = data.get("md_author")
self.title = data.get("md_title")
if hls := data.get("hls_live") or data.get("hls_ondemand") or data.get("hls"):
return HLSStream.parse_variant_playlist(self.session, hls)
if dash := data.get("dash_live") or data.get("dash_ondemand"):
return DASHStream.parse_manifest(self.session, dash)
__plugin__ = VK