forked from streamlink/streamlink
/
n13tv.py
150 lines (124 loc) · 4.64 KB
/
n13tv.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
"""
$description Israeli live TV channel and video on-demand service owned by Network 13.
$url 13tv.co.il
$type live, vod
$region Israel
"""
import logging
import re
from streamlink.compat import urljoin, urlunparse
from streamlink.plugin import Plugin, PluginError, pluginmatcher
from streamlink.plugin.api import validate
from streamlink.stream.hls import HLSStream
log = logging.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(validate.all(
[{'Link': validate.url()}],
validate.get(0),
validate.get('Link')
))
vod_schema = validate.Schema(validate.all([{
'ShowTitle': validate.text,
'ProtocolType': validate.all(
validate.text,
validate.transform(lambda x: x.replace("://", ""))
),
'ServerAddress': validate.text,
'MediaRoot': validate.text,
'MediaFile': validate.text,
'Bitrates': validate.text,
'StreamingType': validate.text,
'Token': validate.all(
validate.text,
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("URL={0}".format(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']:
host, base_path = self.server_addr_re.search(
vod_data['ServerAddress']
).groups()
if not host or not base_path:
raise PluginError("Could not split 'ServerAddress' components")
base_file, file_ext = self.media_file_re.search(
vod_data['MediaFile']
).groups()
if not base_file or not file_ext:
raise PluginError("Could not split 'MediaFile' components")
media_path = "{0}{1}{2}{3}{4}{5}".format(
base_path,
vod_data['MediaRoot'],
base_file,
vod_data['Bitrates'],
file_ext,
vod_data['StreamingType']
)
log.debug("Media path={0}".format(media_path))
vod_url = urlunparse((
vod_data['ProtocolType'],
host,
media_path,
'',
vod_data['Token'],
''
))
log.debug("URL={0}".format(vod_url))
return HLSStream.parse_variant_playlist(self.session, vod_url)
def _get_streams(self):
url_type = self.match.group(1)
log.debug("URL type={0}".format(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("Video name={0}".format(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("Main JS path={0}".format(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("User ID={0}".format(user_id))
if url_type == "live":
return self._get_live(user_id)
else:
return self._get_vod(user_id, video_name)
__plugin__ = N13TV