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