ITRadio/server/proj/api/views.py

52 lines
2.1 KiB
Python

import requests
from django.http import HttpResponse
from rest_framework.views import APIView
from rest_framework.viewsets import ViewSet
from rest_framework.response import Response
from rest_framework import status
from rest_framework.decorators import action
from rest_framework.permissions import AllowAny
class FetchAndServeFile(ViewSet):
def list(self, request):
file_url = f"http://82.97.242.49:10084/api/station/it-radio/file/fc6377b2dae75d29358783bc/play"
api_key = "49226d3488aac3f5:18d88659c6c1c5e131a0ce0a94d55235"
headers = {
"Authorization": f"Bearer {api_key}"
}
try:
response = requests.get(file_url, headers=headers)
response.raise_for_status()
# Create a response with the appropriate content type and headers
file_response = HttpResponse(response.content, content_type='audio/mpeg')
file_response['Content-Disposition'] = 'attachment; filename="output.mp3"'
return Response(f'{file_response['Content-Disposition']}', status=status.HTTP_200_OK)
except requests.RequestException as e:
return Response({'error': str(e)}, status=status.HTTP_400_BAD_REQUEST)
""" def get(self, request, station, song_id, *args, **kwargs):
file_url = f"http://82.97.242.49:10084/api/station/{station}/file/{song_id}/play"
api_key = "49226d3488aac3f5:18d88659c6c1c5e131a0ce0a94d55235"
headers = {
"Authorization": f"Bearer {api_key}"
}
try:
response = requests.get(file_url, headers=headers)
response.raise_for_status()
# Create a response with the appropriate content type and headers
file_response = HttpResponse(response.content, content_type='audio/mpeg')
file_response['Content-Disposition'] = 'attachment; filename="output.mp3"'
return file_response
except requests.RequestException as e:
return Response({'error': str(e)}, status=status.HTTP_400_BAD_REQUEST) """