| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139 |
- #!/usr/bin/env python3
- """Minimal MPD server for exercising the UI's commands without touching a real
- player. Records every command received to the file given as the second
- argument. Usage: fake_mpd.py PORT LOGFILE [IDLE_EVENT_SECONDS [NEXT_DELAY_SECONDS]]
- """
- import socketserver
- import sys
- import threading
- import time
- log_path = sys.argv[2]
- idle_event_s = float(sys.argv[3]) if len(sys.argv) > 3 and sys.argv[3] else None
- delay_s = float(sys.argv[4]) if len(sys.argv) > 4 else 0
- class Player:
- state = "play"
- volume = 50
- song = 2
- player = Player()
- # the queue: playlistlength in status matches it
- QUEUE = [
- ("Artysta %d" % i, "Utwór numer %d" % i, 180 + i * 7) for i in range(10)
- ]
- QUEUE[3] = ("Bardzo, bardzo długa nazwa wykonawcy", "Również bardzo długi tytuł utworu", 193)
- QUEUE[4] = ("Krótki", "Tytuł tak długi, że sam zajmie całą linię wyświetlacza", 402)
- QUEUE[5] = (None, None, None) # a stream
- def playlistinfo():
- out = ""
- for i, (artist, title, duration) in enumerate(QUEUE):
- if artist is None:
- out += "file: http://radio.example/stream\nName: Radio Testowe\n"
- else:
- out += "file: muzyka/%d.flac\nArtist: %s\nTitle: %s\n" % (i, artist, title)
- out += "Time: %d\nduration: %d.000\n" % (duration, duration)
- out += "Pos: %d\nId: %d\n" % (i, i + 1)
- return out + "OK\n"
- def record(line):
- with open(log_path, "a") as f:
- f.write(line + "\n")
- class Handler(socketserver.StreamRequestHandler):
- def send(self, text):
- self.wfile.write(text.encode())
- self.wfile.flush()
- def idle_event(self):
- with self.lock:
- if self.idling:
- self.idling = False
- self.send("changed: player\nOK\n")
- def handle(self):
- self.lock = threading.Lock()
- self.idling = False
- self.send("OK MPD 0.21.4\n")
- announced = False
- for raw in self.rfile:
- line = raw.decode("utf8").strip()
- cmd = line.split(" ")[0]
- if cmd != "idle":
- record(line)
- if cmd == "idle":
- # normally answered by the following noidle; optionally
- # announce a change once per connection, as a real server would
- with self.lock:
- self.idling = True
- if idle_event_s is not None and not announced:
- announced = True
- threading.Timer(idle_event_s, self.idle_event).start()
- continue
- if cmd == "noidle":
- # as MPD: ignored unless idle, then the idle gets its answer
- with self.lock:
- if self.idling:
- self.idling = False
- self.send("OK\n")
- continue
- if delay_s and cmd in ("next", "previous"):
- time.sleep(delay_s) # opening a song from a sleeping disk
- if cmd == "status":
- self.send(
- f"volume: {player.volume}\nrepeat: 1\nrandom: 1\nsingle: 0\n"
- f"consume: 0\nplaylist: 7\nplaylistlength: 10\n"
- f"state: {player.state}\nsong: {player.song}\nsongid: 3\n"
- "time: 83:296\nelapsed: 83.100\nbitrate: 320\nduration: 296.000\n"
- "OK\n"
- )
- elif cmd == "currentsong":
- self.send(
- "file: muzyka/test.flac\nArtist: Zażółć Gęślą\n"
- "Title: Jaźń – bardzo długi tytuł utworu, który na pewno "
- "nie zmieści się w jednej linii wyświetlacza\n"
- f"Pos: {player.song}\nId: 3\nOK\n"
- )
- elif cmd == "volume":
- player.volume += int(line.split(" ")[1].strip('"'))
- self.send("OK\n")
- elif cmd == "stop":
- player.state = "stop"
- self.send("OK\n")
- elif cmd == "playlistinfo":
- self.send(playlistinfo())
- elif cmd == "play":
- player.state = "play"
- if " " in line:
- player.song = int(line.split(" ")[1].strip('"'))
- self.send("OK\n")
- elif cmd == "pause":
- player.state = "pause" if player.state == "play" else "play"
- self.send("OK\n")
- elif cmd == "next":
- player.song += 1
- self.send("OK\n")
- elif cmd == "previous":
- player.song -= 1
- self.send("OK\n")
- elif cmd == "seekcur":
- self.send("OK\n")
- elif cmd == "close":
- return
- else:
- self.send(f"ACK [5@0] {{{cmd}}} unknown command\n")
- class Server(socketserver.ThreadingTCPServer):
- allow_reuse_address = True
- daemon_threads = True
- Server(("127.0.0.1", int(sys.argv[1])), Handler).serve_forever()
|