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