]> jfr.im git - yt-dlp.git/blame - youtube_dl/extractor/beatport.py
release 2017.10.01
[yt-dlp.git] / youtube_dl / extractor / beatport.py
CommitLineData
11984c74
DP
1# coding: utf-8
2from __future__ import unicode_literals
3
11984c74 4import re
11984c74 5
517bcca2 6from .common import InfoExtractor
fcd87701 7from ..compat import compat_str
517bcca2
S
8from ..utils import int_or_none
9
11984c74 10
ac943d48
DR
11class BeatportIE(InfoExtractor):
12 _VALID_URL = r'https?://(?:www\.|pro\.)?beatport\.com/track/(?P<display_id>[^/]+)/(?P<id>[0-9]+)'
11984c74 13 _TESTS = [{
ac943d48 14 'url': 'https://beatport.com/track/synesthesia-original-mix/5379371',
11984c74
DP
15 'md5': 'b3c34d8639a2f6a7f734382358478887',
16 'info_dict': {
bba3fc79
S
17 'id': '5379371',
18 'display_id': 'synesthesia-original-mix',
11984c74
DP
19 'ext': 'mp4',
20 'title': 'Froxic - Synesthesia (Original Mix)',
21 },
22 }, {
ac943d48 23 'url': 'https://beatport.com/track/love-and-war-original-mix/3756896',
11984c74
DP
24 'md5': 'e44c3025dfa38c6577fbaeb43da43514',
25 'info_dict': {
bba3fc79
S
26 'id': '3756896',
27 'display_id': 'love-and-war-original-mix',
11984c74
DP
28 'ext': 'mp3',
29 'title': 'Wolfgang Gartner - Love & War (Original Mix)',
30 },
31 }, {
ac943d48 32 'url': 'https://beatport.com/track/birds-original-mix/4991738',
11984c74
DP
33 'md5': 'a1fd8e8046de3950fd039304c186c05f',
34 'info_dict': {
bba3fc79
S
35 'id': '4991738',
36 'display_id': 'birds-original-mix',
11984c74
DP
37 'ext': 'mp4',
38 'title': "Tos, Middle Milk, Mumblin' Johnsson - Birds (Original Mix)",
39 }
40 }]
41
42 def _real_extract(self, url):
ba1d4c04
S
43 mobj = re.match(self._VALID_URL, url)
44 track_id = mobj.group('id')
45 display_id = mobj.group('display_id')
46
47 webpage = self._download_webpage(url, display_id)
11984c74 48
fcd87701
S
49 playables = self._parse_json(
50 self._search_regex(
51 r'window\.Playables\s*=\s*({.+?});', webpage,
52 'playables info', flags=re.DOTALL),
53 track_id)
11984c74 54
65c5e044 55 track = next(t for t in playables['tracks'] if t['id'] == int(track_id))
11984c74 56
11984c74
DP
57 title = ', '.join((a['name'] for a in track['artists'])) + ' - ' + track['name']
58 if track['mix']:
59 title += ' (' + track['mix'] + ')'
60
11984c74
DP
61 formats = []
62 for ext, info in track['preview'].items():
fcd87701 63 if not info['url']:
11984c74
DP
64 continue
65 fmt = {
66 'url': info['url'],
67 'ext': ext,
68 'format_id': ext,
69 'vcodec': 'none',
70 }
71 if ext == 'mp3':
72 fmt['preference'] = 0
73 fmt['acodec'] = 'mp3'
74 fmt['abr'] = 96
75 fmt['asr'] = 44100
76 elif ext == 'mp4':
77 fmt['preference'] = 1
78 fmt['acodec'] = 'aac'
79 fmt['abr'] = 96
80 fmt['asr'] = 44100
517bcca2 81 formats.append(fmt)
1b537781 82 self._sort_formats(formats)
11984c74 83
517bcca2 84 images = []
11984c74 85 for name, info in track['images'].items():
517bcca2
S
86 image_url = info.get('url')
87 if name == 'dynamic' or not image_url:
11984c74 88 continue
fcd87701 89 image = {
11984c74 90 'id': name,
517bcca2
S
91 'url': image_url,
92 'height': int_or_none(info.get('height')),
93 'width': int_or_none(info.get('width')),
11984c74 94 }
fcd87701 95 images.append(image)
11984c74
DP
96
97 return {
fcd87701 98 'id': compat_str(track.get('id')) or track_id,
ba1d4c04 99 'display_id': track.get('slug') or display_id,
11984c74
DP
100 'title': title,
101 'formats': formats,
517bcca2 102 'thumbnails': images,
11984c74 103 }