]> jfr.im git - yt-dlp.git/blame - youtube_dl/extractor/bandcamp.py
[vimeo] Some modernization and style fixes
[yt-dlp.git] / youtube_dl / extractor / bandcamp.py
CommitLineData
3798eadc
PH
1from __future__ import unicode_literals
2
45aef472
PH
3import json
4import re
5
6from .common import InfoExtractor
7from ..utils import (
cffa6aa1 8 compat_str,
09804265 9 compat_urlparse,
45aef472
PH
10 ExtractorError,
11)
12
13
14class BandcampIE(InfoExtractor):
15 _VALID_URL = r'http://.*?\.bandcamp\.com/track/(?P<title>.*)'
cffa6aa1 16 _TESTS = [{
3798eadc
PH
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,
6f5ac90c 23 },
3798eadc 24 '_skip': 'There is a limit of 200 free downloads / month for the test song'
cffa6aa1 25 }]
45aef472
PH
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:
cffa6aa1 34 m_trackinfo = re.search(r'trackinfo: (.+),\s*?\n', webpage)
5ecd3c6a
PH
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)
cffa6aa1 55
d35dc6d3 56 return {
cffa6aa1
PH
57 'id': compat_str(d['id']),
58 'title': d['title'],
59 'formats': formats,
5ecd3c6a 60 'duration': duration,
d35dc6d3 61 }
5ecd3c6a 62 else:
3798eadc 63 raise ExtractorError('No free songs found')
45aef472
PH
64
65 download_link = m_download.group(1)
5ecd3c6a
PH
66 video_id = re.search(
67 r'var TralbumData = {(.*?)id: (?P<id>\d*?)$',
68 webpage, re.MULTILINE | re.DOTALL).group('id')
45aef472 69
5ecd3c6a 70 download_webpage = self._download_webpage(download_link, video_id,
45aef472
PH
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.
3798eadc 77 mp3_info = info['downloads']['mp3-320']
45aef472 78 # If we try to use this url it says the link has expired
3798eadc 79 initial_url = mp3_info['url']
45aef472
PH
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
5ecd3c6a 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'))
298f16f9 85 final_url_webpage = self._download_webpage(request_url, video_id, 'Requesting download url')
45aef472
PH
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
5ecd3c6a
PH
90 return {
91 'id': video_id,
3798eadc 92 'title': info['title'],
5ecd3c6a
PH
93 'ext': 'mp3',
94 'vcodec': 'none',
95 'url': final_url,
f8b5ab8c
PH
96 'thumbnail': info.get('thumb_url'),
97 'uploader': info.get('artist'),
5ecd3c6a 98 }
09804265
JMF
99
100
101class BandcampAlbumIE(InfoExtractor):
3798eadc 102 IE_NAME = 'Bandcamp:album'
09804265
JMF
103 _VALID_URL = r'http://.*?\.bandcamp\.com/album/(?P<title>.*)'
104
d35dc6d3 105 _TEST = {
3798eadc
PH
106 'url': 'http://blazo.bandcamp.com/album/jazz-format-mixtape-vol-1',
107 'playlist': [
d35dc6d3 108 {
3798eadc
PH
109 'file': '1353101989.mp3',
110 'md5': '39bc1eded3476e927c724321ddf116cf',
111 'info_dict': {
112 'title': 'Intro',
d35dc6d3
JMF
113 }
114 },
115 {
3798eadc
PH
116 'file': '38097443.mp3',
117 'md5': '1a2c32e2691474643e912cc6cd4bffaa',
118 'info_dict': {
119 'title': 'Kero One - Keep It Alive (Blazo remix)',
d35dc6d3
JMF
120 }
121 },
122 ],
3798eadc
PH
123 'params': {
124 'playlistend': 2
d35dc6d3 125 },
3798eadc 126 'skip': 'Bancamp imposes download limits. See test_playlists:test_bandcamp_album for the playlist test'
d35dc6d3
JMF
127 }
128
09804265
JMF
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:
3798eadc 135 raise ExtractorError('The page doesn\'t contain any tracks')
09804265
JMF
136 entries = [
137 self.url_result(compat_urlparse.urljoin(url, t_path), ie=BandcampIE.ie_key())
138 for t_path in tracks_paths]
3798eadc 139 title = self._search_regex(r'album_title : "(.*?)"', webpage, 'title')
09804265
JMF
140 return {
141 '_type': 'playlist',
142 'title': title,
143 'entries': entries,
144 }