from timeside.core import implements, interfacedoc
from timeside.core.provider import Provider
from timeside.core.exceptions import ProviderError
from timeside.core.api import IProvider
from timeside.core.tools.utils import slugify
import os
from requests import get
import json
[docs]class DeezerPreview(Provider):
"""Deezer Plugin to retrieve deezer's 30 seconds tracks preview"""
implements(IProvider)
def __init__(self, url=None, id=None, download=False, path=""):
self.url = url
self.id = id
self.path = path
self.download = download
self.info = None
if not self.url and not self.id:
raise AttributeError("A URL or an ID must be given")
elif self.id and not self.url:
self.set_url_from_id()
elif not self.id and self.url:
self.set_id_from_url()
self.get_info()
def get_info(self):
try:
request = get(self.url)
assert request.status_code == 200
except AssertionError:
raise ProviderError('deezer_preview', external_id=self.id)
self.request_dict = json.loads(request.content)
[docs] @staticmethod
@interfacedoc
def id():
return 'deezer_preview'
[docs] @staticmethod
@interfacedoc
def name():
return "Deezer Preview"
[docs] @staticmethod
@interfacedoc
def description():
return "Deezer preview provider"
[docs] @staticmethod
@interfacedoc
def domain():
return "www.deezer.com"
[docs] @staticmethod
@interfacedoc
def access():
return True
[docs] @interfacedoc
def exists(self):
return True
[docs] @interfacedoc
def set_id_from_url(self):
self.id = self.url.split("/")[-1:][0]
[docs] @interfacedoc
def set_url_from_id(self):
self.url = 'https://api.deezer.com/track/' + self.id
[docs] @interfacedoc
def get_title(self):
return self.info['title']
[docs] @interfacedoc
def get_file(self):
source_uri = self.request_dict['preview']
if self.download:
file_name = self.request_dict['artist']['name'] + '-' + self.request_dict['title_short'] + '-' + self.id
file_name = slugify(file_name) + '.mp3'
file_path = os.path.join(path,file_name)
r = get(source_uri)
if not os.path.exists(path):
os.makedirs(path)
with open(file_path,'wb') as f:
f.write(r.content)
f.close()
return file_path
else:
return source_uri