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