]> gitweb @ CieloNegro.org - youtube-dl.git/blob - youtube_dl/extractor/deezer.py
[nhk] add support for audio URLs
[youtube-dl.git] / 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': r'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'__DZR_APP_STATE__\s*=\s*({.+?})\s*</script>',
45              r'naboo\.display\(\'[^\']+\',\s*(.*?)\);\n'),
46             webpage, 'data JSON')
47         data = json.loads(data_json)
48
49         playlist_title = data.get('DATA', {}).get('TITLE')
50         playlist_uploader = data.get('DATA', {}).get('PARENT_USERNAME')
51         playlist_thumbnail = self._search_regex(
52             r'<img id="naboo_playlist_image".*?src="([^"]+)"', webpage,
53             'playlist thumbnail')
54
55         preview_pattern = self._search_regex(
56             r"var SOUND_PREVIEW_GATEWAY\s*=\s*'([^']+)';", webpage,
57             'preview URL pattern', fatal=False)
58         entries = []
59         for s in data['SONGS']['data']:
60             puid = s['MD5_ORIGIN']
61             preview_video_url = preview_pattern.\
62                 replace('{0}', puid[0]).\
63                 replace('{1}', puid).\
64                 replace('{2}', s['MEDIA_VERSION'])
65             formats = [{
66                 'format_id': 'preview',
67                 'url': preview_video_url,
68                 'preference': -100,  # Only the first 30 seconds
69                 'ext': 'mp3',
70             }]
71             self._sort_formats(formats)
72             artists = ', '.join(
73                 orderedSet(a['ART_NAME'] for a in s['ARTISTS']))
74             entries.append({
75                 'id': s['SNG_ID'],
76                 'duration': int_or_none(s.get('DURATION')),
77                 'title': '%s - %s' % (artists, s['SNG_TITLE']),
78                 'uploader': s['ART_NAME'],
79                 'uploader_id': s['ART_ID'],
80                 'age_limit': 16 if s.get('EXPLICIT_LYRICS') == '1' else 0,
81                 'formats': formats,
82             })
83
84         return {
85             '_type': 'playlist',
86             'id': playlist_id,
87             'title': playlist_title,
88             'uploader': playlist_uploader,
89             'thumbnail': playlist_thumbnail,
90             'entries': entries,
91         }