From c7a48d2bd9334f40b3380b54dc0cad3e0d4aefd3 Mon Sep 17 00:00:00 2001 From: scoobybejesus <21372487+scoobybejesus@users.noreply.github.com> Date: Sat, 7 Aug 2021 22:33:24 -0400 Subject: [PATCH 1/5] Add websocket route for current track --- ircradio/routes.py | 20 ++++++++++++++++++++ 1 file changed, 20 insertions(+) diff --git a/ircradio/routes.py b/ircradio/routes.py index 0a4fd09..9a56514 100644 --- a/ircradio/routes.py +++ b/ircradio/routes.py @@ -4,6 +4,8 @@ from datetime import datetime from typing import Tuple, Optional from quart import request, render_template, abort, jsonify +import asyncio +import json import settings from ircradio.factory import app @@ -108,3 +110,21 @@ async def user_library(): by_karma = [] return await render_template("library.html", name=name, by_date=by_date, by_karma=by_karma) + + +@app.websocket("/ws") ++async def np(): + while True: + """get current song from history""" + history = Radio.history() + val = "" + if not history: + val = f"Nothing is playing?!" + else: + song = history[0] + val = song.title + + data = json.dumps({"now_playing": val}) + + await websocket.send(f"{data}") + await asyncio.sleep(5) From 9716feab339fde7990d786f734fed5cef1da829b Mon Sep 17 00:00:00 2001 From: scoobybejesus <21372487+scoobybejesus@users.noreply.github.com> Date: Sat, 7 Aug 2021 22:36:47 -0400 Subject: [PATCH 2/5] Script to open websocket for #now_playing updates --- ircradio/templates/base.html | 22 +++++++++++++++++++++- 1 file changed, 21 insertions(+), 1 deletion(-) diff --git a/ircradio/templates/base.html b/ircradio/templates/base.html index 7c4cd48..e13f44e 100644 --- a/ircradio/templates/base.html +++ b/ircradio/templates/base.html @@ -30,9 +30,29 @@ IRC!Radio + + + {% block content %} {% endblock %} - \ No newline at end of file + From e69229eefee11557a65c4d1f6685646e2c39ca8c Mon Sep 17 00:00:00 2001 From: scoobybejesus <21372487+scoobybejesus@users.noreply.github.com> Date: Sat, 7 Aug 2021 22:39:11 -0400 Subject: [PATCH 3/5] Adds #now_playing, as updated by websocket script --- ircradio/templates/index.html | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/ircradio/templates/index.html b/ircradio/templates/index.html index bfce5c8..faa94f0 100644 --- a/ircradio/templates/index.html +++ b/ircradio/templates/index.html @@ -12,6 +12,9 @@

Enjoy the music :)


+

+
Now playing:
+
Nothing here yet

Command list:

@@ -62,4 +65,4 @@ -{% endblock %} \ No newline at end of file +{% endblock %} From c43ad4bffd6889df4bfe52b1effd94b52e2da840 Mon Sep 17 00:00:00 2001 From: scoobybejesus <21372487+scoobybejesus@users.noreply.github.com> Date: Sat, 7 Aug 2021 22:50:34 -0400 Subject: [PATCH 4/5] Nginx changes for websocket --- ircradio/templates/nginx.jinja2 | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/ircradio/templates/nginx.jinja2 b/ircradio/templates/nginx.jinja2 index 7f3e8b8..5eea65c 100644 --- a/ircradio/templates/nginx.jinja2 +++ b/ircradio/templates/nginx.jinja2 @@ -45,4 +45,12 @@ server { proxy_set_header X-Forwarded-For $remote_addr; proxy_set_header X-Forwarded-Proto $scheme; } + + location /ws { + proxy_pass http://{{ host }}:{{ port }}/ws; + proxy_http_version 1.1; + proxy_set_header Upgrade $http_upgrade; + proxy_set_header Connection "Upgrade"; + proxy_set_header Host $host; + } } From 5e966b872c8db4d848cd0009c7ba02fc2ce78744 Mon Sep 17 00:00:00 2001 From: scoobybejesus <21372487+scoobybejesus@users.noreply.github.com> Date: Sun, 8 Aug 2021 13:00:30 -0400 Subject: [PATCH 5/5] Websocket only sends new track (or updated name) --- ircradio/routes.py | 9 ++++++--- 1 file changed, 6 insertions(+), 3 deletions(-) diff --git a/ircradio/routes.py b/ircradio/routes.py index 9a56514..eadaa80 100644 --- a/ircradio/routes.py +++ b/ircradio/routes.py @@ -113,7 +113,8 @@ async def user_library(): @app.websocket("/ws") -+async def np(): +async def np(): + last_song = "" while True: """get current song from history""" history = Radio.history() @@ -124,7 +125,9 @@ async def user_library(): song = history[0] val = song.title - data = json.dumps({"now_playing": val}) + if val != last_song: + data = json.dumps({"now_playing": val}) + await websocket.send(f"{data}") - await websocket.send(f"{data}") + last_song = val await asyncio.sleep(5)