webserver.py aktualisiert
This commit is contained in:
parent
27874e57f6
commit
d8af8e398b
1 changed files with 154 additions and 16 deletions
170
webserver.py
170
webserver.py
|
|
@ -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":
|
||||||
mpvurl = subprocess.Popen(cmd,shell=True,stdout=subprocess.PIPE,stderr=subprocess.STDOUT)
|
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)
|
||||||
|
|
||||||
|
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.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")
|
|
||||||
self.end_headers()
|
|
||||||
|
|
||||||
elif apirq.startswith("?url"):
|
elif apirq.startswith("?add"):
|
||||||
music=apirq.split("=",1)[1]
|
arg=apirq.split("=",1)[1]
|
||||||
cmdmusic = "echo '{\"command\":[\"loadfile\", \"" + music + "\"]}' | socat - /tmp/mpvsocket"
|
cmdurl = "echo '{\"command\":[\"loadfile\", \"" + arg + "\", \"append-play\"]}' | socat - /tmp/mpvsocket"
|
||||||
cmd = cmdmusic
|
cmd = cmdurl
|
||||||
mpvurl = subprocess.Popen(cmd,shell=True,stdout=subprocess.PIPE,stderr=subprocess.STDOUT)
|
mpvurl = subprocess.Popen(cmd,shell=True,stdout=subprocess.PIPE,stderr=subprocess.STDOUT)
|
||||||
print(music)
|
response = "Add File To Playlist: " + arg
|
||||||
print(cmdmusic)
|
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'Playing String', )
|
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
|
||||||
|
self.wfile.write(response.encode("utf-8"))
|
||||||
|
self.wfile.write(b"\n")
|
||||||
|
value = value + 1
|
||||||
|
|
||||||
httpd = HTTPServer(('', 8000), SimpleHTTPRequestHandler)
|
httpd = HTTPServer(('', 8000), SimpleHTTPRequestHandler)
|
||||||
httpd.serve_forever()
|
httpd.serve_forever()
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue