]> jfr.im git - yt-dlp.git/blob - youtube_dlc/extractor/bandcamp.py
[skyitalia] added geoblock msg
[yt-dlp.git] / youtube_dlc / extractor / bandcamp.py
1 from __future__ import unicode_literals
2
3 import random
4 import re
5 import time
6
7 from .common import InfoExtractor
8 from ..compat import (
9 compat_str,
10 compat_urlparse,
11 )
12 from ..utils import (
13 ExtractorError,
14 float_or_none,
15 int_or_none,
16 KNOWN_EXTENSIONS,
17 parse_filesize,
18 str_or_none,
19 try_get,
20 unescapeHTML,
21 update_url_query,
22 unified_strdate,
23 unified_timestamp,
24 url_or_none,
25 )
26
27
28 class BandcampIE(InfoExtractor):
29 _VALID_URL = r'https?://[^/]+\.bandcamp\.com/track/(?P<title>[^/?#&]+)'
30 _TESTS = [{
31 'url': 'http://youtube-dlc.bandcamp.com/track/youtube-dlc-test-song',
32 'md5': 'c557841d5e50261777a6585648adf439',
33 'info_dict': {
34 'id': '1812978515',
35 'ext': 'mp3',
36 'title': "youtube-dl \"'/\\\u00e4\u21ad - youtube-dl \"'/\\\u00e4\u21ad - youtube-dl test song \"'/\\\u00e4\u21ad",
37 'duration': 9.8485,
38 'uploader': "youtube-dl \"'/\\\u00e4\u21ad",
39 'timestamp': 1354224127,
40 'upload_date': '20121129',
41 },
42 '_skip': 'There is a limit of 200 free downloads / month for the test song'
43 }, {
44 # free download
45 'url': 'http://benprunty.bandcamp.com/track/lanius-battle',
46 'md5': '5d92af55811e47f38962a54c30b07ef0',
47 'info_dict': {
48 'id': '2650410135',
49 'ext': 'aiff',
50 'title': 'Ben Prunty - Lanius (Battle)',
51 'thumbnail': r're:^https?://.*\.jpg$',
52 'uploader': 'Ben Prunty',
53 'timestamp': 1396508491,
54 'upload_date': '20140403',
55 'release_date': '20140403',
56 'duration': 260.877,
57 'track': 'Lanius (Battle)',
58 'track_number': 1,
59 'track_id': '2650410135',
60 'artist': 'Ben Prunty',
61 'album': 'FTL: Advanced Edition Soundtrack',
62 },
63 }, {
64 # no free download, mp3 128
65 'url': 'https://relapsealumni.bandcamp.com/track/hail-to-fire',
66 'md5': 'fec12ff55e804bb7f7ebeb77a800c8b7',
67 'info_dict': {
68 'id': '2584466013',
69 'ext': 'mp3',
70 'title': 'Mastodon - Hail to Fire',
71 'thumbnail': r're:^https?://.*\.jpg$',
72 'uploader': 'Mastodon',
73 'timestamp': 1322005399,
74 'upload_date': '20111122',
75 'release_date': '20040207',
76 'duration': 120.79,
77 'track': 'Hail to Fire',
78 'track_number': 5,
79 'track_id': '2584466013',
80 'artist': 'Mastodon',
81 'album': 'Call of the Mastodon',
82 },
83 }]
84
85 def _real_extract(self, url):
86 mobj = re.match(self._VALID_URL, url)
87 title = mobj.group('title')
88 webpage = self._download_webpage(url, title)
89 thumbnail = self._html_search_meta('og:image', webpage, default=None)
90
91 track_id = None
92 track = None
93 track_number = None
94 duration = None
95
96 formats = []
97 trackinfo_block = self._html_search_regex(
98 r'trackinfo(?:["\']|&quot;):\[\s*({.+?})\s*\],(?:["\']|&quot;)',
99 webpage, 'track info', default='{}')
100
101 track_info = self._parse_json(trackinfo_block, title)
102 if track_info:
103 file_ = track_info.get('file')
104 if isinstance(file_, dict):
105 for format_id, format_url in file_.items():
106 if not url_or_none(format_url):
107 continue
108 ext, abr_str = format_id.split('-', 1)
109 formats.append({
110 'format_id': format_id,
111 'url': self._proto_relative_url(format_url, 'http:'),
112 'ext': ext,
113 'vcodec': 'none',
114 'acodec': ext,
115 'abr': int_or_none(abr_str),
116 })
117
118 track_id = str_or_none(track_info.get('track_id') or track_info.get('id'))
119 track_number = int_or_none(track_info.get('track_num'))
120 duration = float_or_none(track_info.get('duration'))
121
122 def extract(key):
123 data = self._html_search_regex(
124 r',(["\']|&quot;)%s\1:\1(?P<value>(?:\\\1|((?!\1).))+)\1' % key,
125 webpage, key, default=None, group='value')
126 return data.replace(r'\"', '"').replace('\\\\', '\\') if data else data
127
128 track = extract('title')
129 artist = extract('artist')
130 album = extract('album_title')
131 timestamp = unified_timestamp(
132 extract('publish_date') or extract('album_publish_date'))
133 release_date = unified_strdate(extract('album_release_date'))
134
135 download_link = self._search_regex(
136 r'freeDownloadPage(?:["\']|&quot;):\s*(["\']|&quot;)(?P<url>(?:(?!\1).)+)\1', webpage,
137 'download link', default=None, group='url')
138 if download_link:
139 track_id = self._search_regex(
140 r'\?id=(?P<id>\d+)&',
141 download_link, 'track id')
142
143 download_webpage = self._download_webpage(
144 download_link, track_id, 'Downloading free downloads page')
145
146 blob = self._parse_json(
147 self._search_regex(
148 r'data-blob=(["\'])(?P<blob>{.+?})\1', download_webpage,
149 'blob', group='blob'),
150 track_id, transform_source=unescapeHTML)
151
152 info = try_get(
153 blob, (lambda x: x['digital_items'][0],
154 lambda x: x['download_items'][0]), dict)
155 if info:
156 downloads = info.get('downloads')
157 if isinstance(downloads, dict):
158 if not track:
159 track = info.get('title')
160 if not artist:
161 artist = info.get('artist')
162 if not thumbnail:
163 thumbnail = info.get('thumb_url')
164
165 download_formats = {}
166 download_formats_list = blob.get('download_formats')
167 if isinstance(download_formats_list, list):
168 for f in blob['download_formats']:
169 name, ext = f.get('name'), f.get('file_extension')
170 if all(isinstance(x, compat_str) for x in (name, ext)):
171 download_formats[name] = ext.strip('.')
172
173 for format_id, f in downloads.items():
174 format_url = f.get('url')
175 if not format_url:
176 continue
177 # Stat URL generation algorithm is reverse engineered from
178 # download_*_bundle_*.js
179 stat_url = update_url_query(
180 format_url.replace('/download/', '/statdownload/'), {
181 '.rand': int(time.time() * 1000 * random.random()),
182 })
183 format_id = f.get('encoding_name') or format_id
184 stat = self._download_json(
185 stat_url, track_id, 'Downloading %s JSON' % format_id,
186 transform_source=lambda s: s[s.index('{'):s.rindex('}') + 1],
187 fatal=False)
188 if not stat:
189 continue
190 retry_url = url_or_none(stat.get('retry_url'))
191 if not retry_url:
192 continue
193 formats.append({
194 'url': self._proto_relative_url(retry_url, 'http:'),
195 'ext': download_formats.get(format_id),
196 'format_id': format_id,
197 'format_note': f.get('description'),
198 'filesize': parse_filesize(f.get('size_mb')),
199 'vcodec': 'none',
200 })
201
202 self._sort_formats(formats)
203
204 title = '%s - %s' % (artist, track) if artist else track
205
206 if not duration:
207 duration = float_or_none(self._html_search_meta(
208 'duration', webpage, default=None))
209
210 return {
211 'id': track_id,
212 'title': title,
213 'thumbnail': thumbnail,
214 'uploader': artist,
215 'timestamp': timestamp,
216 'release_date': release_date,
217 'duration': duration,
218 'track': track,
219 'track_number': track_number,
220 'track_id': track_id,
221 'artist': artist,
222 'album': album,
223 'formats': formats,
224 }
225
226
227 class BandcampAlbumIE(InfoExtractor):
228 IE_NAME = 'Bandcamp:album'
229 _VALID_URL = r'https?://(?:(?P<subdomain>[^.]+)\.)?bandcamp\.com(?:/album/(?P<album_id>[^/?#&]+))?'
230
231 _TESTS = [{
232 'url': 'http://blazo.bandcamp.com/album/jazz-format-mixtape-vol-1',
233 'playlist': [
234 {
235 'md5': '39bc1eded3476e927c724321ddf116cf',
236 'info_dict': {
237 'id': '1353101989',
238 'ext': 'mp3',
239 'title': 'Intro',
240 }
241 },
242 {
243 'md5': '1a2c32e2691474643e912cc6cd4bffaa',
244 'info_dict': {
245 'id': '38097443',
246 'ext': 'mp3',
247 'title': 'Kero One - Keep It Alive (Blazo remix)',
248 }
249 },
250 ],
251 'info_dict': {
252 'title': 'Jazz Format Mixtape vol.1',
253 'id': 'jazz-format-mixtape-vol-1',
254 'uploader_id': 'blazo',
255 },
256 'params': {
257 'playlistend': 2
258 },
259 'skip': 'Bandcamp imposes download limits.'
260 }, {
261 'url': 'http://nightbringer.bandcamp.com/album/hierophany-of-the-open-grave',
262 'info_dict': {
263 'title': 'Hierophany of the Open Grave',
264 'uploader_id': 'nightbringer',
265 'id': 'hierophany-of-the-open-grave',
266 },
267 'playlist_mincount': 9,
268 }, {
269 'url': 'http://dotscale.bandcamp.com',
270 'info_dict': {
271 'title': 'Loom',
272 'id': 'dotscale',
273 'uploader_id': 'dotscale',
274 },
275 'playlist_mincount': 7,
276 }, {
277 # with escaped quote in title
278 'url': 'https://jstrecords.bandcamp.com/album/entropy-ep',
279 'info_dict': {
280 'title': '"Entropy" EP',
281 'uploader_id': 'jstrecords',
282 'id': 'entropy-ep',
283 },
284 'playlist_mincount': 3,
285 }, {
286 # not all tracks have songs
287 'url': 'https://insulters.bandcamp.com/album/we-are-the-plague',
288 'info_dict': {
289 'id': 'we-are-the-plague',
290 'title': 'WE ARE THE PLAGUE',
291 'uploader_id': 'insulters',
292 },
293 'playlist_count': 2,
294 }]
295
296 @classmethod
297 def suitable(cls, url):
298 return (False
299 if BandcampWeeklyIE.suitable(url) or BandcampIE.suitable(url)
300 else super(BandcampAlbumIE, cls).suitable(url))
301
302 def _real_extract(self, url):
303 mobj = re.match(self._VALID_URL, url)
304 uploader_id = mobj.group('subdomain')
305 album_id = mobj.group('album_id')
306 playlist_id = album_id or uploader_id
307 webpage = self._download_webpage(url, playlist_id)
308 track_elements = re.findall(
309 r'(?s)<div[^>]*>(.*?<a[^>]+href="([^"]+?)"[^>]+itemprop="url"[^>]*>.*?)</div>', webpage)
310 if not track_elements:
311 raise ExtractorError('The page doesn\'t contain any tracks')
312 # Only tracks with duration info have songs
313 entries = [
314 self.url_result(
315 compat_urlparse.urljoin(url, t_path),
316 ie=BandcampIE.ie_key(),
317 video_title=self._search_regex(
318 r'<span\b[^>]+\bitemprop=["\']name["\'][^>]*>([^<]+)',
319 elem_content, 'track title', fatal=False))
320 for elem_content, t_path in track_elements
321 if self._html_search_meta('duration', elem_content, default=None)]
322
323 title = self._html_search_regex(
324 r'album_title\s*(?:&quot;|["\']):\s*(&quot;|["\'])(?P<album>(?:\\\1|((?!\1).))+)\1',
325 webpage, 'title', fatal=False, group='album')
326
327 if title:
328 title = title.replace(r'\"', '"')
329
330 return {
331 '_type': 'playlist',
332 'uploader_id': uploader_id,
333 'id': playlist_id,
334 'title': title,
335 'entries': entries,
336 }
337
338
339 class BandcampWeeklyIE(InfoExtractor):
340 IE_NAME = 'Bandcamp:weekly'
341 _VALID_URL = r'https?://(?:www\.)?bandcamp\.com/?\?(?:.*?&)?show=(?P<id>\d+)'
342 _TESTS = [{
343 'url': 'https://bandcamp.com/?show=224',
344 'md5': 'b00df799c733cf7e0c567ed187dea0fd',
345 'info_dict': {
346 'id': '224',
347 'ext': 'opus',
348 'title': 'BC Weekly April 4th 2017 - Magic Moments',
349 'description': 'md5:5d48150916e8e02d030623a48512c874',
350 'duration': 5829.77,
351 'release_date': '20170404',
352 'series': 'Bandcamp Weekly',
353 'episode': 'Magic Moments',
354 'episode_number': 208,
355 'episode_id': '224',
356 }
357 }, {
358 'url': 'https://bandcamp.com/?blah/blah@&show=228',
359 'only_matching': True
360 }]
361
362 def _real_extract(self, url):
363 video_id = self._match_id(url)
364 webpage = self._download_webpage(url, video_id)
365
366 blob = self._parse_json(
367 self._search_regex(
368 r'data-blob=(["\'])(?P<blob>{.+?})\1', webpage,
369 'blob', group='blob'),
370 video_id, transform_source=unescapeHTML)
371
372 show = blob['bcw_show']
373
374 # This is desired because any invalid show id redirects to `bandcamp.com`
375 # which happens to expose the latest Bandcamp Weekly episode.
376 show_id = int_or_none(show.get('show_id')) or int_or_none(video_id)
377
378 formats = []
379 for format_id, format_url in show['audio_stream'].items():
380 if not url_or_none(format_url):
381 continue
382 for known_ext in KNOWN_EXTENSIONS:
383 if known_ext in format_id:
384 ext = known_ext
385 break
386 else:
387 ext = None
388 formats.append({
389 'format_id': format_id,
390 'url': format_url,
391 'ext': ext,
392 'vcodec': 'none',
393 })
394 self._sort_formats(formats)
395
396 title = show.get('audio_title') or 'Bandcamp Weekly'
397 subtitle = show.get('subtitle')
398 if subtitle:
399 title += ' - %s' % subtitle
400
401 episode_number = None
402 seq = blob.get('bcw_seq')
403
404 if seq and isinstance(seq, list):
405 try:
406 episode_number = next(
407 int_or_none(e.get('episode_number'))
408 for e in seq
409 if isinstance(e, dict) and int_or_none(e.get('id')) == show_id)
410 except StopIteration:
411 pass
412
413 return {
414 'id': video_id,
415 'title': title,
416 'description': show.get('desc') or show.get('short_desc'),
417 'duration': float_or_none(show.get('audio_duration')),
418 'is_live': False,
419 'release_date': unified_strdate(show.get('published_date')),
420 'series': 'Bandcamp Weekly',
421 'episode': show.get('subtitle'),
422 'episode_number': episode_number,
423 'episode_id': compat_str(video_id),
424 'formats': formats
425 }