]> Raphaƫl G. Git Repositories - youtubedl/blob - youtube_dl/extractor/bandcamp.py
debian/control: Add recommends on aria2 | wget | curl to use external downloaders.
[youtubedl] / youtube_dl / extractor / bandcamp.py
1 from __future__ import unicode_literals
2
3 import json
4 import re
5
6 from .common import InfoExtractor
7 from ..compat import (
8 compat_str,
9 compat_urlparse,
10 )
11 from ..utils import (
12 ExtractorError,
13 )
14
15
16 class BandcampIE(InfoExtractor):
17 _VALID_URL = r'https?://.*?\.bandcamp\.com/track/(?P<title>.*)'
18 _TESTS = [{
19 'url': 'http://youtube-dl.bandcamp.com/track/youtube-dl-test-song',
20 'md5': 'c557841d5e50261777a6585648adf439',
21 'info_dict': {
22 'id': '1812978515',
23 'ext': 'mp3',
24 'title': "youtube-dl \"'/\\\u00e4\u21ad - youtube-dl test song \"'/\\\u00e4\u21ad",
25 'duration': 9.8485,
26 },
27 '_skip': 'There is a limit of 200 free downloads / month for the test song'
28 }, {
29 'url': 'http://benprunty.bandcamp.com/track/lanius-battle',
30 'md5': '2b68e5851514c20efdff2afc5603b8b4',
31 'info_dict': {
32 'id': '2650410135',
33 'ext': 'mp3',
34 'title': 'Lanius (Battle)',
35 'uploader': 'Ben Prunty Music',
36 },
37 }]
38
39 def _real_extract(self, url):
40 mobj = re.match(self._VALID_URL, url)
41 title = mobj.group('title')
42 webpage = self._download_webpage(url, title)
43 m_download = re.search(r'freeDownloadPage: "(.*?)"', webpage)
44 if not m_download:
45 m_trackinfo = re.search(r'trackinfo: (.+),\s*?\n', webpage)
46 if m_trackinfo:
47 json_code = m_trackinfo.group(1)
48 data = json.loads(json_code)[0]
49
50 formats = []
51 for format_id, format_url in data['file'].items():
52 ext, abr_str = format_id.split('-', 1)
53 formats.append({
54 'format_id': format_id,
55 'url': format_url,
56 'ext': ext,
57 'vcodec': 'none',
58 'acodec': ext,
59 'abr': int(abr_str),
60 })
61
62 self._sort_formats(formats)
63
64 return {
65 'id': compat_str(data['id']),
66 'title': data['title'],
67 'formats': formats,
68 'duration': float(data['duration']),
69 }
70 else:
71 raise ExtractorError('No free songs found')
72
73 download_link = m_download.group(1)
74 video_id = self._search_regex(
75 r'var TralbumData = {.*?id: (?P<id>\d+),?$',
76 webpage, 'video id', flags=re.MULTILINE | re.DOTALL)
77
78 download_webpage = self._download_webpage(download_link, video_id, 'Downloading free downloads page')
79 # We get the dictionary of the track from some javascript code
80 info = re.search(r'items: (.*?),$', download_webpage, re.MULTILINE).group(1)
81 info = json.loads(info)[0]
82 # We pick mp3-320 for now, until format selection can be easily implemented.
83 mp3_info = info['downloads']['mp3-320']
84 # If we try to use this url it says the link has expired
85 initial_url = mp3_info['url']
86 re_url = r'(?P<server>http://(.*?)\.bandcamp\.com)/download/track\?enc=mp3-320&fsig=(?P<fsig>.*?)&id=(?P<id>.*?)&ts=(?P<ts>.*)$'
87 m_url = re.match(re_url, initial_url)
88 # We build the url we will use to get the final track url
89 # This url is build in Bandcamp in the script download_bunde_*.js
90 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'))
91 final_url_webpage = self._download_webpage(request_url, video_id, 'Requesting download url')
92 # If we could correctly generate the .rand field the url would be
93 # in the "download_url" key
94 final_url = re.search(r'"retry_url":"(.*?)"', final_url_webpage).group(1)
95
96 return {
97 'id': video_id,
98 'title': info['title'],
99 'ext': 'mp3',
100 'vcodec': 'none',
101 'url': final_url,
102 'thumbnail': info.get('thumb_url'),
103 'uploader': info.get('artist'),
104 }
105
106
107 class BandcampAlbumIE(InfoExtractor):
108 IE_NAME = 'Bandcamp:album'
109 _VALID_URL = r'https?://(?:(?P<subdomain>[^.]+)\.)?bandcamp\.com(?:/album/(?P<title>[^?#]+)|/?(?:$|[?#]))'
110
111 _TESTS = [{
112 'url': 'http://blazo.bandcamp.com/album/jazz-format-mixtape-vol-1',
113 'playlist': [
114 {
115 'md5': '39bc1eded3476e927c724321ddf116cf',
116 'info_dict': {
117 'id': '1353101989',
118 'ext': 'mp3',
119 'title': 'Intro',
120 }
121 },
122 {
123 'md5': '1a2c32e2691474643e912cc6cd4bffaa',
124 'info_dict': {
125 'id': '38097443',
126 'ext': 'mp3',
127 'title': 'Kero One - Keep It Alive (Blazo remix)',
128 }
129 },
130 ],
131 'info_dict': {
132 'title': 'Jazz Format Mixtape vol.1',
133 },
134 'params': {
135 'playlistend': 2
136 },
137 'skip': 'Bandcamp imposes download limits. See test_playlists:test_bandcamp_album for the playlist test'
138 }, {
139 'url': 'http://nightbringer.bandcamp.com/album/hierophany-of-the-open-grave',
140 'info_dict': {
141 'title': 'Hierophany of the Open Grave',
142 },
143 'playlist_mincount': 9,
144 }, {
145 'url': 'http://dotscale.bandcamp.com',
146 'info_dict': {
147 'title': 'Loom',
148 },
149 'playlist_mincount': 7,
150 }]
151
152 def _real_extract(self, url):
153 mobj = re.match(self._VALID_URL, url)
154 playlist_id = mobj.group('subdomain')
155 title = mobj.group('title')
156 display_id = title or playlist_id
157 webpage = self._download_webpage(url, display_id)
158 tracks_paths = re.findall(r'<a href="(.*?)" itemprop="url">', webpage)
159 if not tracks_paths:
160 raise ExtractorError('The page doesn\'t contain any tracks')
161 entries = [
162 self.url_result(compat_urlparse.urljoin(url, t_path), ie=BandcampIE.ie_key())
163 for t_path in tracks_paths]
164 title = self._search_regex(
165 r'album_title\s*:\s*"(.*?)"', webpage, 'title', fatal=False)
166 return {
167 '_type': 'playlist',
168 'id': playlist_id,
169 'display_id': display_id,
170 'title': title,
171 'entries': entries,
172 }