]> jfr.im git - yt-dlp.git/blame - youtube_dl/extractor/bandcamp.py
[nhl] Modernize
[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):
b48f147d 15 _VALID_URL = r'https?://.*?\.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",
79981f03 22 "duration": 9.8485,
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)
45aef472 31 m_download = re.search(r'freeDownloadPage: "(.*?)"', webpage)
79981f03 32 if not m_download:
cffa6aa1 33 m_trackinfo = re.search(r'trackinfo: (.+),\s*?\n', webpage)
5ecd3c6a
PH
34 if m_trackinfo:
35 json_code = m_trackinfo.group(1)
79981f03 36 data = json.loads(json_code)[0]
5ecd3c6a 37
5ecd3c6a 38 formats = []
79981f03 39 for format_id, format_url in data['file'].items():
2902d44f 40 ext, abr_str = format_id.split('-', 1)
5ecd3c6a
PH
41 formats.append({
42 'format_id': format_id,
43 'url': format_url,
79981f03 44 'ext': ext,
5ecd3c6a 45 'vcodec': 'none',
79981f03 46 'acodec': ext,
47 'abr': int(abr_str),
5ecd3c6a
PH
48 })
49
50 self._sort_formats(formats)
cffa6aa1 51
d35dc6d3 52 return {
79981f03 53 'id': compat_str(data['id']),
54 'title': data['title'],
cffa6aa1 55 'formats': formats,
79981f03 56 'duration': float(data['duration']),
d35dc6d3 57 }
5ecd3c6a 58 else:
3798eadc 59 raise ExtractorError('No free songs found')
45aef472
PH
60
61 download_link = m_download.group(1)
5ecd3c6a
PH
62 video_id = re.search(
63 r'var TralbumData = {(.*?)id: (?P<id>\d*?)$',
64 webpage, re.MULTILINE | re.DOTALL).group('id')
45aef472 65
79981f03 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)
45aef472
PH
69 info = json.loads(info)[0]
70 # We pick mp3-320 for now, until format selection can be easily implemented.
3798eadc 71 mp3_info = info['downloads']['mp3-320']
45aef472 72 # If we try to use this url it says the link has expired
3798eadc 73 initial_url = mp3_info['url']
45aef472
PH
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
5ecd3c6a 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'))
298f16f9 79 final_url_webpage = self._download_webpage(request_url, video_id, 'Requesting download url')
45aef472
PH
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
5ecd3c6a
PH
84 return {
85 'id': video_id,
3798eadc 86 'title': info['title'],
5ecd3c6a
PH
87 'ext': 'mp3',
88 'vcodec': 'none',
89 'url': final_url,
f8b5ab8c
PH
90 'thumbnail': info.get('thumb_url'),
91 'uploader': info.get('artist'),
5ecd3c6a 92 }
09804265
JMF
93
94
95class BandcampAlbumIE(InfoExtractor):
3798eadc 96 IE_NAME = 'Bandcamp:album'
79981f03 97 _VALID_URL = r'https?://(?:(?P<subdomain>[^.]+)\.)?bandcamp\.com(?:/album/(?P<title>[^?#]+))'
09804265 98
d35dc6d3 99 _TEST = {
3798eadc
PH
100 'url': 'http://blazo.bandcamp.com/album/jazz-format-mixtape-vol-1',
101 'playlist': [
d35dc6d3 102 {
3798eadc
PH
103 'file': '1353101989.mp3',
104 'md5': '39bc1eded3476e927c724321ddf116cf',
105 'info_dict': {
106 'title': 'Intro',
d35dc6d3
JMF
107 }
108 },
109 {
3798eadc
PH
110 'file': '38097443.mp3',
111 'md5': '1a2c32e2691474643e912cc6cd4bffaa',
112 'info_dict': {
113 'title': 'Kero One - Keep It Alive (Blazo remix)',
d35dc6d3
JMF
114 }
115 },
116 ],
3798eadc
PH
117 'params': {
118 'playlistend': 2
d35dc6d3 119 },
79981f03 120 'skip': 'Bandcamp imposes download limits. See test_playlists:test_bandcamp_album for the playlist test'
d35dc6d3
JMF
121 }
122
09804265
JMF
123 def _real_extract(self, url):
124 mobj = re.match(self._VALID_URL, url)
b48f147d 125 playlist_id = mobj.group('subdomain')
09804265 126 title = mobj.group('title')
b48f147d
PH
127 display_id = title or playlist_id
128 webpage = self._download_webpage(url, display_id)
09804265
JMF
129 tracks_paths = re.findall(r'<a href="(.*?)" itemprop="url">', webpage)
130 if not tracks_paths:
3798eadc 131 raise ExtractorError('The page doesn\'t contain any tracks')
09804265
JMF
132 entries = [
133 self.url_result(compat_urlparse.urljoin(url, t_path), ie=BandcampIE.ie_key())
134 for t_path in tracks_paths]
3798eadc 135 title = self._search_regex(r'album_title : "(.*?)"', webpage, 'title')
09804265
JMF
136 return {
137 '_type': 'playlist',
b48f147d
PH
138 'id': playlist_id,
139 'display_id': display_id,
09804265
JMF
140 'title': title,
141 'entries': entries,
142 }