initial commit
This commit is contained in:
commit
c959f35ad0
1
.gitignore
vendored
Normal file
1
.gitignore
vendored
Normal file
@ -0,0 +1 @@
|
||||
.env
|
3
env.example
Normal file
3
env.example
Normal file
@ -0,0 +1,3 @@
|
||||
IP=0.0.0.0 # Listen on all interfaces
|
||||
PORT_NUMBER=8080 # You can change this port if needed
|
||||
MPV_SOCKET=/mpvsocket
|
16
mpv-youtube-queue-server.service
Normal file
16
mpv-youtube-queue-server.service
Normal file
@ -0,0 +1,16 @@
|
||||
[Unit]
|
||||
Description=Python Server for MPV
|
||||
After=network.target
|
||||
|
||||
[Service]
|
||||
User=<USER>
|
||||
WorkingDirectory=<PATH_TO_PYTHON_SCRIPT>
|
||||
ExecStart=<PATH_TO_PYTHON> <PATH_TO_PYTHON_SCRIPT>
|
||||
Restart=on-failure
|
||||
Environment="MPV_SOCKET=/tmp/mpvsocket"
|
||||
Environment="HOST_NAME=0.0.0.0"
|
||||
Environment="PORT_NUMBER=42069"
|
||||
|
||||
[Install]
|
||||
WantedBy=multi-user.target
|
||||
|
98
server.py
Executable file
98
server.py
Executable file
@ -0,0 +1,98 @@
|
||||
#!/usr/bin/env python3
|
||||
import logging
|
||||
import os
|
||||
import socket
|
||||
import time
|
||||
import urllib.parse
|
||||
from http.server import BaseHTTPRequestHandler, HTTPServer
|
||||
|
||||
# Configuration
|
||||
MPV_SOCKET = os.getenv("MPV_SOCKET", "/tmp/mpvsocket")
|
||||
HOST_NAME = os.getenv("HOST_NAME", "0.0.0.0")
|
||||
PORT_NUMBER = int(os.getenv("PORT_NUMBER", "8080"))
|
||||
SOCKET_RETRY_DELAY = 5 # Time in seconds between retries to connect to the socket
|
||||
|
||||
# Set up basic logging
|
||||
logging.basicConfig(
|
||||
level=logging.INFO, format="%(asctime)s - %(levelname)s - %(message)s"
|
||||
)
|
||||
|
||||
|
||||
def send_to_mpv(command):
|
||||
"""Send a command to the mpv socket, retrying if the socket is not available."""
|
||||
while True:
|
||||
try:
|
||||
# Check if the socket exists
|
||||
if not os.path.exists(MPV_SOCKET):
|
||||
logging.warning(
|
||||
f"Socket {MPV_SOCKET} not found, retrying in {SOCKET_RETRY_DELAY} seconds..."
|
||||
)
|
||||
time.sleep(SOCKET_RETRY_DELAY)
|
||||
continue
|
||||
|
||||
# Send command to mpv socket
|
||||
with socket.socket(socket.AF_UNIX, socket.SOCK_STREAM) as client_socket:
|
||||
client_socket.connect(MPV_SOCKET)
|
||||
client_socket.sendall(command.encode("utf-8"))
|
||||
logging.info("Command sent to mpv successfully.")
|
||||
return True
|
||||
except socket.error as e:
|
||||
logging.error(
|
||||
f"Failed to connect to socket: {e}. Retrying in {SOCKET_RETRY_DELAY} seconds..."
|
||||
)
|
||||
time.sleep(SOCKET_RETRY_DELAY)
|
||||
|
||||
|
||||
class MyHandler(BaseHTTPRequestHandler):
|
||||
def do_GET(self):
|
||||
# Parse the URL and extract the "url" parameter
|
||||
query_components = urllib.parse.parse_qs(urllib.parse.urlparse(self.path).query)
|
||||
video_url = query_components.get("url", [None])[0]
|
||||
|
||||
if video_url:
|
||||
# Decode the URL to handle any special characters
|
||||
video_url = urllib.parse.unquote(video_url)
|
||||
|
||||
# Log the received URL
|
||||
logging.info(f"Received URL: {video_url}")
|
||||
|
||||
# Create the command to send to mpv
|
||||
command = f'{{"command": ["script-message", "add_to_youtube_queue", "{video_url}"]}}\n'
|
||||
|
||||
# Try to send the command to mpv
|
||||
if send_to_mpv(command):
|
||||
self.send_response(200)
|
||||
self.end_headers()
|
||||
self.wfile.write(b"URL added to mpv queue")
|
||||
else:
|
||||
self.send_response(500)
|
||||
self.end_headers()
|
||||
self.wfile.write(b"Failed to add URL to mpv queue")
|
||||
else:
|
||||
# Log the error
|
||||
logging.error("Missing 'url' parameter")
|
||||
self.send_response(400)
|
||||
self.end_headers()
|
||||
self.wfile.write(b"Missing 'url' parameter")
|
||||
|
||||
def log_message(self, format, *args):
|
||||
# Override default log_message method to avoid duplicate logging from BaseHTTPRequestHandler
|
||||
logging.info(f"{self.address_string()} - {format % args}")
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
# Log server start
|
||||
logging.info(f"Starting server on {HOST_NAME}:{PORT_NUMBER}...")
|
||||
|
||||
try:
|
||||
httpd = HTTPServer((HOST_NAME, PORT_NUMBER), MyHandler)
|
||||
logging.info(f"Server running on port {PORT_NUMBER}...")
|
||||
httpd.serve_forever()
|
||||
except Exception as e:
|
||||
# Log any exceptions that occur during server operation
|
||||
logging.exception(f"Error occurred: {e}")
|
||||
except KeyboardInterrupt:
|
||||
# Log server shutdown
|
||||
logging.info("Server is shutting down...")
|
||||
httpd.server_close()
|
||||
logging.info("Server stopped.")
|
Loading…
Reference in New Issue
Block a user