196 lines
6.4 KiB
Python
Raw Normal View History

2014-09-12 07:25:58 +02:00
from __future__ import unicode_literals
import json
import re
from .common import InfoExtractor
from ..utils import (
2014-09-13 08:51:21 +02:00
ExtractorError,
2014-09-12 07:25:58 +02:00
int_or_none,
orderedSet,
2017-05-24 22:55:50 +00:00
bytes_to_intlist,
intlist_to_bytes,
2014-09-12 07:25:58 +02:00
)
2017-05-24 13:18:51 +00:00
from hashlib import md5
2017-05-24 22:55:50 +00:00
from ..aes import aes_ecb_encrypt
2017-05-24 13:18:51 +00:00
from binascii import b2a_hex
2017-05-24 15:56:49 +00:00
from ..compat import compat_ord, compat_chr
2017-05-24 13:18:51 +00:00
def md5hex(data):
""" return hex string of md5 of the given string """
2017-05-24 15:56:49 +00:00
return md5(data).hexdigest().encode('utf-8')
2017-05-24 13:18:51 +00:00
2017-05-24 16:07:26 +00:00
2017-05-24 13:18:51 +00:00
def calcurlkey(songid, md5origin, mediaver=4, fmt=1):
""" Calculate the deezer download url given the songid, origin and media+format """
data = b'\xa4'.join(_.encode("utf-8") for _ in [md5origin, str(fmt), str(songid), str(mediaver)])
2017-05-24 16:07:26 +00:00
data = b'\xa4'.join([md5hex(data), data]) + b'\xa4'
if len(data) % 16:
data += b'\x00' * (16 - len(data) % 16)
2017-05-24 22:55:50 +00:00
enc = aes_ecb_encrypt(bytes_to_intlist(data), bytes_to_intlist(b"jo6aey6haid2Teih"))
return b2a_hex(intlist_to_bytes(enc)).decode('utf-8')
2017-05-24 13:18:51 +00:00
def calcblowfishkey(songid):
""" Calculate the Blowfish decrypt key for a given songid """
h = md5hex(b"%d" % songid)
key = b"g4el58wc0zvf9na1"
2017-05-24 16:07:26 +00:00
return "".join(compat_chr(compat_ord(h[i]) ^ compat_ord(h[i + 16]) ^ compat_ord(key[i])) for i in range(16))
2017-05-24 13:18:51 +00:00
def getformat(song):
""" return format id for a song """
if song["FILESIZE_MP3_320"]:
return 3
if song["FILESIZE_MP3_256"]:
return 5
return 1
class Format:
def __init__(self, name, fmtid, ext, bitrate):
self.name = name
self.formatid = fmtid
self.extension = ext
self.bitrate = bitrate
_formatinfo = {
'MP3_MISC': Format('MP3_MISC', 0, 'mp3', 128),
'MP3_128': Format('MP3_128', 1, 'mp3', 128),
# 2 used to be MP3_192
'MP3_320': Format('MP3_320', 3, 'mp3', 320),
# 4 used to be AAC_96
'MP3_256': Format('MP3_256', 5, 'mp3', 256),
'AAC_64': Format('AAC_64', 6, 'aac', 64), # used to be AAC_192
'MP3_192': Format('MP3_192', 7, 'mp3', 192),
'AAC_96': Format('AAC_96', 8, 'aac', 96),
'FLAC': Format('FLAC', 9, 'flac', 0),
'MP3_64': Format('MP3_64', 10, 'mp3', 64),
'MP3_32': Format('MP3_32', 11, 'mp3', 32),
}
def convert_to_int(x):
try:
return int(x)
except:
return 0
def songformats(s):
"""
Enumerate all formats for a song
"""
for k, v in s.items():
if k.startswith("FILESIZE_") and convert_to_int(v):
name = k[9:]
if name in _formatinfo:
yield _formatinfo[name]
2017-05-24 15:56:49 +00:00
class DeezerPlaylistIE(InfoExtractor):
_VALID_URL = r'https?://(?:www\.)?deezer\.com/(?P<type>\w+)/(?P<id>[0-9]+)'
2017-05-24 15:56:49 +00:00
_TEST = {
'url': 'http://www.deezer.com/playlist/176747451',
'info_dict': {
'id': '176747451',
'title': 'Best!',
'uploader': 'anonymous',
'thumbnail': r're:^https?://\S*cdn-images.deezer.com/images/playlist/.*\.jpg$',
2017-05-24 15:56:49 +00:00
},
'playlist_count': 30,
}
def _real_extract(self, url):
mobj = re.match(self._VALID_URL, url)
list_id = mobj.group('id')
list_type = mobj.group('type')
2017-05-24 15:56:49 +00:00
webpage = self._download_webpage(url, list_id)
2017-05-24 15:56:49 +00:00
geoblocking_msg = self._html_search_regex(
r'<div class="page-soon.*<h2>(.*?)</h2>', webpage, 'geoblocking message',
2017-05-24 15:56:49 +00:00
default=None)
if geoblocking_msg is not None:
raise ExtractorError(
'Deezer said: %s' % geoblocking_msg, expected=True)
host_stream_cdn = self._search_regex(
r'var\s+HOST_STREAM_CDN\s*=\s*[\'"](.*?)[\'"]', webpage, 'host stream cdn')
setting_domain_img = self._search_regex(
r'var\s+SETTING_DOMAIN_IMG\s*=\s*[\'"](.*?)[\'"]', webpage, 'setting domain img')
2017-05-24 15:56:49 +00:00
data_json = self._search_regex(
(r'__DZR_APP_STATE__\s*=\s*({.+?})\s*</script>',
r'naboo\.display\(\'[^\']+\',\s*(.*?)\);\n'),
webpage, 'data JSON')
data = json.loads(data_json)
list_title = data.get('DATA', {}).get('TITLE')
list_uploader = data.get('DATA', {}).get('PARENT_USERNAME')
list_thumbnail = setting_domain_img + ("/playlist/" if list_type == 'playlist' else "/cover/")
2017-05-24 15:56:49 +00:00
entries = []
if 'SONGS' in data:
# playlist, album
songlist = data['SONGS']['data']
if list_type == 'playlist':
listimgid = data['DATA']['PLAYLIST_PICTURE']
else:
listimgid = data['DATA']['ALB_PICTURE']
2017-05-24 15:56:49 +00:00
elif 'MD5_ORIGIN' in data['DATA']:
# track
2017-05-24 16:07:26 +00:00
songlist = [data['DATA']]
listimgid = data['DATA']['ALB_PICTURE']
2017-05-24 15:56:49 +00:00
elif "ALBUMS" in data:
# artist
2017-05-24 15:56:49 +00:00
songlist = []
for album in data["ALBUMS"]["data"]:
songlist.extend(album['SONGS']['data'])
listimgid = data['DATA']['ART_PICTURE']
2017-05-24 15:56:49 +00:00
else:
raise ExtractorError('Could not find songs')
for s in songlist:
formats = []
for fmt in songformats(s):
urlkey = calcurlkey(int(s["SNG_ID"]), str(s["MD5_ORIGIN"]), int(s["MEDIA_VERSION"]), fmt.formatid)
url = host_stream_cdn.replace('{0}', str(s["MD5_ORIGIN"])[0]) + "/" + urlkey
formats.append({
'format_id': fmt.name,
'key': calcblowfishkey(int(s["SNG_ID"])),
'url': url,
'preference': fmt.bitrate,
'ext': fmt.extension,
'protocol': 'deezer',
'vcodec': 'none',
})
2017-05-24 15:56:49 +00:00
self._sort_formats(formats)
artists = ', '.join(
orderedSet(a['ART_NAME'] for a in s['ARTISTS']))
entries.append({
'id': s['SNG_ID'],
'duration': int_or_none(s.get('DURATION')),
'title': '%s - %s' % (artists, s['SNG_TITLE']),
'uploader': s['ART_NAME'],
'uploader_id': s['ART_ID'],
'age_limit': 16 if convert_to_int(s.get('EXPLICIT_LYRICS')) else 0,
2017-05-24 15:56:49 +00:00
'formats': formats,
'thumbnail': setting_domain_img + '/cover/' + s['ALB_PICTURE'] + '/200x200.jpg'
2017-05-24 15:56:49 +00:00
})
return {
'_type': 'playlist',
'id': list_id,
'title': list_title,
'uploader': list_uploader,
'thumbnail': list_thumbnail + listimgid + '/200x200.jpg',
2017-05-24 15:56:49 +00:00
'entries': entries,
}