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