]>
Raphaƫl G. Git Repositories - youtubedl/blob - youtube_dl/extractor/bandcamp.py
c13446665d2fb0e202973a26f5c7499c325719d1
   1 from __future__ 
import unicode_literals
 
   6 from .common 
import InfoExtractor
 
  14 class BandcampIE(InfoExtractor
): 
  15     _VALID_URL 
= r
'https?://.*?\.bandcamp\.com/track/(?P<title>.*)' 
  17         'url': 'http://youtube-dl.bandcamp.com/track/youtube-dl-test-song', 
  18         'md5': 'c557841d5e50261777a6585648adf439', 
  22             'title': "youtube-dl  \"'/\\\u00e4\u21ad - youtube-dl test song \"'/\\\u00e4\u21ad", 
  25         '_skip': 'There is a limit of 200 free downloads / month for the test song' 
  27         'url': 'http://benprunty.bandcamp.com/track/lanius-battle', 
  28         'md5': '2b68e5851514c20efdff2afc5603b8b4', 
  32             'title': 'Lanius (Battle)', 
  33             'uploader': 'Ben Prunty Music', 
  37     def _real_extract(self
, url
): 
  38         mobj 
= re
.match(self
._VALID
_URL
, url
) 
  39         title 
= mobj
.group('title') 
  40         webpage 
= self
._download
_webpage
(url
, title
) 
  41         m_download 
= re
.search(r
'freeDownloadPage: "(.*?)"', webpage
) 
  43             m_trackinfo 
= re
.search(r
'trackinfo: (.+),\s*?\n', webpage
) 
  45                 json_code 
= m_trackinfo
.group(1) 
  46                 data 
= json
.loads(json_code
)[0] 
  49                 for format_id
, format_url 
in data
['file'].items(): 
  50                     ext
, abr_str 
= format_id
.split('-', 1) 
  52                         'format_id': format_id
, 
  60                 self
._sort
_formats
(formats
) 
  63                     'id': compat_str(data
['id']), 
  64                     'title': data
['title'], 
  66                     'duration': float(data
['duration']), 
  69                 raise ExtractorError('No free songs found') 
  71         download_link 
= m_download
.group(1) 
  72         video_id 
= self
._search
_regex
( 
  73             r
'var TralbumData = {.*?id: (?P<id>\d+),?$', 
  74             webpage
, 'video id', flags
=re
.MULTILINE | re
.DOTALL
) 
  76         download_webpage 
= self
._download
_webpage
(download_link
, video_id
, 'Downloading free downloads page') 
  77         # We get the dictionary of the track from some javascript code 
  78         info 
= re
.search(r
'items: (.*?),$', download_webpage
, re
.MULTILINE
).group(1) 
  79         info 
= json
.loads(info
)[0] 
  80         # We pick mp3-320 for now, until format selection can be easily implemented. 
  81         mp3_info 
= info
['downloads']['mp3-320'] 
  82         # If we try to use this url it says the link has expired 
  83         initial_url 
= mp3_info
['url'] 
  84         re_url 
= r
'(?P<server>http://(.*?)\.bandcamp\.com)/download/track\?enc=mp3-320&fsig=(?P<fsig>.*?)&id=(?P<id>.*?)&ts=(?P<ts>.*)$' 
  85         m_url 
= re
.match(re_url
, initial_url
) 
  86         #We build the url we will use to get the final track url 
  87         # This url is build in Bandcamp in the script download_bunde_*.js 
  88         request_url 
= '%s/statdownload/track?enc=mp3-320&fsig=%s&id=%s&ts=%s&.rand=665028774616&.vrs=1' % (m_url
.group('server'), m_url
.group('fsig'), video_id
, m_url
.group('ts')) 
  89         final_url_webpage 
= self
._download
_webpage
(request_url
, video_id
, 'Requesting download url') 
  90         # If we could correctly generate the .rand field the url would be 
  91         #in the "download_url" key 
  92         final_url 
= re
.search(r
'"retry_url":"(.*?)"', final_url_webpage
).group(1) 
  96             'title': info
['title'], 
 100             'thumbnail': info
.get('thumb_url'), 
 101             'uploader': info
.get('artist'), 
 105 class BandcampAlbumIE(InfoExtractor
): 
 106     IE_NAME 
= 'Bandcamp:album' 
 107     _VALID_URL 
= r
'https?://(?:(?P<subdomain>[^.]+)\.)?bandcamp\.com(?:/album/(?P<title>[^?#]+))' 
 110         'url': 'http://blazo.bandcamp.com/album/jazz-format-mixtape-vol-1', 
 113                 'file': '1353101989.mp3', 
 114                 'md5': '39bc1eded3476e927c724321ddf116cf', 
 120                 'file': '38097443.mp3', 
 121                 'md5': '1a2c32e2691474643e912cc6cd4bffaa', 
 123                     'title': 'Kero One - Keep It Alive (Blazo remix)', 
 130         'skip': 'Bandcamp imposes download limits. See test_playlists:test_bandcamp_album for the playlist test' 
 132         'url': 'http://nightbringer.bandcamp.com/album/hierophany-of-the-open-grave', 
 134             'title': 'Hierophany of the Open Grave', 
 136         'playlist_mincount': 9, 
 139     def _real_extract(self
, url
): 
 140         mobj 
= re
.match(self
._VALID
_URL
, url
) 
 141         playlist_id 
= mobj
.group('subdomain') 
 142         title 
= mobj
.group('title') 
 143         display_id 
= title 
or playlist_id
 
 144         webpage 
= self
._download
_webpage
(url
, display_id
) 
 145         tracks_paths 
= re
.findall(r
'<a href="(.*?)" itemprop="url">', webpage
) 
 147             raise ExtractorError('The page doesn\'t contain any tracks') 
 149             self
.url_result(compat_urlparse
.urljoin(url
, t_path
), ie
=BandcampIE
.ie_key()) 
 150             for t_path 
in tracks_paths
] 
 151         title 
= self
._search
_regex
(r
'album_title : "(.*?)"', webpage
, 'title') 
 155             'display_id': display_id
,