большоооооооооооооооооооой рефакторинг

This commit is contained in:
Mike0001-droid 2024-06-13 16:26:12 +05:00
parent 4348f184eb
commit 2572bdb4ff
5 changed files with 33 additions and 3 deletions

1
.gitignore vendored
View File

@ -17,3 +17,4 @@ server/proj/conf/settings/__init__.py
__pycache__
media/
env/
db.sqlite3

View File

@ -10,7 +10,7 @@ from rest_framework.permissions import AllowAny
class FetchAndServeFile(ViewSet):
#http://82.97.242.49:10084/api/station/it-radio/file/5196269acd9d54e86297557a6d9a7aab/
def list(self, request):
file_url = f"http://82.97.242.49:10084/api/station/it-radio/file/fc6377b2dae75d29358783bc/play"
file_url = f"http://82.97.242.49:10084/api/station/it-radio/file/fc6377b2dae75d29358783bc"
api_key = "49226d3488aac3f5:18d88659c6c1c5e131a0ce0a94d55235"
headers = {
@ -19,8 +19,9 @@ class FetchAndServeFile(ViewSet):
try:
response = requests.get(file_url, headers=headers)
response.raise_for_status()
response.raise_for_status()
print(response.content)
# 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"'

View File

@ -7,6 +7,7 @@ from django.core.exceptions import ObjectDoesNotExist, MultipleObjectsReturned
from django.shortcuts import get_object_or_404, get_list_or_404
from rest_framework.permissions import IsAuthenticated
import requests
from django.http import HttpResponse
from .schemas import SongSchema, DeleteSongSchema
from .models import Song, FavoriteSong
@ -15,7 +16,7 @@ from .serializers import SongSerializer, FavoriteSongSerializer
class SongViewSet(GenericViewSet):
queryset = Song.objects.all()
serializer_class = SongSerializer
permission_classes = (IsAuthenticated,)
#permission_classes = (IsAuthenticated,)
def list(self, request):
songs_pk = FavoriteSong.objects.filter(user=request.user, song__isnull=False).values_list('song_id', flat=True)
@ -39,6 +40,33 @@ class SongViewSet(GenericViewSet):
return Response(serializer.data)
@action(
detail=False,
methods=['get'],
url_path='get_audio/(?P<azura_id>[a-zA-Z0-9_]+)',
url_name='get_audio',
)
def get_audio(self, request, azura_id):
try:
song_obj = Song.objects.get(azura_id=azura_id)
file_url = f"http://82.97.242.49:10084/api/station/it-radio/file/{song_obj.azura_id}/play"
API_KEY = "49226d3488aac3f5:18d88659c6c1c5e131a0ce0a94d55235"
headers = {
"Authorization": f"Bearer {API_KEY}"
}
response = requests.get(file_url, headers=headers)
response.raise_for_status()
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
except ObjectDoesNotExist:
return Response({"error": 'Объекта не существует'}, status=status.HTTP_404_NOT_FOUND)
@action(detail=False, methods=['post'], schema=SongSchema())
def add_favorite(self, request):
data = None