add save/load queue routes
All checks were successful
Build Docker Image / build (push) Successful in 57s

This commit is contained in:
sudacode 2024-09-10 01:04:22 -07:00
parent 81317c4f30
commit 410a7c92d9
2 changed files with 41 additions and 1 deletions

View File

@ -1 +1 @@
0.1.1
0.1.2

View File

@ -62,6 +62,16 @@ class WatchHistory(Base):
)
class SavedQueue(Base):
__tablename__ = "saved_queue"
sqid = Column(Integer, primary_key=True, autoincrement=True)
video_url = Column(String(255), nullable=False)
created_date = Column(DateTime, nullable=False, server_default=func.now())
created_by = Column(
String(100), nullable=False, server_default="mpv-youtube-queue-server"
)
# Ensure tables exist
Base.metadata.create_all(engine)
@ -87,6 +97,36 @@ def send_to_mpv(command):
return False
@app.route("/save_queue", methods=["POST"])
def save_queue():
data = request.get_json()
if data is None or "urls" not in data:
logging.error("Invalid JSON data")
return jsonify(message="Invalid JSON data"), 400
logging.debug(f"Received data: {data}")
urls = data.get("urls")
logging.debug("Truncating saved queue")
session.query(SavedQueue).delete()
for url in urls:
logging.debug(f"Adding {url} to the saved queue")
new_entry = SavedQueue(video_url=url)
try:
session.add(new_entry)
except exc.SQLAlchemyError as e:
logging.error(f"Failed to insert data into database: {e}")
return jsonify(message="Failed to add data to database"), 500
session.commit()
return jsonify(message="Data added to saved queue"), 200
@app.route("/load_queue", methods=["GET"])
def load_queue():
logging.debug("Loading saved queue")
urls = [entry.video_url for entry in session.query(SavedQueue).all()]
logging.debug(f"Loaded {len(urls)} URLs from the saved queue")
return jsonify(urls), 200
@app.route("/add_video", methods=["POST"])
def add_video():
data = request.get_json()