]> jfr.im git - yt-dlp.git/blob - yt_dlp/extractor/deezer.py
[ie/orf:on] Improve extraction (#9677)
[yt-dlp.git] / yt_dlp / extractor / deezer.py
1 import json
2
3 from .common import InfoExtractor
4 from ..utils import (
5 ExtractorError,
6 int_or_none,
7 orderedSet,
8 )
9
10
11 class DeezerBaseInfoExtractor(InfoExtractor):
12 def get_data(self, url):
13 if not self.get_param('test'):
14 self.report_warning('For now, this extractor only supports the 30 second previews. Patches welcome!')
15
16 mobj = self._match_valid_url(url)
17 data_id = mobj.group('id')
18
19 webpage = self._download_webpage(url, data_id)
20 geoblocking_msg = self._html_search_regex(
21 r'<p class="soon-txt">(.*?)</p>', webpage, 'geoblocking message',
22 default=None)
23 if geoblocking_msg is not None:
24 raise ExtractorError(
25 'Deezer said: %s' % geoblocking_msg, expected=True)
26
27 data_json = self._search_regex(
28 (r'__DZR_APP_STATE__\s*=\s*({.+?})\s*</script>',
29 r'naboo\.display\(\'[^\']+\',\s*(.*?)\);\n'),
30 webpage, 'data JSON')
31 data = json.loads(data_json)
32 return data_id, webpage, data
33
34
35 class DeezerPlaylistIE(DeezerBaseInfoExtractor):
36 _VALID_URL = r'https?://(?:www\.)?deezer\.com/(../)?playlist/(?P<id>[0-9]+)'
37 _TEST = {
38 'url': 'http://www.deezer.com/playlist/176747451',
39 'info_dict': {
40 'id': '176747451',
41 'title': 'Best!',
42 'uploader': 'anonymous',
43 'thumbnail': r're:^https?://(e-)?cdns-images\.dzcdn\.net/images/cover/.*\.jpg$',
44 },
45 'playlist_count': 29,
46 }
47
48 def _real_extract(self, url):
49 playlist_id, webpage, data = self.get_data(url)
50
51 playlist_title = data.get('DATA', {}).get('TITLE')
52 playlist_uploader = data.get('DATA', {}).get('PARENT_USERNAME')
53 playlist_thumbnail = self._search_regex(
54 r'<img id="naboo_playlist_image".*?src="([^"]+)"', webpage,
55 'playlist thumbnail')
56
57 entries = []
58 for s in data.get('SONGS', {}).get('data'):
59 formats = [{
60 'format_id': 'preview',
61 'url': s.get('MEDIA', [{}])[0].get('HREF'),
62 'preference': -100, # Only the first 30 seconds
63 'ext': 'mp3',
64 }]
65 artists = ', '.join(
66 orderedSet(a.get('ART_NAME') for a in s.get('ARTISTS')))
67 entries.append({
68 'id': s.get('SNG_ID'),
69 'duration': int_or_none(s.get('DURATION')),
70 'title': '%s - %s' % (artists, s.get('SNG_TITLE')),
71 'uploader': s.get('ART_NAME'),
72 'uploader_id': s.get('ART_ID'),
73 'age_limit': 16 if s.get('EXPLICIT_LYRICS') == '1' else 0,
74 'formats': formats,
75 })
76
77 return {
78 '_type': 'playlist',
79 'id': playlist_id,
80 'title': playlist_title,
81 'uploader': playlist_uploader,
82 'thumbnail': playlist_thumbnail,
83 'entries': entries,
84 }
85
86
87 class DeezerAlbumIE(DeezerBaseInfoExtractor):
88 _VALID_URL = r'https?://(?:www\.)?deezer\.com/(../)?album/(?P<id>[0-9]+)'
89 _TEST = {
90 'url': 'https://www.deezer.com/fr/album/67505622',
91 'info_dict': {
92 'id': '67505622',
93 'title': 'Last Week',
94 'uploader': 'Home Brew',
95 'thumbnail': r're:^https?://(e-)?cdns-images\.dzcdn\.net/images/cover/.*\.jpg$',
96 },
97 'playlist_count': 7,
98 }
99
100 def _real_extract(self, url):
101 album_id, webpage, data = self.get_data(url)
102
103 album_title = data.get('DATA', {}).get('ALB_TITLE')
104 album_uploader = data.get('DATA', {}).get('ART_NAME')
105 album_thumbnail = self._search_regex(
106 r'<img id="naboo_album_image".*?src="([^"]+)"', webpage,
107 'album thumbnail')
108
109 entries = []
110 for s in data.get('SONGS', {}).get('data'):
111 formats = [{
112 'format_id': 'preview',
113 'url': s.get('MEDIA', [{}])[0].get('HREF'),
114 'preference': -100, # Only the first 30 seconds
115 'ext': 'mp3',
116 }]
117 artists = ', '.join(
118 orderedSet(a.get('ART_NAME') for a in s.get('ARTISTS')))
119 entries.append({
120 'id': s.get('SNG_ID'),
121 'duration': int_or_none(s.get('DURATION')),
122 'title': '%s - %s' % (artists, s.get('SNG_TITLE')),
123 'uploader': s.get('ART_NAME'),
124 'uploader_id': s.get('ART_ID'),
125 'age_limit': 16 if s.get('EXPLICIT_LYRICS') == '1' else 0,
126 'formats': formats,
127 'track': s.get('SNG_TITLE'),
128 'track_number': int_or_none(s.get('TRACK_NUMBER')),
129 'track_id': s.get('SNG_ID'),
130 'artist': album_uploader,
131 'album': album_title,
132 'album_artist': album_uploader,
133 })
134
135 return {
136 '_type': 'playlist',
137 'id': album_id,
138 'title': album_title,
139 'uploader': album_uploader,
140 'thumbnail': album_thumbnail,
141 'entries': entries,
142 }