]> Raphaƫl G. Git Repositories - youtubedl/blob - youtube_dl/extractor/crackle.py
debian/control: Mark compliance with Debian policy 4.1.5.
[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 def _real_extract(self, url):
52 video_id = self._match_id(url)
53
54 country_code = self._downloader.params.get('geo_bypass_country', None)
55 countries = [country_code] if country_code else (
56 'US', 'AU', 'CA', 'AS', 'FM', 'GU', 'MP', 'PR', 'PW', 'MH', 'VI')
57
58 last_e = None
59
60 for country in countries:
61 try:
62 media = self._download_json(
63 'https://web-api-us.crackle.com/Service.svc/details/media/%s/%s'
64 % (video_id, country), video_id,
65 'Downloading media JSON as %s' % country,
66 'Unable to download media JSON', query={
67 'disableProtocols': 'true',
68 'format': 'json'
69 })
70 except ExtractorError as e:
71 # 401 means geo restriction, trying next country
72 if isinstance(e.cause, compat_HTTPError) and e.cause.code == 401:
73 last_e = e
74 continue
75 raise
76
77 media_urls = media.get('MediaURLs')
78 if not media_urls or not isinstance(media_urls, list):
79 continue
80
81 title = media['Title']
82
83 formats = []
84 for e in media['MediaURLs']:
85 if e.get('UseDRM') is True:
86 continue
87 format_url = url_or_none(e.get('Path'))
88 if not format_url:
89 continue
90 ext = determine_ext(format_url)
91 if ext == 'm3u8':
92 formats.extend(self._extract_m3u8_formats(
93 format_url, video_id, 'mp4', entry_protocol='m3u8_native',
94 m3u8_id='hls', fatal=False))
95 elif ext == 'mpd':
96 formats.extend(self._extract_mpd_formats(
97 format_url, video_id, mpd_id='dash', fatal=False))
98 self._sort_formats(formats)
99
100 description = media.get('Description')
101 duration = int_or_none(media.get(
102 'DurationInSeconds')) or parse_duration(media.get('Duration'))
103 view_count = int_or_none(media.get('CountViews'))
104 average_rating = float_or_none(media.get('UserRating'))
105 age_limit = parse_age_limit(media.get('Rating'))
106 genre = media.get('Genre')
107 release_year = int_or_none(media.get('ReleaseYear'))
108 creator = media.get('Directors')
109 artist = media.get('Cast')
110
111 if media.get('MediaTypeDisplayValue') == 'Full Episode':
112 series = media.get('ShowName')
113 episode = title
114 season_number = int_or_none(media.get('Season'))
115 episode_number = int_or_none(media.get('Episode'))
116 else:
117 series = episode = season_number = episode_number = None
118
119 subtitles = {}
120 cc_files = media.get('ClosedCaptionFiles')
121 if isinstance(cc_files, list):
122 for cc_file in cc_files:
123 if not isinstance(cc_file, dict):
124 continue
125 cc_url = url_or_none(cc_file.get('Path'))
126 if not cc_url:
127 continue
128 lang = cc_file.get('Locale') or 'en'
129 subtitles.setdefault(lang, []).append({'url': cc_url})
130
131 thumbnails = []
132 images = media.get('Images')
133 if isinstance(images, list):
134 for image_key, image_url in images.items():
135 mobj = re.search(r'Img_(\d+)[xX](\d+)', image_key)
136 if not mobj:
137 continue
138 thumbnails.append({
139 'url': image_url,
140 'width': int(mobj.group(1)),
141 'height': int(mobj.group(2)),
142 })
143
144 return {
145 'id': video_id,
146 'title': title,
147 'description': description,
148 'duration': duration,
149 'view_count': view_count,
150 'average_rating': average_rating,
151 'age_limit': age_limit,
152 'genre': genre,
153 'creator': creator,
154 'artist': artist,
155 'release_year': release_year,
156 'series': series,
157 'episode': episode,
158 'season_number': season_number,
159 'episode_number': episode_number,
160 'thumbnails': thumbnails,
161 'subtitles': subtitles,
162 'formats': formats,
163 }
164
165 raise last_e