webserver.py aktualisiert

This commit is contained in:
Kaisa Marysia 2024-07-07 15:08:14 +02:00
parent 27874e57f6
commit d8af8e398b

View file

@ -4,6 +4,8 @@ import socket
import sys import sys
import getopt import getopt
import signal import signal
import json
import datetime
from http.server import HTTPServer, BaseHTTPRequestHandler from http.server import HTTPServer, BaseHTTPRequestHandler
from urllib.parse import urlparse from urllib.parse import urlparse
@ -16,30 +18,166 @@ class SimpleHTTPRequestHandler(BaseHTTPRequestHandler):
mpvurl = "url" mpvurl = "url"
i = self.path.index ( "?" ) i = self.path.index ( "?" )
apirq = self.path[i:] apirq = self.path[i:]
print(apirq) if apirq.startswith("?mpv"):
if apirq == mpvpause: arg=apirq.split("=",1)[1]
cmd = "echo 'cycle pause' | socat - /tmp/mpvsocket" if arg == "pause":
outputcmd = "echo \'{ \"command\": [\"get_property\", \"pause\"] }\' | socat - /tmp/mpvsocket"
output = subprocess.check_output(outputcmd,shell=True)
outputjson = json.loads(output)
outputjson = str(outputjson["data"])
if outputjson == "False":
cmd = "echo '{\"command\":[\"set_property\",\"pause\",true]}' | socat - /tmp/mpvsocket"
mpvurl = subprocess.Popen(cmd,shell=True,stdout=subprocess.PIPE,stderr=subprocess.STDOUT) mpvurl = subprocess.Popen(cmd,shell=True,stdout=subprocess.PIPE,stderr=subprocess.STDOUT)
elif outputjson == "True":
cmdurl = "echo '{ \"command\": [\"set_property\", \"pause\", false]}' | socat - /tmp/mpvsocket"
cmd = cmdurl
mpvurl = subprocess.Popen(cmd,shell=True,stdout=subprocess.PIPE,stderr=subprocess.STDOUT)
elif arg == "volume":
outputcmd = "echo '{ \"command\": [\"get_property_string\", \"volume\"] }' | socat - /tmp/mpvsocket"
output = subprocess.check_output(outputcmd,shell=True)
outputjson = json.loads(output)
outputjson = str(outputjson["data"])
response = "Volume is: " + outputjson
self.send_response(200)
self.end_headers()
self.wfile.write(response.encode("utf-8"))
elif arg == "volminus":
outputcmd = "echo '{ \"command\": [\"get_property_string\", \"volume\"] }' | socat - /tmp/mpvsocket"
output = subprocess.check_output(outputcmd,shell=True)
outputjson = json.loads(output)
outputjson = int(float(outputjson["data"]))
volminus = outputjson-10
arg = str(volminus)
cmdurl = "echo '{\"command\":[\"set_property_string\", \"volume\"," + arg +"]}' | socat - /tmp/mpvsocket"
cmd = cmdurl
mpvurl = subprocess.Popen(cmd,shell=True)
response = "Volume is: " + arg
self.send_response(200)
self.end_headers()
self.wfile.write(response.encode("utf-8"))
elif arg == "volplus":
outputcmd = "echo '{ \"command\": [\"get_property_string\", \"volume\"] }' | socat - /tmp/mpvsocket"
output = subprocess.check_output(outputcmd,shell=True)
outputjson = json.loads(output)
outputjson = int(float(outputjson["data"]))
volplus = outputjson+10
arg = str(volplus)
cmdurl = "echo '{\"command\":[\"set_property_string\", \"volume\"," + arg +"]}' | socat - /tmp/mpvsocket"
cmd = cmdurl
mpvurl = subprocess.Popen(cmd,shell=True)
response = "Volume is: " + arg
self.send_response(200)
self.end_headers()
self.wfile.write(response.encode("utf-8"))
elif arg.startswith("volset"):
arg=arg.split("=",1)[1]
outputcmd = "echo '{ \"command\": [\"get_property_string\", \"volume\"] }' | socat - /tmp/mpvsocket"
output = subprocess.check_output(outputcmd,shell=True)
outputjson = json.loads(output)
outputjson = int(float(outputjson["data"]))
cmdurl = "echo '{\"command\":[\"set_property_string\", \"volume\"," + arg +"]}' | socat - /tmp/mpvsocket"
cmd = cmdurl
mpvurl = subprocess.Popen(cmd,shell=True)
response = "Volume is: " + arg
self.send_response(200)
self.end_headers()
self.wfile.write(response.encode("utf-8"))
elif arg == "playnext":
cmd = "echo 'playlist-next weak' | socat - /tmp/mpvsocket"
mpvurl = subprocess.Popen(cmd,shell=True)
response = "Playing Next Track"
self.send_response(200)
self.end_headers()
self.wfile.write(response.encode("utf-8"))
elif arg == "playprev":
cmd = "echo 'playlist-prev weak' | socat - /tmp/mpvsocket"
mpvurl = subprocess.Popen(cmd,shell=True)
response = "Playing Previous Track"
self.send_response(200)
self.end_headers()
self.wfile.write(response.encode("utf-8"))
elif apirq.startswith("?file"):
arg=apirq.split("=",1)[1]
cmdurl = "echo '{\"command\":[\"loadfile\", \"" + arg + "\"]}' | socat - /tmp/mpvsocket"
cmd = cmdurl
mpvurl = subprocess.Popen(cmd,shell=True,stdout=subprocess.PIPE,stderr=subprocess.STDOUT)
response = "Now Playing: " + arg
self.send_response(200)
self.end_headers()
self.wfile.write(response.encode("utf-8"))
elif apirq.startswith("?add"):
arg=apirq.split("=",1)[1]
cmdurl = "echo '{\"command\":[\"loadfile\", \"" + arg + "\", \"append-play\"]}' | socat - /tmp/mpvsocket"
cmd = cmdurl
mpvurl = subprocess.Popen(cmd,shell=True,stdout=subprocess.PIPE,stderr=subprocess.STDOUT)
response = "Add File To Playlist: " + arg
self.send_response(200)
self.end_headers()
self.wfile.write(response.encode("utf-8"))
elif apirq.startswith("?status"):
cmdtitle = "echo '{\"command\":[\"get_property\", \"media-title\"]}' | socat - /tmp/mpvsocket"
outputtitle = subprocess.check_output(cmdtitle,shell=True)
outputjsontitle = json.loads(outputtitle)
outputjsontitle = str(outputjsontitle["data"])
cmdduration = "echo '{\"command\":[\"get_property\", \"duration\"]}' | socat - /tmp/mpvsocket"
outputduration = subprocess.check_output(cmdduration,shell=True)
outputjsonduration = json.loads(outputduration)
outputjsonduration = int(float(outputjsonduration["data"]))
outputjsonduration = str(datetime.timedelta(seconds=outputjsonduration))
cmdtimepos = "echo '{\"command\":[\"get_property\", \"time-pos\"]}' | socat - /tmp/mpvsocket"
outputtimepos = subprocess.check_output(cmdtimepos,shell=True)
outputjsontimepos = json.loads(outputtimepos)
outputjsontimepos = int(float(outputjsontimepos["data"]))
outputjsontimepos = str(datetime.timedelta(seconds=outputjsontimepos))
cmdvolume = "echo '{\"command\":[\"get_property\", \"volume\"]}' | socat - /tmp/mpvsocket"
outputvolume = subprocess.check_output(cmdvolume,shell=True)
outputjsonvolume = json.loads(outputvolume)
outputjsonvolume = str(outputjsonvolume["data"])
cmdplaylistpos = "echo '{\"command\":[\"get_property\", \"playlist-pos-1\"]}' | socat - /tmp/mpvsocket"
outputplaylistpos = subprocess.check_output(cmdplaylistpos,shell=True)
outputjsonplaylistpos = json.loads(outputplaylistpos)
outputjsonplaylistpos = str(outputjsonplaylistpos["data"])
cmdplaylistcount = "echo '{\"command\":[\"get_property\", \"playlist-count\"]}' | socat - /tmp/mpvsocket"
outputplaylistcount = subprocess.check_output(cmdplaylistcount,shell=True)
outputjsonplaylistcount = json.loads(outputplaylistcount)
outputjsonplaylistcount = str(outputjsonplaylistcount["data"])
cmdplaylistplaylist = "echo '{\"command\":[\"get_property\", \"playlist\"]}' | socat - /tmp/mpvsocket"
outputplaylistplaylist = subprocess.check_output(cmdplaylistplaylist,shell=True)
outputjsonplaylistplaylist = json.loads(outputplaylistplaylist)
irange=int(outputjsonplaylistcount)
outputplaylistlist=[""]
for i in range(0, irange):
outputfullplaylist=outputjsonplaylistplaylist["data"][int(i)]["filename"]
outputplaylistlist.insert(irange, outputfullplaylist)
response = "Now Playing: " + outputjsontitle + "\n" + "Duration: " + outputjsontimepos + "/" + outputjsonduration + "\n" + "Volume: " + outputjsonvolume + "\n" + "Playlist Position: " + outputjsonplaylistpos + "/" + outputjsonplaylistcount + "\n" + "Playlist: "
self.send_response(200) self.send_response(200)
self.end_headers() self.end_headers()
self.wfile.write(b'Paused MPV') self.wfile.write(response.encode("utf-8"))
self.send_header("Content-Type", "text/html") value=0
self.end_headers() for i in outputplaylistlist:
response="[" + str(value) + "]" + i
elif apirq.startswith("?url"): self.wfile.write(response.encode("utf-8"))
music=apirq.split("=",1)[1] self.wfile.write(b"\n")
cmdmusic = "echo '{\"command\":[\"loadfile\", \"" + music + "\"]}' | socat - /tmp/mpvsocket" value = value + 1
cmd = cmdmusic
mpvurl = subprocess.Popen(cmd,shell=True,stdout=subprocess.PIPE,stderr=subprocess.STDOUT)
print(music)
print(cmdmusic)
self.send_response(200)
self.end_headers()
self.wfile.write(b'Playing String', )
self.send_header("Content-Type", "text/html")
self.end_headers()
httpd = HTTPServer(('', 8000), SimpleHTTPRequestHandler) httpd = HTTPServer(('', 8000), SimpleHTTPRequestHandler)
httpd.serve_forever() httpd.serve_forever()