Merge branch 'beatport-pro' of https://github.com/djpohly/youtube-dl into djpohly...
[youtube-dl] / youtube_dl / extractor / beatportpro.py
1 # coding: utf-8
2 from __future__ import unicode_literals
3
4 from .common import InfoExtractor
5
6 import re
7 import json
8
9
10 class BeatportProIE(InfoExtractor):
11     _VALID_URL = r'https?://pro\.beatport\.com/track/.*/(?P<id>[0-9]+)'
12     _TESTS = [{
13         'url': 'https://pro.beatport.com/track/synesthesia-original-mix/5379371',
14         'md5': 'b3c34d8639a2f6a7f734382358478887',
15         'info_dict': {
16             'id': 5379371,
17             'display-id': 'synesthesia-original-mix',
18             'ext': 'mp4',
19             'title': 'Froxic - Synesthesia (Original Mix)',
20         },
21     }, {
22         'url': 'https://pro.beatport.com/track/love-and-war-original-mix/3756896',
23         'md5': 'e44c3025dfa38c6577fbaeb43da43514',
24         'info_dict': {
25             'id': 3756896,
26             'display-id': 'love-and-war-original-mix',
27             'ext': 'mp3',
28             'title': 'Wolfgang Gartner - Love & War (Original Mix)',
29         },
30     }, {
31         'url': 'https://pro.beatport.com/track/birds-original-mix/4991738',
32         'md5': 'a1fd8e8046de3950fd039304c186c05f',
33         'info_dict': {
34             'id': 4991738,
35             'display-id': 'birds-original-mix',
36             'ext': 'mp4',
37             'title': "Tos, Middle Milk, Mumblin' Johnsson - Birds (Original Mix)",
38         }
39     }]
40
41     def _real_extract(self, url):
42         track_id = self._match_id(url)
43         webpage = self._download_webpage(url, track_id)
44
45         # Extract "Playables" JSON information from the page
46         playables = self._search_regex(r'window\.Playables = ({.*?});', webpage,
47                                        'playables info', flags=re.DOTALL)
48         playables = json.loads(playables)
49
50         # Find first track with matching ID (always the first one listed?)
51         track = next(t for t in playables['tracks'] if t['id'] == int(track_id))
52
53         # Construct title from artist(s), track name, and mix name
54         title = ', '.join((a['name'] for a in track['artists'])) + ' - ' + track['name']
55         if track['mix']:
56             title += ' (' + track['mix'] + ')'
57
58         # Get format information
59         formats = []
60         for ext, info in track['preview'].items():
61             if info['url'] is None:
62                 continue
63             fmt = {
64                 'url': info['url'],
65                 'ext': ext,
66                 'format_id': ext,
67                 'vcodec': 'none',
68             }
69             if ext == 'mp3':
70                 fmt['preference'] = 0
71                 fmt['acodec'] = 'mp3'
72                 fmt['abr'] = 96
73                 fmt['asr'] = 44100
74             elif ext == 'mp4':
75                 fmt['preference'] = 1
76                 fmt['acodec'] = 'aac'
77                 fmt['abr'] = 96
78                 fmt['asr'] = 44100
79             formats += [fmt]
80         formats.sort(key=lambda f: f['preference'])
81
82         # Get album art as thumbnails
83         imgs = []
84         for name, info in track['images'].items():
85             if name == 'dynamic' or info['url'] is None:
86                 continue
87             img = {
88                 'id': name,
89                 'url': info['url'],
90                 'height': info['height'],
91                 'width': info['width'],
92             }
93             imgs += [img]
94
95         return {
96             'id': track['id'],
97             'display-id': track['slug'],
98             'title': title,
99             'formats': formats,
100             'thumbnails': imgs,
101         }