d8f2e682fbb50adf60763443adff8961ff53de9e
[youtube-dl] / youtube_dl / extractor / hidive.py
1 # coding: utf-8
2 from __future__ import unicode_literals
3
4 import re
5
6 from .common import InfoExtractor
7 from ..compat import compat_str
8 from ..utils import (
9     ExtractorError,
10     int_or_none,
11     urlencode_postdata,
12 )
13
14
15 class HiDiveIE(InfoExtractor):
16     _VALID_URL = r'https?://(?:www\.)?hidive\.com/stream/(?P<title>[^/]+)/(?P<key>[^/?#&]+)'
17     # Using X-Forwarded-For results in 403 HTTP error for HLS fragments,
18     # so disabling geo bypass completely
19     _GEO_BYPASS = False
20     _NETRC_MACHINE = 'hidive'
21     _LOGGED_IN = False
22     _LOGIN_URL = 'https://www.hidive.com/account/login'
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,
36         },
37         'skip': 'Requires Authentication',
38     }]
39
40     def _real_initialize(self):
41         if self._LOGGED_IN:
42             return
43
44         (email, password) = self._get_login_info()
45         if email is None:
46             return
47
48         webpage = self._download_webpage(self._LOGIN_URL, None)
49         form = self._search_regex(
50             r'(?s)<form[^>]+action="/account/login"[^>]*>(.+?)</form>',
51             webpage, 'login form')
52         data = self._hidden_inputs(form)
53         data.update({
54             'Email': email,
55             'Password': password,
56         })
57         self._download_webpage(
58             self._LOGIN_URL, None, 'Logging in', data=urlencode_postdata(data))
59         self._LOGGED_IN = True
60
61     def _real_extract(self, url):
62         mobj = re.match(self._VALID_URL, url)
63         title, key = mobj.group('title', 'key')
64         video_id = '%s/%s' % (title, key)
65
66         settings = self._download_json(
67             'https://www.hidive.com/play/settings', video_id,
68             data=urlencode_postdata({
69                 'Title': title,
70                 'Key': key,
71                 'PlayerId': 'f4f895ce1ca713ba263b91caeb1daa2d08904783',
72             }))
73
74         restriction = settings.get('restrictionReason')
75         if restriction == 'RegionRestricted':
76             self.raise_geo_restricted()
77
78         if restriction and restriction != 'None':
79             raise ExtractorError(
80                 '%s said: %s' % (self.IE_NAME, restriction), expected=True)
81
82         formats = []
83         subtitles = {}
84         for rendition_id, rendition in settings['renditions'].items():
85             bitrates = rendition.get('bitrates')
86             if not isinstance(bitrates, dict):
87                 continue
88             m3u8_url = bitrates.get('hls')
89             if not isinstance(m3u8_url, compat_str):
90                 continue
91             formats.extend(self._extract_m3u8_formats(
92                 m3u8_url, video_id, 'mp4', entry_protocol='m3u8_native',
93                 m3u8_id='%s-hls' % rendition_id, fatal=False))
94             cc_files = rendition.get('ccFiles')
95             if not isinstance(cc_files, list):
96                 continue
97             for cc_file in cc_files:
98                 if not isinstance(cc_file, list) or len(cc_file) < 3:
99                     continue
100                 cc_lang = cc_file[0]
101                 cc_url = cc_file[2]
102                 if not isinstance(cc_lang, compat_str) or not isinstance(
103                         cc_url, compat_str):
104                     continue
105                 subtitles.setdefault(cc_lang, []).append({
106                     'url': cc_url,
107                 })
108         self._sort_formats(formats)
109
110         season_number = int_or_none(self._search_regex(
111             r's(\d+)', key, 'season number', default=None))
112         episode_number = int_or_none(self._search_regex(
113             r'e(\d+)', key, 'episode number', default=None))
114
115         return {
116             'id': video_id,
117             'title': video_id,
118             'subtitles': subtitles,
119             'formats': formats,
120             'series': title,
121             'season_number': season_number,
122             'episode_number': episode_number,
123         }