fake_mpd.py 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  1. #!/usr/bin/env python3
  2. """Minimal MPD server for exercising the UI's commands without touching a real
  3. player. Records every command received to the file given as the second
  4. argument. Usage: fake_mpd.py PORT LOGFILE [IDLE_EVENT_SECONDS]
  5. """
  6. import socketserver
  7. import sys
  8. import threading
  9. log_path = sys.argv[2]
  10. idle_event_s = float(sys.argv[3]) if len(sys.argv) > 3 else None
  11. class Player:
  12. state = "play"
  13. volume = 50
  14. song = 2
  15. player = Player()
  16. def record(line):
  17. with open(log_path, "a") as f:
  18. f.write(line + "\n")
  19. class Handler(socketserver.StreamRequestHandler):
  20. def send(self, text):
  21. self.wfile.write(text.encode())
  22. self.wfile.flush()
  23. def handle(self):
  24. self.send("OK MPD 0.21.4\n")
  25. announced = False
  26. for raw in self.rfile:
  27. line = raw.decode("utf8").strip()
  28. cmd = line.split(" ")[0]
  29. if cmd != "idle":
  30. record(line)
  31. if cmd == "idle":
  32. # normally answered by the following noidle; optionally
  33. # announce a change once per connection, as a real server would
  34. if idle_event_s is not None and not announced:
  35. announced = True
  36. threading.Timer(
  37. idle_event_s, self.send, ["changed: player\nOK\n"]
  38. ).start()
  39. continue
  40. if cmd == "status":
  41. self.send(
  42. f"volume: {player.volume}\nrepeat: 1\nrandom: 1\nsingle: 0\n"
  43. f"consume: 0\nplaylist: 7\nplaylistlength: 10\n"
  44. f"state: {player.state}\nsong: {player.song}\nsongid: 3\n"
  45. "time: 83:296\nelapsed: 83.100\nbitrate: 320\nduration: 296.000\n"
  46. "OK\n"
  47. )
  48. elif cmd == "currentsong":
  49. self.send(
  50. "file: muzyka/test.flac\nArtist: Zażółć Gęślą\n"
  51. "Title: Jaźń – bardzo długi tytuł utworu, który na pewno "
  52. "nie zmieści się w jednej linii wyświetlacza\n"
  53. f"Pos: {player.song}\nId: 3\nOK\n"
  54. )
  55. elif cmd == "noidle":
  56. self.send("OK\n")
  57. elif cmd == "volume":
  58. player.volume += int(line.split(" ")[1].strip('"'))
  59. self.send("OK\n")
  60. elif cmd == "stop":
  61. player.state = "stop"
  62. self.send("OK\n")
  63. elif cmd == "play":
  64. player.state = "play"
  65. self.send("OK\n")
  66. elif cmd == "pause":
  67. player.state = "pause" if player.state == "play" else "play"
  68. self.send("OK\n")
  69. elif cmd == "next":
  70. player.song += 1
  71. self.send("OK\n")
  72. elif cmd == "previous":
  73. player.song -= 1
  74. self.send("OK\n")
  75. elif cmd == "close":
  76. return
  77. else:
  78. self.send(f"ACK [5@0] {{{cmd}}} unknown command\n")
  79. class Server(socketserver.ThreadingTCPServer):
  80. allow_reuse_address = True
  81. daemon_threads = True
  82. Server(("127.0.0.1", int(sys.argv[1])), Handler).serve_forever()