]> jfr.im git - yt-dlp.git/blob - youtube_dl/extractor/deezer.py
Merge remote-tracking branch 'drags/yt-feed-loadmore'
[yt-dlp.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 int_or_none,
9 orderedSet,
10 )
11
12
13 class DeezerPlaylistIE(InfoExtractor):
14 _VALID_URL = r'https?://(?:www\.)?deezer\.com/playlist/(?P<id>[0-9]+)'
15 _TEST = {
16 'url': 'http://www.deezer.com/playlist/176747451',
17 'info_dict': {
18 'id': '176747451',
19 'title': 'Best!',
20 'uploader': 'Anonymous',
21 'thumbnail': 're:^https?://cdn-images.deezer.com/images/cover/.*\.jpg$',
22 },
23 'playlist_count': 30,
24 }
25
26 def _real_extract(self, url):
27 if 'test' not in self._downloader.params:
28 self._downloader.report_warning('For now, this extractor only supports the 30 second previews. Patches welcome!')
29
30 mobj = re.match(self._VALID_URL, url)
31 playlist_id = mobj.group('id')
32
33 webpage = self._download_webpage(url, playlist_id)
34 data_json = self._search_regex(
35 r'naboo\.display\(\'[^\']+\',\s*(.*?)\);\n', webpage, 'data JSON')
36 data = json.loads(data_json)
37
38 playlist_title = data.get('DATA', {}).get('TITLE')
39 playlist_uploader = data.get('DATA', {}).get('PARENT_USERNAME')
40 playlist_thumbnail = self._search_regex(
41 r'<img id="naboo_playlist_image".*?src="([^"]+)"', webpage,
42 'playlist thumbnail')
43
44 preview_pattern = self._search_regex(
45 r"var SOUND_PREVIEW_GATEWAY\s*=\s*'([^']+)';", webpage,
46 'preview URL pattern', fatal=False)
47 entries = []
48 for s in data['SONGS']['data']:
49 puid = s['MD5_ORIGIN']
50 preview_video_url = preview_pattern.\
51 replace('{0}', puid[0]).\
52 replace('{1}', puid).\
53 replace('{2}', s['MEDIA_VERSION'])
54 formats = [{
55 'format_id': 'preview',
56 'url': preview_video_url,
57 'preference': -100, # Only the first 30 seconds
58 'ext': 'mp3',
59 }]
60 self._sort_formats(formats)
61 artists = ', '.join(
62 orderedSet(a['ART_NAME'] for a in s['ARTISTS']))
63 entries.append({
64 'id': s['SNG_ID'],
65 'duration': int_or_none(s.get('DURATION')),
66 'title': '%s - %s' % (artists, s['SNG_TITLE']),
67 'uploader': s['ART_NAME'],
68 'uploader_id': s['ART_ID'],
69 'age_limit': 16 if s.get('EXPLICIT_LYRICS') == '1' else 0,
70 'formats': formats,
71 })
72
73 return {
74 '_type': 'playlist',
75 'id': playlist_id,
76 'title': playlist_title,
77 'uploader': playlist_uploader,
78 'thumbnail': playlist_thumbnail,
79 'entries': entries,
80 }