ITRadio/server/proj/audio/service.py

62 lines
2.1 KiB
Python

from conf.settings import AZURACAST_URL, AZURACAST_API_KEY
import requests
from django.http import HttpResponse
def authorize_url(method, url):
file_url = url
API_KEY = AZURACAST_API_KEY
headers = {
"Authorization": f"Bearer {API_KEY}"
}
return requests.request(method, file_url, headers=headers)
class AzuraCast:
api_key = AZURACAST_API_KEY
url = AZURACAST_URL
@staticmethod
def get_nowplaying():
file_url = F"{AZURACAST_URL}api/nowplaying/it-radio"
response = authorize_url("get", file_url)
return response.json()
@staticmethod
def get_all_songs():
file_url = F"{AZURACAST_URL}api/station/1/files"
response = authorize_url("get", file_url)
data = []
for i in response.json():
i['azura_id'] = i.pop('song_id')
data.append(i)
return data
@staticmethod
def add_to_playlist(azura_id, data):
file_url = f"{AZURACAST_URL}api/station/it-radio/file/{azura_id}"
response = authorize_url("get", file_url)
file_play = f"{AZURACAST_URL}api/station/it-radio/file/{response.json()['unique_id']}/play"
data.update(unique_id=file_play)
return data
@staticmethod
def delete_song(azura_id):
file_url = f"{AZURACAST_URL}api/station/it-radio/file/{azura_id}"
response = authorize_url("delete", file_url)
return response
@staticmethod
def get_audio(song_obj, unique_id):
file_url = f"{AZURACAST_URL}/api/station/it-radio/file/{unique_id}/play"
response = authorize_url("get", file_url)
file_response = HttpResponse(response.content, content_type='audio/mpeg')
file = f'{song_obj.title}-{song_obj.artist}.mp3'
file_response['Content-Disposition'] = f'attachment; filename={file}'
return file_response
@staticmethod
def get_unique_id(azura_id):
file_url = f"{AZURACAST_URL}api/station/it-radio/file/{azura_id}"
response = authorize_url("get", file_url)
return response.json()['unique_id']