2 from __future__
import unicode_literals
8 from .common
import InfoExtractor
9 from ..compat
import compat_str
26 class BandcampIE(InfoExtractor
):
27 _VALID_URL
= r
'https?://[^/]+\.bandcamp\.com/track/(?P<id>[^/?#&]+)'
29 'url': 'http://youtube-dl.bandcamp.com/track/youtube-dl-test-song',
30 'md5': 'c557841d5e50261777a6585648adf439',
34 'title': "youtube-dl \"'/\\ä↭ - youtube-dl \"'/\\ä↭ - youtube-dl test song \"'/\\ä↭",
36 'uploader': 'youtube-dl "\'/\\ä↭',
37 'upload_date': '20121129',
38 'timestamp': 1354224127,
40 '_skip': 'There is a limit of 200 free downloads / month for the test song'
43 'url': 'http://benprunty.bandcamp.com/track/lanius-battle',
47 'title': 'Ben Prunty - Lanius (Battle)',
48 'thumbnail': r
're:^https?://.*\.jpg$',
49 'uploader': 'Ben Prunty',
50 'timestamp': 1396508491,
51 'upload_date': '20140403',
52 'release_timestamp': 1396483200,
53 'release_date': '20140403',
55 'track': 'Lanius (Battle)',
57 'track_id': '2650410135',
58 'artist': 'Ben Prunty',
59 'album': 'FTL: Advanced Edition Soundtrack',
62 # no free download, mp3 128
63 'url': 'https://relapsealumni.bandcamp.com/track/hail-to-fire',
64 'md5': 'fec12ff55e804bb7f7ebeb77a800c8b7',
68 'title': 'Mastodon - Hail to Fire',
69 'thumbnail': r
're:^https?://.*\.jpg$',
70 'uploader': 'Mastodon',
71 'timestamp': 1322005399,
72 'upload_date': '20111122',
73 'release_timestamp': 1076112000,
74 'release_date': '20040207',
76 'track': 'Hail to Fire',
78 'track_id': '2584466013',
80 'album': 'Call of the Mastodon',
84 def _extract_data_attr(self
, webpage
, video_id
, attr
='tralbum', fatal
=True):
85 return self
._parse
_json
(self
._html
_search
_regex
(
86 r
'data-%s=(["\'])({.+?}
)\
1' % attr, webpage,
87 attr + ' data
', group=2), video_id, fatal=fatal)
89 def _real_extract(self, url):
90 title = self._match_id(url)
91 webpage = self._download_webpage(url, title)
92 tralbum = self._extract_data_attr(webpage, title)
93 thumbnail = self._og_search_thumbnail(webpage)
101 track_info = try_get(tralbum, lambda x: x['trackinfo
'][0], dict)
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),
117 track = track_info.get('title
')
118 track_id = str_or_none(
119 track_info.get('track_id
') or track_info.get('id'))
120 track_number = int_or_none(track_info.get('track_num
'))
121 duration = float_or_none(track_info.get('duration
'))
123 embed = self._extract_data_attr(webpage, title, 'embed
', False)
124 current = tralbum.get('current
') or {}
125 artist = embed.get('artist
') or current.get('artist
') or tralbum.get('artist
')
126 timestamp = unified_timestamp(
127 current.get('publish_date
') or tralbum.get('album_publish_date
'))
129 download_link = tralbum.get('freeDownloadPage
')
131 track_id = compat_str(tralbum['id'])
133 download_webpage = self._download_webpage(
134 download_link, track_id, 'Downloading free downloads page
')
136 blob = self._extract_data_attr(download_webpage, track_id, 'blob
')
139 blob, (lambda x: x['digital_items
'][0],
140 lambda x: x['download_items
'][0]), dict)
142 downloads = info.get('downloads
')
143 if isinstance(downloads, dict):
145 track = info.get('title
')
147 artist = info.get('artist
')
149 thumbnail = info.get('thumb_url
')
151 download_formats = {}
152 download_formats_list = blob.get('download_formats
')
153 if isinstance(download_formats_list, list):
154 for f in blob['download_formats
']:
155 name, ext = f.get('name
'), f.get('file_extension
')
156 if all(isinstance(x, compat_str) for x in (name, ext)):
157 download_formats[name] = ext.strip('.')
159 for format_id, f in downloads.items():
160 format_url = f.get('url
')
163 # Stat URL generation algorithm is reverse engineered from
164 # download_*_bundle_*.js
165 stat_url = update_url_query(
166 format_url.replace('/download
/', '/statdownload
/'), {
167 '.rand
': int(time.time() * 1000 * random.random()),
169 format_id = f.get('encoding_name
') or format_id
170 stat = self._download_json(
171 stat_url, track_id, 'Downloading
%s JSON
' % format_id,
172 transform_source=lambda s: s[s.index('{'):s.rindex('}
') + 1],
176 retry_url = url_or_none(stat.get('retry_url
'))
180 'url
': self._proto_relative_url(retry_url, 'http
:'),
181 'ext
': download_formats.get(format_id),
182 'format_id
': format_id,
183 'format_note
': f.get('description
'),
184 'filesize
': parse_filesize(f.get('size_mb
')),
186 'acodec
': format_id.split('-')[0],
189 self._sort_formats(formats)
191 title = '%s - %s' % (artist, track) if artist else track
194 duration = float_or_none(self._html_search_meta(
195 'duration
', webpage, default=None))
200 'thumbnail
': thumbnail,
202 'timestamp
': timestamp,
203 'release_timestamp
': unified_timestamp(tralbum.get('album_release_date
')),
204 'duration
': duration,
206 'track_number
': track_number,
207 'track_id
': track_id,
209 'album
': embed.get('album_title
'),
214 class BandcampAlbumIE(BandcampIE):
215 IE_NAME = 'Bandcamp
:album
'
216 _VALID_URL = r'https?
://(?
:(?P
<subdomain
>[^
.]+)\
.)?bandcamp\
.com
/album
/(?P
<id>[^
/?
#&]+)'
219 'url': 'http://blazo.bandcamp.com/album/jazz-format-mixtape-vol-1',
222 'md5': '39bc1eded3476e927c724321ddf116cf',
226 'title': 'Blazo - Intro',
227 'timestamp': 1311756226,
228 'upload_date': '20110727',
233 'md5': '1a2c32e2691474643e912cc6cd4bffaa',
237 'title': 'Blazo - Kero One - Keep It Alive (Blazo remix)',
238 'timestamp': 1311757238,
239 'upload_date': '20110727',
245 'title': 'Jazz Format Mixtape vol.1',
246 'id': 'jazz-format-mixtape-vol-1',
247 'uploader_id': 'blazo',
252 'skip': 'Bandcamp imposes download limits.'
254 'url': 'http://nightbringer.bandcamp.com/album/hierophany-of-the-open-grave',
256 'title': 'Hierophany of the Open Grave',
257 'uploader_id': 'nightbringer',
258 'id': 'hierophany-of-the-open-grave',
260 'playlist_mincount': 9,
262 # with escaped quote in title
263 'url': 'https://jstrecords.bandcamp.com/album/entropy-ep',
265 'title': '"Entropy" EP',
266 'uploader_id': 'jstrecords',
268 'description': 'md5:0ff22959c943622972596062f2f366a5',
270 'playlist_mincount': 3,
272 # not all tracks have songs
273 'url': 'https://insulters.bandcamp.com/album/we-are-the-plague',
275 'id': 'we-are-the-plague',
276 'title': 'WE ARE THE PLAGUE',
277 'uploader_id': 'insulters',
278 'description': 'md5:b3cf845ee41b2b1141dc7bde9237255f',
284 def suitable(cls
, url
):
286 if BandcampWeeklyIE
.suitable(url
) or BandcampIE
.suitable(url
)
287 else super(BandcampAlbumIE
, cls
).suitable(url
))
289 def _real_extract(self
, url
):
290 uploader_id
, album_id
= self
._match
_valid
_url
(url
).groups()
291 playlist_id
= album_id
or uploader_id
292 webpage
= self
._download
_webpage
(url
, playlist_id
)
293 tralbum
= self
._extract
_data
_attr
(webpage
, playlist_id
)
294 track_info
= tralbum
.get('trackinfo')
296 raise ExtractorError('The page doesn\'t contain any tracks')
297 # Only tracks with duration info have songs
300 urljoin(url
, t
['title_link']), BandcampIE
.ie_key(),
301 str_or_none(t
.get('track_id') or t
.get('id')), t
.get('title'))
303 if t
.get('duration')]
305 current
= tralbum
.get('current') or {}
309 'uploader_id': uploader_id
,
311 'title': current
.get('title'),
312 'description': current
.get('about'),
317 class BandcampWeeklyIE(BandcampIE
):
318 IE_NAME
= 'Bandcamp:weekly'
319 _VALID_URL
= r
'https?://(?:www\.)?bandcamp\.com/?\?(?:.*?&)?show=(?P<id>\d+)'
321 'url': 'https://bandcamp.com/?show=224',
322 'md5': 'b00df799c733cf7e0c567ed187dea0fd',
326 'title': 'BC Weekly April 4th 2017 - Magic Moments',
327 'description': 'md5:5d48150916e8e02d030623a48512c874',
329 'release_date': '20170404',
330 'series': 'Bandcamp Weekly',
331 'episode': 'Magic Moments',
338 'url': 'https://bandcamp.com/?blah/blah@&show=228',
339 'only_matching': True
342 def _real_extract(self
, url
):
343 show_id
= self
._match
_id
(url
)
344 webpage
= self
._download
_webpage
(url
, show_id
)
346 blob
= self
._extract
_data
_attr
(webpage
, show_id
, 'blob')
348 show
= blob
['bcw_data'][show_id
]
351 for format_id
, format_url
in show
['audio_stream'].items():
352 if not url_or_none(format_url
):
354 for known_ext
in KNOWN_EXTENSIONS
:
355 if known_ext
in format_id
:
361 'format_id': format_id
,
366 self
._sort
_formats
(formats
)
368 title
= show
.get('audio_title') or 'Bandcamp Weekly'
369 subtitle
= show
.get('subtitle')
371 title
+= ' - %s' % subtitle
376 'description': show
.get('desc') or show
.get('short_desc'),
377 'duration': float_or_none(show
.get('audio_duration')),
379 'release_date': unified_strdate(show
.get('published_date')),
380 'series': 'Bandcamp Weekly',
381 'episode': show
.get('subtitle'),
382 'episode_id': show_id
,
387 class BandcampUserIE(InfoExtractor
):
388 IE_NAME
= 'Bandcamp:user'
389 _VALID_URL
= r
'https?://(?!www\.)(?P<id>[^.]+)\.bandcamp\.com(?:/music)?/?(?:[#?]|$)'
392 # Type 1 Bandcamp user page.
393 'url': 'https://adrianvonziegler.bandcamp.com',
395 'id': 'adrianvonziegler',
396 'title': 'Discography of adrianvonziegler',
398 'playlist_mincount': 23,
400 # Bandcamp user page with only one album
401 'url': 'http://dotscale.bandcamp.com',
404 'title': 'Discography of dotscale'
408 # Type 2 Bandcamp user page.
409 'url': 'https://nightcallofficial.bandcamp.com',
411 'id': 'nightcallofficial',
412 'title': 'Discography of nightcallofficial',
416 'url': 'https://steviasphere.bandcamp.com/music',
417 'playlist_mincount': 47,
419 'id': 'steviasphere',
420 'title': 'Discography of steviasphere',
423 'url': 'https://coldworldofficial.bandcamp.com/music',
424 'playlist_mincount': 10,
426 'id': 'coldworldofficial',
427 'title': 'Discography of coldworldofficial',
430 'url': 'https://nuclearwarnowproductions.bandcamp.com/music',
431 'playlist_mincount': 399,
433 'id': 'nuclearwarnowproductions',
434 'title': 'Discography of nuclearwarnowproductions',
438 def _real_extract(self
, url
):
439 uploader
= self
._match
_id
(url
)
440 webpage
= self
._download
_webpage
(url
, uploader
)
442 discography_data
= (re
.findall(r
'<li data-item-id=["\'][^
>]+>\s
*<a href
=["\']([^"\']+)', webpage)
443 or re.findall(r'<div
[^
>]+trackTitle
["\'][^"\']+["\']([^"\']+)', webpage))
445 return self.playlist_from_matches(
446 discography_data, uploader, f'Discography of {uploader}
', getter=lambda x: urljoin(url, x))