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