[cleanup] Misc (#8968)
[yt-dlp.git] / yt_dlp / extractor / beatport.py
blob0aecbd089d33c7772cbdb1270abd9a1c18984f17
1 import re
3 from .common import InfoExtractor
4 from ..compat import compat_str
5 from ..utils import int_or_none
8 class BeatportIE(InfoExtractor):
9 _VALID_URL = r'https?://(?:www\.|pro\.)?beatport\.com/track/(?P<display_id>[^/]+)/(?P<id>[0-9]+)'
10 _TESTS = [{
11 'url': 'https://beatport.com/track/synesthesia-original-mix/5379371',
12 'md5': 'b3c34d8639a2f6a7f734382358478887',
13 'info_dict': {
14 'id': '5379371',
15 'display_id': 'synesthesia-original-mix',
16 'ext': 'mp4',
17 'title': 'Froxic - Synesthesia (Original Mix)',
19 }, {
20 'url': 'https://beatport.com/track/love-and-war-original-mix/3756896',
21 'md5': 'e44c3025dfa38c6577fbaeb43da43514',
22 'info_dict': {
23 'id': '3756896',
24 'display_id': 'love-and-war-original-mix',
25 'ext': 'mp3',
26 'title': 'Wolfgang Gartner - Love & War (Original Mix)',
28 }, {
29 'url': 'https://beatport.com/track/birds-original-mix/4991738',
30 'md5': 'a1fd8e8046de3950fd039304c186c05f',
31 'info_dict': {
32 'id': '4991738',
33 'display_id': 'birds-original-mix',
34 'ext': 'mp4',
35 'title': "Tos, Middle Milk, Mumblin' Johnsson - Birds (Original Mix)",
39 def _real_extract(self, url):
40 mobj = self._match_valid_url(url)
41 track_id = mobj.group('id')
42 display_id = mobj.group('display_id')
44 webpage = self._download_webpage(url, display_id)
46 playables = self._parse_json(
47 self._search_regex(
48 r'window\.Playables\s*=\s*({.+?});', webpage,
49 'playables info', flags=re.DOTALL),
50 track_id)
52 track = next(t for t in playables['tracks'] if t['id'] == int(track_id))
54 title = ', '.join((a['name'] for a in track['artists'])) + ' - ' + track['name']
55 if track['mix']:
56 title += ' (' + track['mix'] + ')'
58 formats = []
59 for ext, info in track['preview'].items():
60 if not info['url']:
61 continue
62 fmt = {
63 'url': info['url'],
64 'ext': ext,
65 'format_id': ext,
66 'vcodec': 'none',
68 if ext == 'mp3':
69 fmt['acodec'] = 'mp3'
70 fmt['abr'] = 96
71 fmt['asr'] = 44100
72 elif ext == 'mp4':
73 fmt['acodec'] = 'aac'
74 fmt['abr'] = 96
75 fmt['asr'] = 44100
76 formats.append(fmt)
78 images = []
79 for name, info in track['images'].items():
80 image_url = info.get('url')
81 if name == 'dynamic' or not image_url:
82 continue
83 image = {
84 'id': name,
85 'url': image_url,
86 'height': int_or_none(info.get('height')),
87 'width': int_or_none(info.get('width')),
89 images.append(image)
91 return {
92 'id': compat_str(track.get('id')) or track_id,
93 'display_id': track.get('slug') or display_id,
94 'title': title,
95 'formats': formats,
96 'thumbnails': images,