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