188 lines
9.9 KiB
Python
188 lines
9.9 KiB
Python
#!/usr/bin/env python
|
|
import subprocess
|
|
import socket
|
|
import sys
|
|
import getopt
|
|
import signal
|
|
import json
|
|
import datetime
|
|
|
|
from http.server import HTTPServer, BaseHTTPRequestHandler
|
|
from urllib.parse import urlparse
|
|
|
|
class SimpleHTTPRequestHandler(BaseHTTPRequestHandler):
|
|
|
|
def do_GET(self):
|
|
mpvpause = "?mpv=pause"
|
|
mpvurl = "url"
|
|
i = self.path.index ( "?" )
|
|
apirq = self.path[i:]
|
|
if apirq.startswith("?mpv"):
|
|
arg=apirq.split("=",1)[1]
|
|
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)
|
|
|
|
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("?help"):
|
|
response="/?help show this help page" + "\n" + "/?status show status page" + "\n" + "?file=<PATH|URL> play file or URL" + "\n" + "/?add=<PATH|URL> add file or path to playlist" + "\n" + "/?mpv=playnext play next track" + "\n" + "/?mpv=playprec play previous track" + "\n" + "/?mpv=pause pause player" + "\n" + "/?mpv=volset=50 set volume. [0-100]" + "\n" + "/?mpv=volplus increase volume" + "\n" + "/?mpv=volminus decrease volume"
|
|
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.end_headers()
|
|
self.wfile.write(response.encode("utf-8"))
|
|
value=0
|
|
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(('', 80), SimpleHTTPRequestHandler)
|
|
httpd.serve_forever()
|