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