def parsestring(string: str, name: str = None, logtext: bool = True): if logtext: print("Parsing playlist string") if name is None: name = "String playlist " + str(randint(10000, 99999)) sequences = [] for line in string.splitlines(): line = line.strip() if line == "" or line.startswith("#") or line.startswith(";"): continue path = os.path.join(cfg.SEQUENCE_DIR, line) sequences.append(Sequence.parsefile(path)) if len(sequences) == 0: return None return SequencePlaylist(sequences, name)
def do_GET(self): # Construct a server response. try: if not self.check_auth(): return if self.path.endswith(".js") or \ self.path.endswith(".css") or \ self.path.endswith(".ico") or \ self.path.endswith(".png" or \ self.path.endswith(".jpg")): f = self.send_head() if f: self.copyfile(f, self.wfile) f.close() else: if self.path == "/auth" and not self.isuser: self.send_response(401) self.send_header('WWW-Authenticate', 'Basic realm="Authenticate"') self.end_headers() return else: self.send_response(200) self.send_header('Content-type', "text/html") self.end_headers() if self.path == "/get/sequences/html/": for file in os.listdir(cfg.SEQUENCE_DIR): btnclass = "default" if not PLAYER.currentplaylist is None and PLAYER.currentplaylist.name == file: btnclass = "primary" self.wfile.write( bytearray(('<button class="btn btn-' + btnclass + '">' + file + '</button>').encode())) elif self.path == "/get/playlists/html/": for file in os.listdir(cfg.PLAYLIST_DIR): btnclass = "default" if not PLAYER.currentplaylist is None and PLAYER.currentplaylist.name == file: btnclass = "primary" self.wfile.write( bytearray(('<button class="btn btn-' + btnclass + '">' + file + '</button>').encode())) elif self.path == "/get/sequences/": for file in os.listdir(cfg.SEQUENCE_DIR): self.wfile.write(bytearray((file + "\n").encode())) elif self.path == "/get/playlists/": for file in os.listdir(cfg.PLAYLIST_DIR): self.wfile.write(bytearray((file + "\n").encode())) elif self.path == "/get/current/": if PLAYER.currentplaylist is not None: self.wfile.write( bytearray(PLAYER.currentplaylist.name.encode())) else: self.wfile.write(b"") elif self.path == "/stop/": self.wfile.write(b"") PLAYER.stopcurrentplaylist() PLAYER.clear() elif str(self.path).startswith("/set/sequence/"): name = str(self.path)[14:] self.wfile.write(b"") path = os.path.join(cfg.SEQUENCE_DIR, name) try: PLAYER.runsequence(Sequence.parsefile(path)) except FileNotFoundError: print("Sequence '{0}' does not exist!".format(path)) elif str(self.path).startswith("/set/playlist/"): name = str(self.path)[14:] self.wfile.write(b"") path = os.path.join(cfg.PLAYLIST_DIR, name) try: PLAYER.runplaylist(SequencePlaylist.parsefile(path)) except FileNotFoundError: print("Playlist '{0}' does not exist!".format(path)) else: f = open(cfg.HTML_FILE, "r").read() self.wfile.write(self.parsefile(f).encode('utf-8')) except Exception as ex: print("ERROR: {0}".format(ex)) self.send_response(500) self.end_headers() if cfg.VERBOSE_LOGGING: raise ex
os.makedirs(cfg.SEQUENCE_DIR) if not os.path.exists(cfg.PLAYLIST_DIR): os.makedirs(cfg.PLAYLIST_DIR) if cfg.NO_PI: PLAYER = SequencePlayerWindow() else: PLAYER = NeopixelSequencePlayer() if cfg.STARTUP_PLAYLIST: if cfg.VERBOSE_LOGGING: print("Going to run the startup playlist") path = os.path.join(cfg.PLAYLIST_DIR, cfg.STARTUP_PLAYLIST) try: PLAYER.runplaylist(SequencePlaylist.parsefile(path)) except FileNotFoundError: print("Startup playlist '{0}' does not exist!".format(path)) elif cfg.STARTUP_SEQUENCE: if cfg.VERBOSE_LOGGING: print("Going to run the startup sequence") path = os.path.join(cfg.SEQUENCE_DIR, cfg.STARTUP_SEQUENCE) try: PLAYER.runsequence(Sequence.parsefile(path)) except FileNotFoundError: print("Startup sequence '{0}' does not exist!".format(path)) if __name__ == '__main__': print('Server listening on port {0}...'.format(cfg.PORT)) HTTPD = StoppableHTTPServer(('', cfg.PORT), Handler) HTTPD.run()