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