102 lines
2.9 KiB
Python
102 lines
2.9 KiB
Python
import logging
|
|
|
|
from flask import Blueprint, g, jsonify, request
|
|
from sqlalchemy.exc import SQLAlchemyError
|
|
|
|
from app.database import get_db_session
|
|
from app.models import SavedQueue, WatchHistory
|
|
from app.mpv import send_to_mpv
|
|
|
|
bp = Blueprint("views", __name__)
|
|
|
|
|
|
@bp.before_request
|
|
def before_request():
|
|
g.db_session = get_db_session()
|
|
|
|
|
|
@bp.teardown_app_request
|
|
def shutdown_session(exception=None):
|
|
if hasattr(g, "db_session"):
|
|
g.db_session.close()
|
|
|
|
|
|
@bp.route("/save_queue", methods=["POST"])
|
|
def save_queue():
|
|
data = request.get_json()
|
|
if not data or "urls" not in data:
|
|
return jsonify(message="Invalid JSON data"), 400
|
|
|
|
db_session = g.db_session
|
|
db_session.query(SavedQueue).delete()
|
|
|
|
for url in data["urls"]:
|
|
new_entry = SavedQueue(video_url=url)
|
|
db_session.add(new_entry)
|
|
|
|
try:
|
|
db_session.commit()
|
|
return jsonify(message="Data added to saved queue"), 200
|
|
except SQLAlchemyError as e:
|
|
db_session.rollback()
|
|
logging.error(f"Failed to insert data: {e}")
|
|
return jsonify(message="Database error"), 500
|
|
|
|
|
|
@bp.route("/load_queue", methods=["GET"])
|
|
def load_queue():
|
|
"""
|
|
Retrieves the saved queue of video URLs.
|
|
"""
|
|
logging.debug("Loading saved queue")
|
|
db_session = g.db_session
|
|
urls = [entry.video_url for entry in db_session.query(SavedQueue).all()]
|
|
logging.debug(f"Loaded {len(urls)} URLs from the saved queue")
|
|
return jsonify(urls), 200
|
|
|
|
|
|
@bp.route("/add_video", methods=["POST"])
|
|
def add_video():
|
|
data = request.get_json()
|
|
if not all(
|
|
k in data for k in ["video_url", "video_name", "channel_url", "channel_name"]
|
|
):
|
|
return jsonify(message="Missing required fields"), 400
|
|
|
|
new_entry = WatchHistory(
|
|
video_url=data["video_url"],
|
|
video_name=data["video_name"],
|
|
channel_url=data["channel_url"],
|
|
channel_name=data["channel_name"],
|
|
)
|
|
|
|
db_session = g.db_session
|
|
db_session.add(new_entry)
|
|
|
|
try:
|
|
db_session.commit()
|
|
logging.debug("Video added to watch history")
|
|
logging.debug(f"URL: {data['video_url']}")
|
|
logging.debug(f"Video name: {data['video_name']}")
|
|
logging.debug(f"Channel URL: {data['channel_url']}")
|
|
logging.debug(f"Channel name: {data['channel_name']}")
|
|
return jsonify(message="Video added"), 200
|
|
except SQLAlchemyError as e:
|
|
db_session.rollback()
|
|
logging.error(f"Database error: {e}")
|
|
return jsonify(message="Failed to add video"), 500
|
|
|
|
|
|
@bp.route("/", methods=["GET"])
|
|
def handle_request():
|
|
video_url = request.args.get("url")
|
|
if not video_url:
|
|
return "Missing 'url' parameter", 400
|
|
|
|
command = (
|
|
f'{{"command": ["script-message", "add_to_youtube_queue", "{video_url}"]}}\n'
|
|
)
|
|
if send_to_mpv(command):
|
|
return "URL added to mpv queue", 200
|
|
return "Failed to add URL to mpv queue", 500
|