remove unnecessary assignment parenthesis
[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     _LOGIN_URL = 'https://www.hidive.com/account/login'
22
23     _TESTS = [{
24         'url': 'https://www.hidive.com/stream/the-comic-artist-and-his-assistants/s01e001',
25         'info_dict': {
26             'id': 'the-comic-artist-and-his-assistants/s01e001',
27             'ext': 'mp4',
28             'title': 'the-comic-artist-and-his-assistants/s01e001',
29             'series': 'the-comic-artist-and-his-assistants',
30             'season_number': 1,
31             'episode_number': 1,
32         },
33         'params': {
34             'skip_download': True,
35         },
36         'skip': 'Requires Authentication',
37     }]
38
39     def _real_initialize(self):
40         email, password = self._get_login_info()
41         if email is None:
42             return
43
44         webpage = self._download_webpage(self._LOGIN_URL, None)
45         form = self._search_regex(
46             r'(?s)<form[^>]+action="/account/login"[^>]*>(.+?)</form>',
47             webpage, 'login form')
48         data = self._hidden_inputs(form)
49         data.update({
50             'Email': email,
51             'Password': password,
52         })
53         self._download_webpage(
54             self._LOGIN_URL, None, 'Logging in', data=urlencode_postdata(data))
55
56     def _real_extract(self, url):
57         mobj = re.match(self._VALID_URL, url)
58         title, key = mobj.group('title', 'key')
59         video_id = '%s/%s' % (title, key)
60
61         settings = self._download_json(
62             'https://www.hidive.com/play/settings', video_id,
63             data=urlencode_postdata({
64                 'Title': title,
65                 'Key': key,
66                 'PlayerId': 'f4f895ce1ca713ba263b91caeb1daa2d08904783',
67             }))
68
69         restriction = settings.get('restrictionReason')
70         if restriction == 'RegionRestricted':
71             self.raise_geo_restricted()
72
73         if restriction and restriction != 'None':
74             raise ExtractorError(
75                 '%s said: %s' % (self.IE_NAME, restriction), expected=True)
76
77         formats = []
78         subtitles = {}
79         for rendition_id, rendition in settings['renditions'].items():
80             bitrates = rendition.get('bitrates')
81             if not isinstance(bitrates, dict):
82                 continue
83             m3u8_url = bitrates.get('hls')
84             if not isinstance(m3u8_url, compat_str):
85                 continue
86             formats.extend(self._extract_m3u8_formats(
87                 m3u8_url, video_id, 'mp4', entry_protocol='m3u8_native',
88                 m3u8_id='%s-hls' % rendition_id, fatal=False))
89             cc_files = rendition.get('ccFiles')
90             if not isinstance(cc_files, list):
91                 continue
92             for cc_file in cc_files:
93                 if not isinstance(cc_file, list) or len(cc_file) < 3:
94                     continue
95                 cc_lang = cc_file[0]
96                 cc_url = cc_file[2]
97                 if not isinstance(cc_lang, compat_str) or not isinstance(
98                         cc_url, compat_str):
99                     continue
100                 subtitles.setdefault(cc_lang, []).append({
101                     'url': cc_url,
102                 })
103         self._sort_formats(formats)
104
105         season_number = int_or_none(self._search_regex(
106             r's(\d+)', key, 'season number', default=None))
107         episode_number = int_or_none(self._search_regex(
108             r'e(\d+)', key, 'episode number', default=None))
109
110         return {
111             'id': video_id,
112             'title': video_id,
113             'subtitles': subtitles,
114             'formats': formats,
115             'series': title,
116             'season_number': season_number,
117             'episode_number': episode_number,
118         }