From c4859dd4e5391625db80c6c49cf3abddbab999fd Mon Sep 17 00:00:00 2001 From: rudy3333 Date: Fri, 30 May 2025 19:18:24 +0200 Subject: [PATCH] added api to stream music, currently only API. also updated the dockerfile to automatically install ffmpeg (its git ignored) --- eversyncc/urls.py | 1 + eversyncc/views.py | 29 +++++++++++++++++++++++-- index.html | 5 +++++ pomodoro.html | 54 ++++++++++++++++++++++++++++++++-------------- requirements.txt | 3 ++- 5 files changed, 73 insertions(+), 19 deletions(-) diff --git a/eversyncc/urls.py b/eversyncc/urls.py index 73fbf58..3935ce6 100644 --- a/eversyncc/urls.py +++ b/eversyncc/urls.py @@ -49,6 +49,7 @@ urlpatterns = [ path('inbox/', views.inbox, name='inbox'), path('chat', views.chat_page, name="chat"), path('delete//', views.delete_message, name='delete_message'), + path('stream_song/', views.stream_song, name='stream_song'), ] if settings.DEBUG: diff --git a/eversyncc/views.py b/eversyncc/views.py index b2a7c19..753b907 100644 --- a/eversyncc/views.py +++ b/eversyncc/views.py @@ -1,5 +1,5 @@ from django.shortcuts import render, redirect, get_object_or_404 -from django.http import HttpResponse, JsonResponse +from django.http import HttpResponse, JsonResponse, FileResponse from django.contrib.auth.decorators import login_required from django.contrib.auth.forms import UserCreationForm, UserChangeForm, PasswordChangeForm # Create your views here. @@ -19,6 +19,8 @@ from webpush import send_user_notification from icalendar import Calendar, Event as IcalEvent from django.utils.text import slugify import json +import tempfile +from yt_dlp import YoutubeDL providers = micawber.bootstrap_basic() @@ -515,4 +517,27 @@ def delete_message(request, message_id): if request.method == "POST": message = get_object_or_404(Message, id=message_id, sender=request.user) message.delete() - return redirect('chat') \ No newline at end of file + return redirect('chat') + +def stream_song(request): + query = request.GET.get("query") + if not query: + return JsonResponse({"error": "No search query provided"}, status=400) + + with tempfile.TemporaryDirectory() as tmpdir: + ytpdl_options = { + 'format': 'bestaudio[ext=m4a]/bestaudio/best', + 'noplaylist': 'true', + 'outtmpl': f'{tmpdir}/%(title)s.%(ext)s', + 'postprocessors': [{ + 'key': 'FFmpegExtractAudio', + 'preferredcodec': 'mp3', + }], + 'quiet': True + } + + with YoutubeDL(ytpdl_options) as ytpdl: + info = ytpdl.extract_info(f"ytsearch1:{query} audio", download=True) + filepath = ytpdl.prepare_filename(info['entries'][0]).replace('.webm', '.mp3').replace('.m4a', '.mp3') + return FileResponse(open(filepath, 'rb'), content_type='audio/mpeg') + \ No newline at end of file diff --git a/index.html b/index.html index fe612a6..6ac359b 100644 --- a/index.html +++ b/index.html @@ -164,6 +164,11 @@ chat +
+ + music +
+ diff --git a/pomodoro.html b/pomodoro.html index f33e755..1f85104 100644 --- a/pomodoro.html +++ b/pomodoro.html @@ -37,6 +37,12 @@ color: white; cursor: pointer; } + #pomodoro-settings { + margin-top: 20px; + font-size: 1.2rem; + display: none; + text-align: center; + } @@ -45,6 +51,15 @@

25:00

+ + +
+

Settings

+ + +

+ +
@@ -74,21 +89,10 @@
- - - - \ No newline at end of file + + \ No newline at end of file diff --git a/requirements.txt b/requirements.txt index 29fc4ec..2cc7608 100644 --- a/requirements.txt +++ b/requirements.txt @@ -19,4 +19,5 @@ micawber requests django-webpush icalendar -sentry-sdk[django] \ No newline at end of file +sentry-sdk[django] +yt-dlp \ No newline at end of file