]> Raphaƫl G. Git Repositories - youtubedl/blob - youtube_dl/extractor/crackle.py
debian/{compat,control}: Update to debhelper compatibility level 12.
[youtubedl] / youtube_dl / extractor / crackle.py
1 # coding: utf-8
2 from __future__ import unicode_literals, division
3
4 import re
5
6 from .common import InfoExtractor
7 from ..compat import compat_HTTPError
8 from ..utils import (
9 determine_ext,
10 float_or_none,
11 int_or_none,
12 parse_age_limit,
13 parse_duration,
14 url_or_none,
15 ExtractorError
16 )
17
18
19 class CrackleIE(InfoExtractor):
20 _VALID_URL = r'(?:crackle:|https?://(?:(?:www|m)\.)?(?:sony)?crackle\.com/(?:playlist/\d+/|(?:[^/]+/)+))(?P<id>\d+)'
21 _TESTS = [{
22 # geo restricted to CA
23 'url': 'https://www.crackle.com/andromeda/2502343',
24 'info_dict': {
25 'id': '2502343',
26 'ext': 'mp4',
27 'title': 'Under The Night',
28 'description': 'md5:d2b8ca816579ae8a7bf28bfff8cefc8a',
29 'duration': 2583,
30 'view_count': int,
31 'average_rating': 0,
32 'age_limit': 14,
33 'genre': 'Action, Sci-Fi',
34 'creator': 'Allan Kroeker',
35 'artist': 'Keith Hamilton Cobb, Kevin Sorbo, Lisa Ryder, Lexa Doig, Robert Hewitt Wolfe',
36 'release_year': 2000,
37 'series': 'Andromeda',
38 'episode': 'Under The Night',
39 'season_number': 1,
40 'episode_number': 1,
41 },
42 'params': {
43 # m3u8 download
44 'skip_download': True,
45 }
46 }, {
47 'url': 'https://www.sonycrackle.com/andromeda/2502343',
48 'only_matching': True,
49 }]
50
51 _MEDIA_FILE_SLOTS = {
52 '360p.mp4': {
53 'width': 640,
54 'height': 360,
55 },
56 '480p.mp4': {
57 'width': 768,
58 'height': 432,
59 },
60 '480p_1mbps.mp4': {
61 'width': 852,
62 'height': 480,
63 },
64 }
65
66 def _real_extract(self, url):
67 video_id = self._match_id(url)
68
69 country_code = self._downloader.params.get('geo_bypass_country', None)
70 countries = [country_code] if country_code else (
71 'US', 'AU', 'CA', 'AS', 'FM', 'GU', 'MP', 'PR', 'PW', 'MH', 'VI')
72
73 last_e = None
74
75 for country in countries:
76 try:
77 media = self._download_json(
78 'https://web-api-us.crackle.com/Service.svc/details/media/%s/%s'
79 % (video_id, country), video_id,
80 'Downloading media JSON as %s' % country,
81 'Unable to download media JSON', query={
82 'disableProtocols': 'true',
83 'format': 'json'
84 })
85 except ExtractorError as e:
86 # 401 means geo restriction, trying next country
87 if isinstance(e.cause, compat_HTTPError) and e.cause.code == 401:
88 last_e = e
89 continue
90 raise
91
92 media_urls = media.get('MediaURLs')
93 if not media_urls or not isinstance(media_urls, list):
94 continue
95
96 title = media['Title']
97
98 formats = []
99 for e in media['MediaURLs']:
100 if e.get('UseDRM') is True:
101 continue
102 format_url = url_or_none(e.get('Path'))
103 if not format_url:
104 continue
105 ext = determine_ext(format_url)
106 if ext == 'm3u8':
107 formats.extend(self._extract_m3u8_formats(
108 format_url, video_id, 'mp4', entry_protocol='m3u8_native',
109 m3u8_id='hls', fatal=False))
110 elif ext == 'mpd':
111 formats.extend(self._extract_mpd_formats(
112 format_url, video_id, mpd_id='dash', fatal=False))
113 elif format_url.endswith('.ism/Manifest'):
114 formats.extend(self._extract_ism_formats(
115 format_url, video_id, ism_id='mss', fatal=False))
116 else:
117 mfs_path = e.get('Type')
118 mfs_info = self._MEDIA_FILE_SLOTS.get(mfs_path)
119 if not mfs_info:
120 continue
121 formats.append({
122 'url': format_url,
123 'format_id': 'http-' + mfs_path.split('.')[0],
124 'width': mfs_info['width'],
125 'height': mfs_info['height'],
126 })
127 self._sort_formats(formats)
128
129 description = media.get('Description')
130 duration = int_or_none(media.get(
131 'DurationInSeconds')) or parse_duration(media.get('Duration'))
132 view_count = int_or_none(media.get('CountViews'))
133 average_rating = float_or_none(media.get('UserRating'))
134 age_limit = parse_age_limit(media.get('Rating'))
135 genre = media.get('Genre')
136 release_year = int_or_none(media.get('ReleaseYear'))
137 creator = media.get('Directors')
138 artist = media.get('Cast')
139
140 if media.get('MediaTypeDisplayValue') == 'Full Episode':
141 series = media.get('ShowName')
142 episode = title
143 season_number = int_or_none(media.get('Season'))
144 episode_number = int_or_none(media.get('Episode'))
145 else:
146 series = episode = season_number = episode_number = None
147
148 subtitles = {}
149 cc_files = media.get('ClosedCaptionFiles')
150 if isinstance(cc_files, list):
151 for cc_file in cc_files:
152 if not isinstance(cc_file, dict):
153 continue
154 cc_url = url_or_none(cc_file.get('Path'))
155 if not cc_url:
156 continue
157 lang = cc_file.get('Locale') or 'en'
158 subtitles.setdefault(lang, []).append({'url': cc_url})
159
160 thumbnails = []
161 images = media.get('Images')
162 if isinstance(images, list):
163 for image_key, image_url in images.items():
164 mobj = re.search(r'Img_(\d+)[xX](\d+)', image_key)
165 if not mobj:
166 continue
167 thumbnails.append({
168 'url': image_url,
169 'width': int(mobj.group(1)),
170 'height': int(mobj.group(2)),
171 })
172
173 return {
174 'id': video_id,
175 'title': title,
176 'description': description,
177 'duration': duration,
178 'view_count': view_count,
179 'average_rating': average_rating,
180 'age_limit': age_limit,
181 'genre': genre,
182 'creator': creator,
183 'artist': artist,
184 'release_year': release_year,
185 'series': series,
186 'episode': episode,
187 'season_number': season_number,
188 'episode_number': episode_number,
189 'thumbnails': thumbnails,
190 'subtitles': subtitles,
191 'formats': formats,
192 }
193
194 raise last_e