]> jfr.im git - yt-dlp.git/blame - yt_dlp/extractor/beatport.py
[test/cookies] Improve logging
[yt-dlp.git] / yt_dlp / 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):
5ad28e7f 43 mobj = self._match_valid_url(url)
ba1d4c04
S
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':
11984c74
DP
72 fmt['acodec'] = 'mp3'
73 fmt['abr'] = 96
74 fmt['asr'] = 44100
75 elif ext == 'mp4':
11984c74
DP
76 fmt['acodec'] = 'aac'
77 fmt['abr'] = 96
78 fmt['asr'] = 44100
517bcca2 79 formats.append(fmt)
1b537781 80 self._sort_formats(formats)
11984c74 81
517bcca2 82 images = []
11984c74 83 for name, info in track['images'].items():
517bcca2
S
84 image_url = info.get('url')
85 if name == 'dynamic' or not image_url:
11984c74 86 continue
fcd87701 87 image = {
11984c74 88 'id': name,
517bcca2
S
89 'url': image_url,
90 'height': int_or_none(info.get('height')),
91 'width': int_or_none(info.get('width')),
11984c74 92 }
fcd87701 93 images.append(image)
11984c74
DP
94
95 return {
fcd87701 96 'id': compat_str(track.get('id')) or track_id,
ba1d4c04 97 'display_id': track.get('slug') or display_id,
11984c74
DP
98 'title': title,
99 'formats': formats,
517bcca2 100 'thumbnails': images,
11984c74 101 }