]> jfr.im git - yt-dlp.git/blame - yt_dlp/extractor/hidive.py
[extractor] Deprecate `_sort_formats`
[yt-dlp.git] / yt_dlp / extractor / hidive.py
CommitLineData
f2cad2e4 1import re
62f49dd3 2
62f49dd3 3from .common import InfoExtractor
62f49dd3
S
4from ..utils import (
5 ExtractorError,
6 int_or_none,
a8cb7eca 7 try_get,
3052a30d 8 url_or_none,
62f49dd3
S
9 urlencode_postdata,
10)
11
12
13class HiDiveIE(InfoExtractor):
705e7c20 14 _VALID_URL = r'https?://(?:www\.)?hidive\.com/stream/(?P<id>(?P<title>[^/]+)/(?P<key>[^/?#&]+))'
62f49dd3
S
15 # Using X-Forwarded-For results in 403 HTTP error for HLS fragments,
16 # so disabling geo bypass completely
17 _GEO_BYPASS = False
e8e58c22 18 _NETRC_MACHINE = 'hidive'
e8e58c22 19 _LOGIN_URL = 'https://www.hidive.com/account/login'
62f49dd3
S
20
21 _TESTS = [{
22 'url': 'https://www.hidive.com/stream/the-comic-artist-and-his-assistants/s01e001',
23 'info_dict': {
24 'id': 'the-comic-artist-and-his-assistants/s01e001',
25 'ext': 'mp4',
26 'title': 'the-comic-artist-and-his-assistants/s01e001',
27 'series': 'the-comic-artist-and-his-assistants',
28 'season_number': 1,
29 'episode_number': 1,
30 },
31 'params': {
32 'skip_download': True,
62f49dd3 33 },
e8e58c22 34 'skip': 'Requires Authentication',
62f49dd3
S
35 }]
36
52efa4b3 37 def _perform_login(self, username, password):
e8e58c22
RA
38 webpage = self._download_webpage(self._LOGIN_URL, None)
39 form = self._search_regex(
40 r'(?s)<form[^>]+action="/account/login"[^>]*>(.+?)</form>',
2c646fe4 41 webpage, 'login form', default=None)
42 if not form: # logged in
43 return
e8e58c22
RA
44 data = self._hidden_inputs(form)
45 data.update({
52efa4b3 46 'Email': username,
e8e58c22
RA
47 'Password': password,
48 })
49 self._download_webpage(
50 self._LOGIN_URL, None, 'Logging in', data=urlencode_postdata(data))
e8e58c22 51
f2cad2e4 52 def _call_api(self, video_id, title, key, data={}, **kwargs):
53 data = {
54 **data,
55 'Title': title,
56 'Key': key,
57 'PlayerId': 'f4f895ce1ca713ba263b91caeb1daa2d08904783',
58 }
59 return self._download_json(
60 'https://www.hidive.com/play/settings', video_id,
61 data=urlencode_postdata(data), **kwargs) or {}
62
63 def _extract_subtitles_from_rendition(self, rendition, subtitles, parsed_urls):
64 for cc_file in rendition.get('ccFiles', []):
65 cc_url = url_or_none(try_get(cc_file, lambda x: x[2]))
66 # name is used since we cant distinguish subs with same language code
67 cc_lang = try_get(cc_file, (lambda x: x[1].replace(' ', '-').lower(), lambda x: x[0]), str)
68 if cc_url not in parsed_urls and cc_lang:
69 parsed_urls.add(cc_url)
70 subtitles.setdefault(cc_lang, []).append({'url': cc_url})
71
e8f726a5 72 def _get_subtitles(self, url, video_id, title, key, parsed_urls):
f2cad2e4 73 webpage = self._download_webpage(url, video_id, fatal=False) or ''
e8f726a5 74 subtitles = {}
f2cad2e4 75 for caption in set(re.findall(r'data-captions=\"([^\"]+)\"', webpage)):
76 renditions = self._call_api(
77 video_id, title, key, {'Captions': caption}, fatal=False,
78 note=f'Downloading {caption} subtitle information').get('renditions') or {}
79 for rendition_id, rendition in renditions.items():
80 self._extract_subtitles_from_rendition(rendition, subtitles, parsed_urls)
81 return subtitles
82
62f49dd3 83 def _real_extract(self, url):
705e7c20 84 video_id, title, key = self._match_valid_url(url).group('id', 'title', 'key')
f2cad2e4 85 settings = self._call_api(video_id, title, key)
62f49dd3 86
705e7c20 87 restriction = settings.get('restrictionReason')
88 if restriction == 'RegionRestricted':
89 self.raise_geo_restricted()
90 if restriction and restriction != 'None':
91 raise ExtractorError(
92 '%s said: %s' % (self.IE_NAME, restriction), expected=True)
62f49dd3 93
e8f726a5 94 formats, parsed_urls = [], {None}
705e7c20 95 for rendition_id, rendition in settings['renditions'].items():
96 audio, version, extra = rendition_id.split('_')
97 m3u8_url = url_or_none(try_get(rendition, lambda x: x['bitrates']['hls']))
f2cad2e4 98 if m3u8_url not in parsed_urls:
99 parsed_urls.add(m3u8_url)
a8cb7eca 100 frmt = self._extract_m3u8_formats(
705e7c20 101 m3u8_url, video_id, 'mp4', entry_protocol='m3u8_native', m3u8_id=rendition_id, fatal=False)
a8cb7eca
AG
102 for f in frmt:
103 f['language'] = audio
705e7c20 104 f['format_note'] = f'{version}, {extra}'
a8cb7eca 105 formats.extend(frmt)
62f49dd3 106
62f49dd3
S
107 return {
108 'id': video_id,
109 'title': video_id,
b5ae35ee 110 'subtitles': self.extract_subtitles(url, video_id, title, key, parsed_urls),
62f49dd3
S
111 'formats': formats,
112 'series': title,
705e7c20 113 'season_number': int_or_none(
114 self._search_regex(r's(\d+)', key, 'season number', default=None)),
115 'episode_number': int_or_none(
116 self._search_regex(r'e(\d+)', key, 'episode number', default=None)),
a8cb7eca 117 'http_headers': {'Referer': url}
62f49dd3 118 }