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