Files
smart-home/halloween/halloween.py
T
2023-10-30 13:12:47 +01:00

86 lines
2.9 KiB
Python

# Wird von der Türklingel ausgelöst und spield zufällig Geistervideos ab.
# Die letzten beiden gespielten Videos werden ausgeschlossen, um für genug Abwechslung zu sorgen.
import os
import random
from http.server import BaseHTTPRequestHandler, HTTPServer
import time
import subprocess
videos = [
"/home/tilman/Videos/halloween-01-friendly_ghost_1.mp4",
"/home/tilman/Videos/halloween-02-friendly_ghost_2.mp4",
"/home/tilman/Videos/halloween-03-friendly_ghost_3.mp4",
"/home/tilman/Videos/halloween-04-friendly_ghost_4.mp4",
"/home/tilman/Videos/halloween-06-ghosts_pumpkins.mp4",
"/home/tilman/Videos/halloween-07-ghosts_candles.mp4",
"/home/tilman/Videos/halloween-08-marching_band.mp4",
"/home/tilman/Videos/halloween-05-skeletons.mp4",
"/home/tilman/Videos/halloween-09-skulls.mp4"]
hostName = "192.168.178.31"
serverPort = 8080
class Player(object):
_instance = None
# we need a subprocess object for the first poll() so we just let the video run once
vid_process = subprocess.Popen(["mplayer","/home/tilman/Videos/halloween-04-friendly_ghost_4.mp4"], stdout=subprocess.PIPE)
def __new__(cls):
if cls._instance is None:
print('Creating the object')
cls._instance = super(Player, cls).__new__(cls)
# Put any initialization here.
return cls._instance
class MyServer(BaseHTTPRequestHandler):
player = Player()
last_no = 0
last_no2 = 0
def do_GET(self):
self.send_response(200)
self.send_header("Content-type", "text/html")
self.end_headers()
self.wfile.write(bytes("<html><head><title>Halloween</title></head>", "utf-8"))
self.wfile.write(bytes("<p>Request: %s</p>" % self.path, "utf-8"))
self.wfile.write(bytes("<body><p>", "utf-8"))
if (self.player.vid_process.poll() is not None):
rnd_no = random.randint(0,6)
while (rnd_no == self.last_no or rnd_no == self.last_no2):
rnd_no = random.randint(0,6)
self.last_no2 = self.last_no
self.last_no = rnd_no
link = videos[rnd_no]
print('START ' + link + '\n')
self.wfile.write(bytes("START " + videos[rnd_no], "utf-8"))
cmd = "mplayer -xineramascreen 2 -fs %s"%(link)
self.player.vid_process = subprocess.Popen(["mplayer",link], stdout=subprocess.PIPE)
else:
print('RUNNING\n')
self.wfile.write(bytes("RUNNING " + videos[self.last_no], "utf-8"))
self.wfile.write(bytes("</p>", "utf-8"))
self.wfile.write(bytes("</body></html>", "utf-8"))
if __name__ == "__main__":
webServer = HTTPServer((hostName, serverPort), MyServer)
print("Server started http://%s:%s" % (hostName, serverPort))
try:
webServer.serve_forever()
except KeyboardInterrupt:
pass
webServer.server_close()
print("Server stopped.")