]> Raphaƫl G. Git Repositories - youtubedl/blob - youtube_dl/extractor/bandcamp.py
Fix lintian warning introduced by Ben's patch.
[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 ..utils import (
8 compat_str,
9 compat_urlparse,
10 ExtractorError,
11 )
12
13
14 class BandcampIE(InfoExtractor):
15 _VALID_URL = r'https?://.*?\.bandcamp\.com/track/(?P<title>.*)'
16 _TESTS = [{
17 'url': 'http://youtube-dl.bandcamp.com/track/youtube-dl-test-song',
18 'md5': 'c557841d5e50261777a6585648adf439',
19 'info_dict': {
20 'id': '1812978515',
21 'ext': 'mp3',
22 'title': "youtube-dl \"'/\\\u00e4\u21ad - youtube-dl test song \"'/\\\u00e4\u21ad",
23 'duration': 9.8485,
24 },
25 '_skip': 'There is a limit of 200 free downloads / month for the test song'
26 }, {
27 'url': 'http://benprunty.bandcamp.com/track/lanius-battle',
28 'md5': '2b68e5851514c20efdff2afc5603b8b4',
29 'info_dict': {
30 'id': '2650410135',
31 'ext': 'mp3',
32 'title': 'Lanius (Battle)',
33 'uploader': 'Ben Prunty Music',
34 },
35 }]
36
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)
42 if not m_download:
43 m_trackinfo = re.search(r'trackinfo: (.+),\s*?\n', webpage)
44 if m_trackinfo:
45 json_code = m_trackinfo.group(1)
46 data = json.loads(json_code)[0]
47
48 formats = []
49 for format_id, format_url in data['file'].items():
50 ext, abr_str = format_id.split('-', 1)
51 formats.append({
52 'format_id': format_id,
53 'url': format_url,
54 'ext': ext,
55 'vcodec': 'none',
56 'acodec': ext,
57 'abr': int(abr_str),
58 })
59
60 self._sort_formats(formats)
61
62 return {
63 'id': compat_str(data['id']),
64 'title': data['title'],
65 'formats': formats,
66 'duration': float(data['duration']),
67 }
68 else:
69 raise ExtractorError('No free songs found')
70
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)
75
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)
93
94 return {
95 'id': video_id,
96 'title': info['title'],
97 'ext': 'mp3',
98 'vcodec': 'none',
99 'url': final_url,
100 'thumbnail': info.get('thumb_url'),
101 'uploader': info.get('artist'),
102 }
103
104
105 class BandcampAlbumIE(InfoExtractor):
106 IE_NAME = 'Bandcamp:album'
107 _VALID_URL = r'https?://(?:(?P<subdomain>[^.]+)\.)?bandcamp\.com(?:/album/(?P<title>[^?#]+))'
108
109 _TESTS = [{
110 'url': 'http://blazo.bandcamp.com/album/jazz-format-mixtape-vol-1',
111 'playlist': [
112 {
113 'md5': '39bc1eded3476e927c724321ddf116cf',
114 'info_dict': {
115 'id': '1353101989',
116 'ext': 'mp3',
117 'title': 'Intro',
118 }
119 },
120 {
121 'md5': '1a2c32e2691474643e912cc6cd4bffaa',
122 'info_dict': {
123 'id': '38097443',
124 'ext': 'mp3',
125 'title': 'Kero One - Keep It Alive (Blazo remix)',
126 }
127 },
128 ],
129 'info_dict': {
130 'title': 'Jazz Format Mixtape vol.1',
131 },
132 'params': {
133 'playlistend': 2
134 },
135 'skip': 'Bandcamp imposes download limits. See test_playlists:test_bandcamp_album for the playlist test'
136 }, {
137 'url': 'http://nightbringer.bandcamp.com/album/hierophany-of-the-open-grave',
138 'info_dict': {
139 'title': 'Hierophany of the Open Grave',
140 },
141 'playlist_mincount': 9,
142 }]
143
144 def _real_extract(self, url):
145 mobj = re.match(self._VALID_URL, url)
146 playlist_id = mobj.group('subdomain')
147 title = mobj.group('title')
148 display_id = title or playlist_id
149 webpage = self._download_webpage(url, display_id)
150 tracks_paths = re.findall(r'<a href="(.*?)" itemprop="url">', webpage)
151 if not tracks_paths:
152 raise ExtractorError('The page doesn\'t contain any tracks')
153 entries = [
154 self.url_result(compat_urlparse.urljoin(url, t_path), ie=BandcampIE.ie_key())
155 for t_path in tracks_paths]
156 title = self._search_regex(r'album_title : "(.*?)"', webpage, 'title')
157 return {
158 '_type': 'playlist',
159 'id': playlist_id,
160 'display_id': display_id,
161 'title': title,
162 'entries': entries,
163 }