Skip to content

Commit

Permalink
fix: screensaver bugs, move controller routes to own file
Browse files Browse the repository at this point in the history
  • Loading branch information
vicwomg committed Jan 9, 2025
1 parent 095d996 commit 9d9bd12
Show file tree
Hide file tree
Showing 5 changed files with 146 additions and 133 deletions.
93 changes: 4 additions & 89 deletions pikaraoke/app.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,9 @@
import sys

import flask_babel
from flask import Flask, redirect, request, session, url_for
from flask import Flask, request, session
from flask_babel import Babel
from flask_socketio import SocketIO, emit
from flask_socketio import SocketIO

from pikaraoke import karaoke
from pikaraoke.constants import LANGUAGES
Expand All @@ -23,6 +23,7 @@
from pikaraoke.lib.selenium import launch_splash_screen
from pikaraoke.routes.admin import admin_bp
from pikaraoke.routes.background_music import background_music_bp
from pikaraoke.routes.controller import controller_bp
from pikaraoke.routes.files import files_bp
from pikaraoke.routes.home import home_bp
from pikaraoke.routes.images import images_bp
Expand Down Expand Up @@ -64,11 +65,7 @@
app.register_blueprint(search_bp)
app.register_blueprint(info_bp)
app.register_blueprint(splash_bp)


def broadcast_event(event, data=None):
print("Broadcasting event: " + event)
emit(event, data, namespace="/", broadcast=True)
app.register_blueprint(controller_bp)


@babel.localeselector
Expand Down Expand Up @@ -115,88 +112,6 @@ def nowplaying():
return ""


# Call this after receiving a command in the front end
@app.route("/clear_command")
def clear_command():
k = get_karaoke_instance()
k.now_playing_command = None
return ""


@app.route("/skip")
def skip():
k = get_karaoke_instance()
broadcast_event("skip")
k.skip()
return redirect(url_for("home.home"))


@app.route("/pause")
def pause():
k = get_karaoke_instance()
if k.is_paused:
broadcast_event("play")
else:
broadcast_event("pause")
k.pause()
return redirect(url_for("home.home"))


@app.route("/transpose/<semitones>", methods=["GET"])
def transpose(semitones):
k = get_karaoke_instance()
k.transpose_current(int(semitones))
return redirect(url_for("home.home"))


@app.route("/restart")
def restart():
k = get_karaoke_instance()
broadcast_event("restart")
k.restart()
return redirect(url_for("home.home"))


@app.route("/volume/<volume>")
def volume(volume):
k = get_karaoke_instance()
broadcast_event("volume", volume)
k.volume_change(float(volume))
return redirect(url_for("home.home"))


@app.route("/vol_up")
def vol_up():
k = get_karaoke_instance()
broadcast_event("volume", "up")
k.vol_up()
return redirect(url_for("home.home"))


@app.route("/vol_down")
def vol_down():
k = get_karaoke_instance()
broadcast_event("volume", "down")
k.vol_down()
return redirect(url_for("home.home"))


@app.route("/end_song", methods=["GET", "POST"])
def end_song():
k = get_karaoke_instance()
d = request.form.to_dict()
reason = d["reason"] if "reason" in d else None
k.end_song(reason)
return "ok"


@app.route("/start_song", methods=["GET"])
def start_song():
k = get_karaoke_instance()
k.start_song()
return "ok"


def main():
platform = get_platform()

Expand Down
6 changes: 6 additions & 0 deletions pikaraoke/lib/current_app.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import time

from flask import current_app, request
from flask_socketio import emit

from pikaraoke.karaoke import Karaoke

Expand Down Expand Up @@ -50,6 +51,11 @@ def get_site_name() -> str:
return current_app.config["SITE_NAME"]


def broadcast_event(event, data=None):
print("Broadcasting event: " + event)
emit(event, data, namespace="/", broadcast=True)


def delayed_halt(cmd):
time.sleep(1.5)
current_app.k.queue_clear()
Expand Down
84 changes: 84 additions & 0 deletions pikaraoke/routes/controller.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,84 @@
import flask_babel
from flask import Blueprint, redirect, request, url_for

from pikaraoke.lib.current_app import broadcast_event, get_karaoke_instance

_ = flask_babel.gettext


controller_bp = Blueprint("controller", __name__)


@controller_bp.route("/skip")
def skip():
k = get_karaoke_instance()
broadcast_event("skip", "user command")
k.skip()
return redirect(url_for("home.home"))


@controller_bp.route("/pause")
def pause():
k = get_karaoke_instance()
if k.is_paused:
broadcast_event("play")
else:
broadcast_event("pause")
k.pause()
return redirect(url_for("home.home"))


@controller_bp.route("/transpose/<semitones>", methods=["GET"])
def transpose(semitones):
k = get_karaoke_instance()
broadcast_event("skip", "transpose current")
k.transpose_current(int(semitones))
return redirect(url_for("home.home"))


@controller_bp.route("/restart")
def restart():
k = get_karaoke_instance()
broadcast_event("restart")
k.restart()
return redirect(url_for("home.home"))


@controller_bp.route("/volume/<volume>")
def volume(volume):
k = get_karaoke_instance()
broadcast_event("volume", volume)
k.volume_change(float(volume))
return redirect(url_for("home.home"))


@controller_bp.route("/vol_up")
def vol_up():
k = get_karaoke_instance()
broadcast_event("volume", "up")
k.vol_up()
return redirect(url_for("home.home"))


@controller_bp.route("/vol_down")
def vol_down():
k = get_karaoke_instance()
broadcast_event("volume", "down")
k.vol_down()
return redirect(url_for("home.home"))


@controller_bp.route("/end_song", methods=["GET", "POST"])
def end_song():
k = get_karaoke_instance()
d = request.form.to_dict()
reason = d["reason"] if "reason" in d else None
k.end_song(reason)
return "ok"


@controller_bp.route("/start_song", methods=["GET"])
def start_song():
k = get_karaoke_instance()
k.start_song()
return "ok"
8 changes: 7 additions & 1 deletion pikaraoke/routes/queue.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,12 @@
import flask_babel
from flask import Blueprint, flash, redirect, render_template, request, url_for

from pikaraoke.lib.current_app import get_karaoke_instance, get_site_name, is_admin
from pikaraoke.lib.current_app import (
broadcast_event,
get_karaoke_instance,
get_site_name,
is_admin,
)

try:
from urllib.parse import unquote
Expand Down Expand Up @@ -55,6 +60,7 @@ def queue_edit():
k.queue_clear()
# MSG: Message shown after clearing the queue
flash(_("Cleared the queue!"), "is-warning")
broadcast_event("skip", "clear queue")
return redirect(url_for("queue.queue"))
else:
song = request.args["song"]
Expand Down
Loading

0 comments on commit 9d9bd12

Please sign in to comment.