-
-
Notifications
You must be signed in to change notification settings - Fork 1.2k
Expand file tree
/
Copy pathn13tv.py
More file actions
154 lines (127 loc) · 4.66 KB
/
Copy pathn13tv.py
File metadata and controls
154 lines (127 loc) · 4.66 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
"""
$description Israeli live TV channel and video on-demand service owned by Network 13.
$url 13tv.co.il
$type live, vod
$region Israel
"""
import re
from urllib.parse import urljoin, urlunparse
from streamlink.logger import getLogger
from streamlink.plugin import Plugin, PluginError, pluginmatcher
from streamlink.plugin.api import validate
from streamlink.stream.hls import HLSStream
log = getLogger(__name__)
@pluginmatcher(
re.compile(r"https?://(?:www\.)?13tv\.co\.il/(live|.*?/)"),
)
class N13TV(Plugin):
api_url = "https://13tv-api.oplayer.io/api/getlink/"
main_js_url_re = re.compile(r'type="text/javascript" src="(.*?main\..+\.js)"')
user_id_re = re.compile(r'"data-ccid":"(.*?)"')
video_name_re = re.compile(r'"videoRef":"(.*?)"')
server_addr_re = re.compile(r"(.*[^/])(/.*)")
media_file_re = re.compile(r"(.*)(\.[^\.].*)")
live_schema = validate.Schema(
[{"Link": validate.url()}],
validate.get(0),
validate.get("Link"),
)
vod_schema = validate.Schema(
[
{
"ShowTitle": str,
"ProtocolType": validate.all(
str,
validate.transform(lambda x: x.replace("://", "")),
),
"ServerAddress": str,
"MediaRoot": str,
"MediaFile": str,
"Bitrates": str,
"StreamingType": str,
"Token": validate.all(
str,
validate.transform(lambda x: x.lstrip("?")),
),
},
],
validate.get(0),
)
def _get_live(self, user_id):
res = self.session.http.get(
self.api_url,
params=dict(
userId=user_id,
serverType="web",
ch=1,
cdnName="casttime",
),
)
url = self.session.http.json(res, schema=self.live_schema)
log.debug(f"URL={url}")
return HLSStream.parse_variant_playlist(self.session, url)
def _get_vod(self, user_id, video_name):
res = self.session.http.get(
urljoin(self.api_url, "getVideoByFileName"),
params=dict(
userId=user_id,
videoName=video_name,
serverType="web",
callback="x",
),
)
vod_data = self.session.http.json(res, schema=self.vod_schema)
if video_name == vod_data["ShowTitle"]:
try:
host, base_path = self.server_addr_re.search(vod_data["ServerAddress"]).groups() # type: ignore
except AttributeError:
raise PluginError("Could not split 'ServerAddress' components") from None
try:
base_file, file_ext = self.media_file_re.search(vod_data["MediaFile"]).groups() # type: ignore
except AttributeError:
raise PluginError("Could not split 'MediaFile' components") from None
media_path = "".join([
base_path,
vod_data["MediaRoot"],
base_file,
vod_data["Bitrates"],
file_ext,
vod_data["StreamingType"],
])
log.debug("Media path=%s", media_path)
vod_url = urlunparse((
vod_data["ProtocolType"],
host,
media_path,
"",
vod_data["Token"],
"",
))
log.debug("URL=%s", vod_url)
return HLSStream.parse_variant_playlist(self.session, vod_url)
def _get_streams(self):
url_type = self.match.group(1)
log.debug(f"URL type={url_type}")
res = self.session.http.get(self.url)
if url_type != "live":
m = self.video_name_re.search(res.text)
video_name = m and m.group(1)
if not video_name:
raise PluginError("Could not determine video_name")
log.debug(f"Video name={video_name}")
m = self.main_js_url_re.search(res.text)
main_js_path = m and m.group(1)
if not main_js_path:
raise PluginError("Could not determine main_js_path")
log.debug(f"Main JS path={main_js_path}")
res = self.session.http.get(urljoin(self.url, main_js_path))
m = self.user_id_re.search(res.text)
user_id = m and m.group(1)
if not user_id:
raise PluginError("Could not determine user_id")
log.debug(f"User ID={user_id}")
if url_type == "live":
return self._get_live(user_id)
else:
return self._get_vod(user_id, video_name)
__plugin__ = N13TV