[curiositystream] fix extraction(closes #12638)
[youtube-dl] / youtube_dl / extractor / curiositystream.py
1 # coding: utf-8
2 from __future__ import unicode_literals
3
4 import re
5
6 from .common import InfoExtractor
7 from ..utils import (
8     int_or_none,
9     urlencode_postdata,
10     compat_str,
11     ExtractorError,
12 )
13
14
15 class CuriosityStreamBaseIE(InfoExtractor):
16     _NETRC_MACHINE = 'curiositystream'
17     _auth_token = None
18     _API_BASE_URL = 'https://api.curiositystream.com/v1/'
19
20     def _handle_errors(self, result):
21         error = result.get('error', {}).get('message')
22         if error:
23             if isinstance(error, dict):
24                 error = ', '.join(error.values())
25             raise ExtractorError(
26                 '%s said: %s' % (self.IE_NAME, error), expected=True)
27
28     def _call_api(self, path, video_id):
29         headers = {}
30         if self._auth_token:
31             headers['X-Auth-Token'] = self._auth_token
32         result = self._download_json(
33             self._API_BASE_URL + path, video_id, headers=headers)
34         self._handle_errors(result)
35         return result['data']
36
37     def _real_initialize(self):
38         (email, password) = self._get_login_info()
39         if email is None:
40             return
41         result = self._download_json(
42             self._API_BASE_URL + 'login', None, data=urlencode_postdata({
43                 'email': email,
44                 'password': password,
45             }))
46         self._handle_errors(result)
47         self._auth_token = result['message']['auth_token']
48
49     def _extract_media_info(self, media):
50         video_id = compat_str(media['id'])
51         limelight_media_id = media['limelight_media_id']
52         title = media['title']
53
54         formats = []
55         for encoding in media.get('encodings', []):
56             m3u8_url = encoding.get('master_playlist_url')
57             if m3u8_url:
58                 formats.extend(self._extract_m3u8_formats(
59                     m3u8_url, video_id, 'mp4', 'm3u8_native',
60                     m3u8_id='hls', fatal=False))
61             encoding_url = encoding.get('url')
62             file_url = encoding.get('file_url')
63             if not encoding_url and not file_url:
64                 continue
65             f = {
66                 'width': int_or_none(encoding.get('width')),
67                 'height': int_or_none(encoding.get('height')),
68                 'vbr': int_or_none(encoding.get('video_bitrate')),
69                 'abr': int_or_none(encoding.get('audio_bitrate')),
70                 'filesize': int_or_none(encoding.get('size_in_bytes')),
71                 'vcodec': encoding.get('video_codec'),
72                 'acodec': encoding.get('audio_codec'),
73                 'container': encoding.get('container_type'),
74             }
75             for f_url in (encoding_url, file_url):
76                 if not f_url:
77                     continue
78                 fmt = f.copy()
79                 rtmp = re.search(r'^(?P<url>rtmpe?://(?P<host>[^/]+)/(?P<app>.+))/(?P<playpath>mp[34]:.+)$', f_url)
80                 if rtmp:
81                     fmt.update({
82                         'url': rtmp.group('url'),
83                         'play_path': rtmp.group('playpath'),
84                         'app': rtmp.group('app'),
85                         'ext': 'flv',
86                         'format_id': 'rtmp',
87                     })
88                 else:
89                     fmt.update({
90                         'url': f_url,
91                         'format_id': 'http',
92                     })
93                 formats.append(fmt)
94         self._sort_formats(formats)
95
96         subtitles = {}
97         for closed_caption in media.get('closed_captions', []):
98             sub_url = closed_caption.get('file')
99             if not sub_url:
100                 continue
101             lang = closed_caption.get('code') or closed_caption.get('language') or 'en'
102             subtitles.setdefault(lang, []).append({
103                 'url': sub_url,
104             })
105
106         return {
107             'id': video_id,
108             'formats': formats,
109             'title': title,
110             'description': media.get('description'),
111             'thumbnail': media.get('image_large') or media.get('image_medium') or media.get('image_small'),
112             'duration': int_or_none(media.get('duration')),
113             'tags': media.get('tags'),
114             'subtitles': subtitles,
115         }
116
117
118 class CuriosityStreamIE(CuriosityStreamBaseIE):
119     IE_NAME = 'curiositystream'
120     _VALID_URL = r'https?://app\.curiositystream\.com/video/(?P<id>\d+)'
121     _TEST = {
122         'url': 'https://app.curiositystream.com/video/2',
123         'md5': '262bb2f257ff301115f1973540de8983',
124         'info_dict': {
125             'id': '2',
126             'ext': 'mp4',
127             'title': 'How Did You Develop The Internet?',
128             'description': 'Vint Cerf, Google\'s Chief Internet Evangelist, describes how he and Bob Kahn created the internet.',
129         }
130     }
131
132     def _real_extract(self, url):
133         video_id = self._match_id(url)
134         media = self._call_api('media/' + video_id, video_id)
135         return self._extract_media_info(media)
136
137
138 class CuriosityStreamCollectionIE(CuriosityStreamBaseIE):
139     IE_NAME = 'curiositystream:collection'
140     _VALID_URL = r'https?://app\.curiositystream\.com/collection/(?P<id>\d+)'
141     _TEST = {
142         'url': 'https://app.curiositystream.com/collection/2',
143         'info_dict': {
144             'id': '2',
145             'title': 'Curious Minds: The Internet',
146             'description': 'How is the internet shaping our lives in the 21st Century?',
147         },
148         'playlist_mincount': 12,
149     }
150
151     def _real_extract(self, url):
152         collection_id = self._match_id(url)
153         collection = self._call_api(
154             'collections/' + collection_id, collection_id)
155         entries = []
156         for media in collection.get('media', []):
157             entries.append(self._extract_media_info(media))
158         return self.playlist_result(
159             entries, collection_id,
160             collection.get('title'), collection.get('description'))