import os import subprocess from http.server import HTTPServer, BaseHTTPRequestHandler FILENAME = 'currentRecording.mp3' VLC_PATH = "C:\\Program Files (x86)\\VideoLAN\VLC\\vlc.exe" old_recording = None old_name = None should_save = False def saved_name(name): return "recordings\\" + name.replace(" ", "_") + ".mp3" def generate_vlc_record_cmd(url, filename): return [VLC_PATH, "-vvv", url, "-I", "dummy", "--sout", "#transcode{acodec=mp3,ab=128}:file{dst=" + filename + "}"] def on_new_media(url, name): global old_recording, should_save, old_name if old_recording: print("Finished old recording") old_recording.terminate() old_recording.wait() if should_save: print("Saving old recording") os.rename(FILENAME, saved_name(old_name)) should_save = False old_recording = subprocess.Popen(generate_vlc_record_cmd(url, FILENAME)) old_name = name class SimpleHTTPRequestHandler(BaseHTTPRequestHandler): def do_GET(self): global should_save self.send_response(200) self.end_headers() if self.path == "/": try: stream_url = self.headers['url'] current_name = self.headers['name'] print("Received new media:", stream_url, current_name) on_new_media(stream_url, current_name) except KeyError: print("problem with http request") elif self.path == "/save": print("Received a save request") should_save = True httpd = HTTPServer(('localhost', 8000), SimpleHTTPRequestHandler) httpd.serve_forever()