2019-05-23 18:10:14 +09:00
|
|
|
import xml.dom.minidom as minidom
|
|
|
|
|
2019-05-17 00:29:49 +09:00
|
|
|
from yandex_music import YandexMusicObject
|
|
|
|
|
|
|
|
|
|
|
|
class DownloadInfo(YandexMusicObject):
|
|
|
|
def __init__(self,
|
|
|
|
codec,
|
|
|
|
bitrate_in_kbps,
|
|
|
|
gain,
|
|
|
|
preview,
|
|
|
|
download_info_url,
|
|
|
|
client=None,
|
|
|
|
**kwargs):
|
|
|
|
self.codec = codec
|
|
|
|
self.bitrate_in_kbps = bitrate_in_kbps
|
|
|
|
self.gain = gain
|
|
|
|
self.preview = preview
|
|
|
|
self.download_info_url = download_info_url
|
|
|
|
|
2019-05-23 18:10:14 +09:00
|
|
|
self.direct_link = None
|
|
|
|
|
2019-05-17 00:29:49 +09:00
|
|
|
self.client = client
|
|
|
|
|
2019-05-23 18:10:14 +09:00
|
|
|
@staticmethod
|
|
|
|
def _get_text_node_data(elements):
|
|
|
|
for element in elements:
|
|
|
|
nodes = element.childNodes
|
|
|
|
for node in nodes:
|
|
|
|
if node.nodeType == node.TEXT_NODE:
|
|
|
|
return node.data
|
|
|
|
|
|
|
|
def get_direct_link(self):
|
|
|
|
# Available within one minute after receiving download_info, otherwise 410!
|
|
|
|
result = self.client._request.retrieve(self.download_info_url)
|
|
|
|
|
|
|
|
doc = minidom.parseString(result.text)
|
|
|
|
host = self._get_text_node_data(doc.getElementsByTagName('host'))
|
|
|
|
path = self._get_text_node_data(doc.getElementsByTagName('path'))
|
|
|
|
ts = self._get_text_node_data(doc.getElementsByTagName('ts'))
|
|
|
|
|
|
|
|
self.direct_link = f'https://{host}/get-{self.codec}/randomTrash/{ts}{path}'
|
|
|
|
|
|
|
|
return self.direct_link
|
|
|
|
|
|
|
|
def download(self, filename):
|
|
|
|
if self.direct_link is None:
|
|
|
|
self.get_direct_link()
|
|
|
|
|
|
|
|
self.client._request.download(self.direct_link, filename)
|
|
|
|
|
2019-05-17 00:29:49 +09:00
|
|
|
@classmethod
|
|
|
|
def de_json(cls, data, client):
|
|
|
|
if not data:
|
|
|
|
return None
|
|
|
|
|
|
|
|
data = super(DownloadInfo, cls).de_json(data, client)
|
|
|
|
|
|
|
|
return cls(client=client, **data)
|
|
|
|
|
|
|
|
@classmethod
|
2019-05-23 18:10:14 +09:00
|
|
|
def de_list(cls, data, client, get_direct_links=False):
|
2019-05-17 00:29:49 +09:00
|
|
|
if not data:
|
|
|
|
return []
|
|
|
|
|
|
|
|
downloads_info = list()
|
|
|
|
for download_info in data:
|
|
|
|
downloads_info.append(cls.de_json(download_info, client))
|
|
|
|
|
2019-05-23 18:10:14 +09:00
|
|
|
if get_direct_links:
|
|
|
|
for info in downloads_info:
|
|
|
|
info.get_direct_link()
|
|
|
|
|
2019-05-17 00:29:49 +09:00
|
|
|
return downloads_info
|