gukgjzkgjhgjh
This commit is contained in:
66
backend/thirdparty/downloader/views.py
vendored
66
backend/thirdparty/downloader/views.py
vendored
@@ -9,6 +9,7 @@ import mimetypes
|
||||
import base64
|
||||
import urllib.request
|
||||
import zipfile
|
||||
import time
|
||||
import requests
|
||||
from urllib.parse import urlparse
|
||||
|
||||
@@ -450,6 +451,11 @@ class Downloader(APIView):
|
||||
{"key": "FFmpegVideoRemuxer", "preferedformat": ext}
|
||||
)
|
||||
|
||||
_platform = (info.get('extractor') or urlparse(url).netloc).lower().replace('www.', '').split(':')[0]
|
||||
_user = request.user if request.user.is_authenticated else None
|
||||
_title = info.get('title', '')
|
||||
start_time = time.time()
|
||||
|
||||
try:
|
||||
if is_playlist:
|
||||
# Handle playlist - create ZIP file
|
||||
@@ -490,9 +496,16 @@ class Downloader(APIView):
|
||||
|
||||
DownloaderRecord.objects.create(
|
||||
url=url,
|
||||
format="zip",
|
||||
length_of_media=total_duration,
|
||||
file_size=total_size,
|
||||
title=_title,
|
||||
platform=_platform,
|
||||
user=_user,
|
||||
format='zip',
|
||||
is_audio_only=bool(extract_audio),
|
||||
video_quality=video_quality,
|
||||
length_of_media=total_duration or None,
|
||||
file_size=total_size or None,
|
||||
processing_time=round(time.time() - start_time, 3),
|
||||
success=True,
|
||||
)
|
||||
|
||||
# Streaming response for ZIP
|
||||
@@ -535,9 +548,16 @@ class Downloader(APIView):
|
||||
|
||||
DownloaderRecord.objects.create(
|
||||
url=url,
|
||||
title=_title,
|
||||
platform=_platform,
|
||||
user=_user,
|
||||
format=ext,
|
||||
length_of_media=duration,
|
||||
file_size=size,
|
||||
is_audio_only=bool(extract_audio),
|
||||
video_quality=video_quality,
|
||||
length_of_media=duration or None,
|
||||
file_size=size or None,
|
||||
processing_time=round(time.time() - start_time, 3),
|
||||
success=True,
|
||||
)
|
||||
|
||||
# Streaming generator that deletes file & temp dir after send
|
||||
@@ -572,6 +592,18 @@ class Downloader(APIView):
|
||||
|
||||
except Exception as e:
|
||||
shutil.rmtree(tmpdir, ignore_errors=True)
|
||||
DownloaderRecord.objects.create(
|
||||
url=url,
|
||||
title=_title,
|
||||
platform=_platform,
|
||||
user=_user,
|
||||
format=ext if not is_playlist else 'zip',
|
||||
is_audio_only=bool(extract_audio),
|
||||
video_quality=video_quality,
|
||||
processing_time=round(time.time() - start_time, 3),
|
||||
success=False,
|
||||
error_message=str(e),
|
||||
)
|
||||
return Response({"error": str(e)}, status=400)
|
||||
|
||||
|
||||
@@ -581,7 +613,6 @@ class Downloader(APIView):
|
||||
# ---------------- STATS FOR GRAPHS ----------------
|
||||
|
||||
from .serializers import DownloaderStatsSerializer
|
||||
from django.db.models import Count, Avg, Sum
|
||||
|
||||
class DownloaderStats(APIView):
|
||||
"""
|
||||
@@ -589,30 +620,11 @@ class DownloaderStats(APIView):
|
||||
"""
|
||||
authentication_classes = []
|
||||
permission_classes = [AllowAny]
|
||||
|
||||
@extend_schema(
|
||||
tags=["downloader", "public"],
|
||||
summary="Get aggregated downloader statistics",
|
||||
responses={200: DownloaderStatsSerializer},
|
||||
)
|
||||
def get(self, request):
|
||||
# agregace číselných polí
|
||||
agg = DownloaderRecord.objects.aggregate(
|
||||
total_downloads=Count("id"),
|
||||
avg_length_of_media=Avg("length_of_media"),
|
||||
avg_file_size=Avg("file_size"),
|
||||
total_length_of_media=Sum("length_of_media"),
|
||||
total_file_size=Sum("file_size"),
|
||||
)
|
||||
|
||||
# zjištění nejčastějšího formátu
|
||||
most_common = (
|
||||
DownloaderRecord.objects.values("format")
|
||||
.annotate(count=Count("id"))
|
||||
.order_by("-count")
|
||||
.first()
|
||||
)
|
||||
|
||||
agg["most_common_format"] = most_common["format"] if most_common else None
|
||||
|
||||
serializer = DownloaderStatsSerializer(agg)
|
||||
return Response(serializer.data)
|
||||
return Response(DownloaderStatsSerializer(DownloaderRecord.objects.all()).data)
|
||||
Reference in New Issue
Block a user