]> git.bitcoin.ninja Git - youtube-dl/blob - youtube_dl/extractor/deezer.py
[brightcove] fix format sorting
[youtube-dl] / youtube_dl / extractor / deezer.py
1 from __future__ import unicode_literals
2
3 import json
4 import re
5
6 from .common import InfoExtractor
7 from ..utils import (
8     ExtractorError,
9     int_or_none,
10     orderedSet,
11 )
12
13
14 class DeezerPlaylistIE(InfoExtractor):
15     _VALID_URL = r'https?://(?:www\.)?deezer\.com/playlist/(?P<id>[0-9]+)'
16     _TEST = {
17         'url': 'http://www.deezer.com/playlist/176747451',
18         'info_dict': {
19             'id': '176747451',
20             'title': 'Best!',
21             'uploader': 'Anonymous',
22             'thumbnail': 're:^https?://cdn-images.deezer.com/images/cover/.*\.jpg$',
23         },
24         'playlist_count': 30,
25         'skip': 'Only available in .de',
26     }
27
28     def _real_extract(self, url):
29         if 'test' not in self._downloader.params:
30             self._downloader.report_warning('For now, this extractor only supports the 30 second previews. Patches welcome!')
31
32         mobj = re.match(self._VALID_URL, url)
33         playlist_id = mobj.group('id')
34
35         webpage = self._download_webpage(url, playlist_id)
36         geoblocking_msg = self._html_search_regex(
37             r'<p class="soon-txt">(.*?)</p>', webpage, 'geoblocking message',
38             default=None)
39         if geoblocking_msg is not None:
40             raise ExtractorError(
41                 'Deezer said: %s' % geoblocking_msg, expected=True)
42
43         data_json = self._search_regex(
44             r'naboo\.display\(\'[^\']+\',\s*(.*?)\);\n', webpage, 'data JSON')
45         data = json.loads(data_json)
46
47         playlist_title = data.get('DATA', {}).get('TITLE')
48         playlist_uploader = data.get('DATA', {}).get('PARENT_USERNAME')
49         playlist_thumbnail = self._search_regex(
50             r'<img id="naboo_playlist_image".*?src="([^"]+)"', webpage,
51             'playlist thumbnail')
52
53         preview_pattern = self._search_regex(
54             r"var SOUND_PREVIEW_GATEWAY\s*=\s*'([^']+)';", webpage,
55             'preview URL pattern', fatal=False)
56         entries = []
57         for s in data['SONGS']['data']:
58             puid = s['MD5_ORIGIN']
59             preview_video_url = preview_pattern.\
60                 replace('{0}', puid[0]).\
61                 replace('{1}', puid).\
62                 replace('{2}', s['MEDIA_VERSION'])
63             formats = [{
64                 'format_id': 'preview',
65                 'url': preview_video_url,
66                 'preference': -100,  # Only the first 30 seconds
67                 'ext': 'mp3',
68             }]
69             self._sort_formats(formats)
70             artists = ', '.join(
71                 orderedSet(a['ART_NAME'] for a in s['ARTISTS']))
72             entries.append({
73                 'id': s['SNG_ID'],
74                 'duration': int_or_none(s.get('DURATION')),
75                 'title': '%s - %s' % (artists, s['SNG_TITLE']),
76                 'uploader': s['ART_NAME'],
77                 'uploader_id': s['ART_ID'],
78                 'age_limit': 16 if s.get('EXPLICIT_LYRICS') == '1' else 0,
79                 'formats': formats,
80             })
81
82         return {
83             '_type': 'playlist',
84             'id': playlist_id,
85             'title': playlist_title,
86             'uploader': playlist_uploader,
87             'thumbnail': playlist_thumbnail,
88             'entries': entries,
89         }