forked from streamlink/streamlink
/
tvp.py
184 lines (163 loc) · 6.16 KB
/
tvp.py
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
"""
$description Live TV channels and VODs from TVP, a Polish public, state-owned broadcaster.
$url stream.tvp.pl
$type live, vod
$notes Some VODs may be geo-restricted. Authentication is not supported.
"""
import logging
import re
from typing import List, Optional, Tuple
from streamlink.compat import urlparse
from streamlink.plugin import Plugin, pluginmatcher
from streamlink.plugin.api import validate
from streamlink.stream.dash import DASHStream
from streamlink.stream.hls import HLSStream
from streamlink.stream.http import HTTPStream
log = logging.getLogger(__name__)
@pluginmatcher(re.compile(
r"""
https?://
(?:
(?:tvpstream\.vod|stream)\.tvp\.pl(?:/(?:\?channel_id=(?P<channel_id>\d+))?)?$
|
vod\.tvp\.pl/[^/]+/.+,(?P<vod_id>\d+)$
)
""",
re.VERBOSE,
))
class TVP(Plugin):
_URL_PLAYER = "https://stream.tvp.pl/sess/TVPlayer2/embed.php"
_URL_VOD = "https://vod.tvp.pl/api/products/{vod_id}/videos/playlist"
def _get_video_id(self, channel_id):
# type: (Optional[str])
items = self.session.http.get(
# type: (List[Tuple[int, int]])
self.url,
headers={
# required, otherwise the next request for retrieving the HLS URL will be aborted by the server
"Connection": "close",
},
schema=validate.Schema(
re.compile(r"window\.__channels\s*=\s*(?P<json>\[.+?])\s*;", re.DOTALL),
validate.none_or_all(
validate.get("json"),
validate.parse_json(),
[
validate.all(
{
"id": int,
"items": validate.none_or_all(
[{
"video_id": int,
}],
validate.get((0, "video_id")),
),
},
validate.union_get("id", "items"),
),
],
),
),
)
if channel_id is not None:
_channel_id = int(channel_id)
try:
return next(item[1] for item in items if item[0] == _channel_id)
except StopIteration:
pass
return items[0][1] if items else None
def _get_live(self, channel_id):
# type: (Optional[str])
video_id = self._get_video_id(channel_id)
if not video_id:
log.error("Could not find video ID")
return
log.debug("video ID: {0}".format(video_id))
streams = self.session.http.get(
# type: (Optional[List[Tuple[str, str]]])
self._URL_PLAYER,
params={
"ID": video_id,
"autoPlay": "without_audio",
},
headers={
"Referer": self.url,
},
schema=validate.Schema(
re.compile(r"window\.__api__\s*=\s*(?P<json>\{.+?})\s*;", re.DOTALL),
validate.get("json"),
validate.parse_json(),
{
"result": validate.none_or_all(
{
"content": {
"files": [
validate.all(
{
"type": validate.text,
"url": validate.url(),
},
validate.union_get("type", "url"),
),
],
},
},
validate.get(("content", "files")),
),
},
validate.get("result"),
),
)
if not streams:
return
def get(items, condition):
return next((_url for _stype, _url in items if condition(_stype, urlparse(_url).path)), None)
# prioritize HLSStream and get the first available stream
url = get(streams, lambda t, _: t == "hls")
if url:
return HLSStream.parse_variant_playlist(self.session, url)
# fall back to DASHStream
url = get(streams, lambda t, p: t == "any_native" and p.endswith(".mpd"))
if url:
return DASHStream.parse_manifest(self.session, url)
# fall back to HTTPStream
url = get(streams, lambda t, p: t == "any_native" and p.endswith(".mp4"))
if url:
return {"vod": HTTPStream(self.session, url)}
def _get_vod(self, vod_id):
log.info(self._URL_VOD.format(vod_id=vod_id))
data = self.session.http.get(
self._URL_VOD.format(vod_id=vod_id),
params={
"platform": "BROWSER",
"videoType": "MOVIE",
},
acceptable_status=(200, 403),
schema=validate.Schema(
validate.parse_json(),
validate.any(
{"code": "GEOIP_FILTER_FAILED"},
validate.all(
{
"sources": {
validate.optional("HLS"): [{
"src": validate.url(),
}],
},
},
validate.get("sources"),
),
),
),
)
if data.get("code") == "GEOIP_FILTER_FAILED":
log.error("The content is not available in your region")
return
if data.get("HLS"):
return HLSStream.parse_variant_playlist(self.session, data["HLS"][0]["src"])
def _get_streams(self):
if self.match.group("vod_id"):
return self._get_vod(self.match.group("vod_id"))
else:
return self._get_live(self.match.group("channel_id"))
__plugin__ = TVP