1 from __future__
import unicode_literals
7 from .common
import InfoExtractor
28 class BandcampIE(InfoExtractor
):
29 _VALID_URL
= r
'https?://[^/]+\.bandcamp\.com/track/(?P<title>[^/?#&]+)'
31 'url': 'http://youtube-dlc.bandcamp.com/track/youtube-dlc-test-song',
32 'md5': 'c557841d5e50261777a6585648adf439',
36 'title': "youtube-dl \"'/\\\u00e4\u21ad - youtube-dl \"'/\\\u00e4\u21ad - youtube-dl test song \"'/\\\u00e4\u21ad",
38 'uploader': "youtube-dl \"'/\\\u00e4\u21ad",
39 'timestamp': 1354224127,
40 'upload_date': '20121129',
42 '_skip': 'There is a limit of 200 free downloads / month for the test song'
45 'url': 'http://benprunty.bandcamp.com/track/lanius-battle',
46 'md5': '5d92af55811e47f38962a54c30b07ef0',
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',
57 'track': 'Lanius (Battle)',
59 'track_id': '2650410135',
60 'artist': 'Ben Prunty',
61 'album': 'FTL: Advanced Edition Soundtrack',
64 # no free download, mp3 128
65 'url': 'https://relapsealumni.bandcamp.com/track/hail-to-fire',
66 'md5': 'fec12ff55e804bb7f7ebeb77a800c8b7',
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',
77 'track': 'Hail to Fire',
79 'track_id': '2584466013',
81 'album': 'Call of the Mastodon',
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)
97 trackinfo_block
= self
._html
_search
_regex
(
98 r
'trackinfo(?:["\']|
"
;):\
[\s
*({.+?}
)\s
*\
],(?
:["\']|")',
99 webpage, 'track info', default='{}')
101 track_info = self._parse_json(trackinfo_block, title)
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):
108 ext, abr_str = format_id.split('-', 1)
110 'format_id': format_id,
111 'url': self._proto_relative_url(format_url, 'http:'),
115 'abr': int_or_none(abr_str),
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'))
123 data = self._html_search_regex(
124 r',(["\']|
"
;)%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
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'))
135 download_link = self._search_regex(
136 r'freeDownloadPage(?:["\']|
"
;):\s
*(["\']|")(?P<url>(?:(?!\1).)+)\1', webpage,
137 'download link', default=None, group='url')
139 track_id = self._search_regex(
140 r'\?id=(?P<id>\d+)&',
141 download_link, 'track id')
143 download_webpage = self._download_webpage(
144 download_link, track_id, 'Downloading free downloads page')
146 blob = self._parse_json(
148 r'data-blob=(["\'])(?P
<blob
>{.+?}
)\
1', download_webpage,
149 'blob
', group='blob
'),
150 track_id, transform_source=unescapeHTML)
153 blob, (lambda x: x['digital_items
'][0],
154 lambda x: x['download_items
'][0]), dict)
156 downloads = info.get('downloads
')
157 if isinstance(downloads, dict):
159 track = info.get('title
')
161 artist = info.get('artist
')
163 thumbnail = info.get('thumb_url
')
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('.')
173 for format_id, f in downloads.items():
174 format_url = f.get('url
')
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()),
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],
190 retry_url = url_or_none(stat.get('retry_url
'))
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
')),
202 self._sort_formats(formats)
204 title = '%s - %s' % (artist, track) if artist else track
207 duration = float_or_none(self._html_search_meta(
208 'duration
', webpage, default=None))
213 'thumbnail
': thumbnail,
215 'timestamp
': timestamp,
216 'release_date
': release_date,
217 'duration
': duration,
219 'track_number
': track_number,
220 'track_id
': track_id,
227 class BandcampAlbumIE(InfoExtractor):
228 IE_NAME = 'Bandcamp
:album
'
229 _VALID_URL = r'https?
://(?
:(?P
<subdomain
>[^
.]+)\
.)?bandcamp\
.com(?
:/album
/(?P
<album_id
>[^
/?
#&]+))?'
232 'url': 'http://blazo.bandcamp.com/album/jazz-format-mixtape-vol-1',
235 'md5': '39bc1eded3476e927c724321ddf116cf',
243 'md5': '1a2c32e2691474643e912cc6cd4bffaa',
247 'title': 'Kero One - Keep It Alive (Blazo remix)',
252 'title': 'Jazz Format Mixtape vol.1',
253 'id': 'jazz-format-mixtape-vol-1',
254 'uploader_id': 'blazo',
259 'skip': 'Bandcamp imposes download limits.'
261 'url': 'http://nightbringer.bandcamp.com/album/hierophany-of-the-open-grave',
263 'title': 'Hierophany of the Open Grave',
264 'uploader_id': 'nightbringer',
265 'id': 'hierophany-of-the-open-grave',
267 'playlist_mincount': 9,
269 'url': 'http://dotscale.bandcamp.com',
273 'uploader_id': 'dotscale',
275 'playlist_mincount': 7,
277 # with escaped quote in title
278 'url': 'https://jstrecords.bandcamp.com/album/entropy-ep',
280 'title': '"Entropy" EP',
281 'uploader_id': 'jstrecords',
284 'playlist_mincount': 3,
286 # not all tracks have songs
287 'url': 'https://insulters.bandcamp.com/album/we-are-the-plague',
289 'id': 'we-are-the-plague',
290 'title': 'WE ARE THE PLAGUE',
291 'uploader_id': 'insulters',
297 def suitable(cls
, url
):
299 if BandcampWeeklyIE
.suitable(url
) or BandcampIE
.suitable(url
)
300 else super(BandcampAlbumIE
, cls
).suitable(url
))
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
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)]
323 title = self._html_search_regex(
324 r'album_title\s*(?:"|["\']):\s
*("
;|
["\'])(?P<album>(?:\\\1|((?!\1).))+)\1',
325 webpage, 'title', fatal=False, group='album')
328 title = title.replace(r'\"', '"')
332 'uploader_id
': uploader_id,
339 class BandcampWeeklyIE(InfoExtractor):
340 IE_NAME = 'Bandcamp
:weekly
'
341 _VALID_URL = r'https?
://(?
:www\
.)?bandcamp\
.com
/?
\?(?
:.*?
&)?show
=(?P
<id>\d
+)'
343 'url
': 'https
://bandcamp
.com
/?show
=224',
344 'md5
': 'b00df799c733cf7e0c567ed187dea0fd
',
348 'title
': 'BC Weekly April
4th
2017 - Magic Moments
',
349 'description
': 'md5
:5d48150916e8e02d030623a48512c874
',
351 'release_date
': '20170404',
352 'series
': 'Bandcamp Weekly
',
353 'episode
': 'Magic Moments
',
354 'episode_number
': 208,
358 'url
': 'https
://bandcamp
.com
/?blah
/blah
@&show
=228',
359 'only_matching
': True
362 def _real_extract(self, url):
363 video_id = self._match_id(url)
364 webpage = self._download_webpage(url, video_id)
366 blob = self._parse_json(
368 r'data
-blob
=(["\'])(?P<blob>{.+?})\1', webpage,
369 'blob', group='blob'),
370 video_id, transform_source=unescapeHTML)
372 show = blob['bcw_show']
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)
379 for format_id, format_url in show['audio_stream'].items():
380 if not url_or_none(format_url):
382 for known_ext in KNOWN_EXTENSIONS:
383 if known_ext in format_id:
389 'format_id': format_id,
394 self._sort_formats(formats)
396 title = show.get('audio_title') or 'Bandcamp Weekly'
397 subtitle = show.get('subtitle')
399 title += ' - %s' % subtitle
401 episode_number = None
402 seq = blob.get('bcw_seq')
404 if seq and isinstance(seq, list):
406 episode_number = next(
407 int_or_none(e.get('episode_number'))
409 if isinstance(e, dict) and int_or_none(e.get('id')) == show_id)
410 except StopIteration:
416 'description': show.get('desc') or show.get('short_desc'),
417 'duration': float_or_none(show.get('audio_duration')),
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),