]> jfr.im git - yt-dlp.git/blame - yt_dlp/extractor/hrti.py
Fix bugs in `PlaylistEntries`
[yt-dlp.git] / yt_dlp / extractor / hrti.py
CommitLineData
6b03e1e2
AT
1import json
2
3from .common import InfoExtractor
e3755a62 4from ..compat import compat_HTTPError
6b03e1e2 5from ..utils import (
e3755a62
S
6 clean_html,
7 ExtractorError,
8 int_or_none,
9 parse_age_limit,
6b03e1e2 10 sanitized_Request,
e3755a62 11 try_get,
6b03e1e2
AT
12)
13
14
e3755a62
S
15class HRTiBaseIE(InfoExtractor):
16 """
17 Base Information Extractor for Croatian Radiotelevision
18 video on demand site https://hrti.hrt.hr
19 Reverse engineered from the JavaScript app in app.min.js
20 """
6b03e1e2
AT
21 _NETRC_MACHINE = 'hrti'
22
e3755a62
S
23 _APP_LANGUAGE = 'hr'
24 _APP_VERSION = '1.1'
25 _APP_PUBLICATION_ID = 'all_in_one'
26 _API_URL = 'http://clientapi.hrt.hr/client_api.php/config/identify/format/json'
52efa4b3 27 _token = None
6b03e1e2 28
52efa4b3 29 def _initialize_pre_login(self):
e3755a62
S
30 init_data = {
31 'application_publication_id': self._APP_PUBLICATION_ID
32 }
33
34 uuid = self._download_json(
35 self._API_URL, None, note='Downloading uuid',
36 errnote='Unable to download uuid',
37 data=json.dumps(init_data).encode('utf-8'))['uuid']
38
39 app_data = {
40 'uuid': uuid,
41 'application_publication_id': self._APP_PUBLICATION_ID,
42 'application_version': self._APP_VERSION
43 }
44
45 req = sanitized_Request(self._API_URL, data=json.dumps(app_data).encode('utf-8'))
6b03e1e2
AT
46 req.get_method = lambda: 'PUT'
47
48 resources = self._download_json(
e3755a62
S
49 req, None, note='Downloading session information',
50 errnote='Unable to download session information')
51
52 self._session_id = resources['session_id']
6b03e1e2 53
6b03e1e2
AT
54 modules = resources['modules']
55
e3755a62
S
56 self._search_url = modules['vod_catalog']['resources']['search']['uri'].format(
57 language=self._APP_LANGUAGE,
58 application_id=self._APP_PUBLICATION_ID)
6b03e1e2 59
3089bc74
S
60 self._login_url = (modules['user']['resources']['login']['uri']
61 + '/format/json').format(session_id=self._session_id)
6b03e1e2 62
e3755a62 63 self._logout_url = modules['user']['resources']['logout']['uri']
6b03e1e2 64
52efa4b3 65 def _perform_login(self, username, password):
e3755a62 66 auth_data = {
6b03e1e2
AT
67 'username': username,
68 'password': password,
e3755a62
S
69 }
70
6b03e1e2
AT
71 try:
72 auth_info = self._download_json(
e3755a62
S
73 self._login_url, None, note='Logging in', errnote='Unable to log in',
74 data=json.dumps(auth_data).encode('utf-8'))
75 except ExtractorError as e:
76 if isinstance(e.cause, compat_HTTPError) and e.cause.code == 406:
77 auth_info = self._parse_json(e.cause.read().encode('utf-8'), None)
78 else:
79 raise
80
81 error_message = auth_info.get('error', {}).get('message')
82 if error_message:
83 raise ExtractorError(
84 '%s said: %s' % (self.IE_NAME, error_message),
85 expected=True)
86
87 self._token = auth_info['secure_streaming_token']
6b03e1e2
AT
88
89 def _real_initialize(self):
52efa4b3 90 if not self._token:
91 # TODO: figure out authentication with cookies
92 self.raise_login_required(method='password')
6b03e1e2 93
e3755a62
S
94
95class HRTiIE(HRTiBaseIE):
96 _VALID_URL = r'''(?x)
97 (?:
98 hrti:(?P<short_id>[0-9]+)|
99 https?://
b0dde668 100 hrti\.hrt\.hr/(?:\#/)?video/show/(?P<id>[0-9]+)/(?P<display_id>[^/]+)?
e3755a62
S
101 )
102 '''
103 _TESTS = [{
104 'url': 'https://hrti.hrt.hr/#/video/show/2181385/republika-dokumentarna-serija-16-hd',
105 'info_dict': {
106 'id': '2181385',
107 'display_id': 'republika-dokumentarna-serija-16-hd',
108 'ext': 'mp4',
109 'title': 'REPUBLIKA, dokumentarna serija (1/6) (HD)',
110 'description': 'md5:48af85f620e8e0e1df4096270568544f',
111 'duration': 2922,
112 'view_count': int,
113 'average_rating': int,
114 'episode_number': int,
115 'season_number': int,
116 'age_limit': 12,
117 },
118 'skip': 'Requires account credentials',
119 }, {
120 'url': 'https://hrti.hrt.hr/#/video/show/2181385/',
121 'only_matching': True,
122 }, {
123 'url': 'hrti:2181385',
124 'only_matching': True,
b0dde668
AT
125 }, {
126 'url': 'https://hrti.hrt.hr/video/show/3873068/cuvar-dvorca-dramska-serija-14',
127 'only_matching': True,
e3755a62 128 }]
6b03e1e2
AT
129
130 def _real_extract(self, url):
5ad28e7f 131 mobj = self._match_valid_url(url)
e3755a62
S
132 video_id = mobj.group('short_id') or mobj.group('id')
133 display_id = mobj.group('display_id') or video_id
6b03e1e2 134
e3755a62
S
135 video = self._download_json(
136 '%s/video_id/%s/format/json' % (self._search_url, video_id),
137 display_id, 'Downloading video metadata JSON')['video'][0]
6b03e1e2 138
e3755a62
S
139 title_info = video['title']
140 title = title_info['title_long']
6b03e1e2
AT
141
142 movie = video['video_assets']['movie'][0]
e3755a62
S
143 m3u8_url = movie['url'].format(TOKEN=self._token)
144 formats = self._extract_m3u8_formats(
145 m3u8_url, display_id, 'mp4', entry_protocol='m3u8_native',
146 m3u8_id='hls')
6b03e1e2
AT
147 self._sort_formats(formats)
148
e3755a62
S
149 description = clean_html(title_info.get('summary_long'))
150 age_limit = parse_age_limit(video.get('parental_control', {}).get('rating'))
151 view_count = int_or_none(video.get('views'))
152 average_rating = int_or_none(video.get('user_rating'))
153 duration = int_or_none(movie.get('duration'))
6b03e1e2
AT
154
155 return {
156 'id': video_id,
e3755a62 157 'display_id': display_id,
6b03e1e2
AT
158 'title': title,
159 'description': description,
e3755a62
S
160 'duration': duration,
161 'view_count': view_count,
162 'average_rating': average_rating,
163 'age_limit': age_limit,
6b03e1e2
AT
164 'formats': formats,
165 }
e3755a62
S
166
167
168class HRTiPlaylistIE(HRTiBaseIE):
ae5af890 169 _VALID_URL = r'https?://hrti\.hrt\.hr/(?:#/)?video/list/category/(?P<id>[0-9]+)/(?P<display_id>[^/]+)?'
e3755a62
S
170 _TESTS = [{
171 'url': 'https://hrti.hrt.hr/#/video/list/category/212/ekumena',
172 'info_dict': {
173 'id': '212',
174 'title': 'ekumena',
175 },
176 'playlist_mincount': 8,
177 'skip': 'Requires account credentials',
178 }, {
179 'url': 'https://hrti.hrt.hr/#/video/list/category/212/',
180 'only_matching': True,
ae5af890
S
181 }, {
182 'url': 'https://hrti.hrt.hr/video/list/category/212/ekumena',
183 'only_matching': True,
e3755a62
S
184 }]
185
186 def _real_extract(self, url):
5ad28e7f 187 mobj = self._match_valid_url(url)
e3755a62
S
188 category_id = mobj.group('id')
189 display_id = mobj.group('display_id') or category_id
190
191 response = self._download_json(
192 '%s/category_id/%s/format/json' % (self._search_url, category_id),
193 display_id, 'Downloading video metadata JSON')
194
195 video_ids = try_get(
196 response, lambda x: x['video_listings'][0]['alternatives'][0]['list'],
197 list) or [video['id'] for video in response.get('videos', []) if video.get('id')]
198
4cb13d0d 199 entries = [self.url_result('hrti:%s' % video_id) for video_id in video_ids]
e3755a62
S
200
201 return self.playlist_result(entries, category_id, display_id)