fake_mpd.py 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139
  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 [NEXT_DELAY_SECONDS]]
  5. """
  6. import socketserver
  7. import sys
  8. import threading
  9. import time
  10. log_path = sys.argv[2]
  11. idle_event_s = float(sys.argv[3]) if len(sys.argv) > 3 and sys.argv[3] else None
  12. delay_s = float(sys.argv[4]) if len(sys.argv) > 4 else 0
  13. class Player:
  14. state = "play"
  15. volume = 50
  16. song = 2
  17. player = Player()
  18. # the queue: playlistlength in status matches it
  19. QUEUE = [
  20. ("Artysta %d" % i, "Utwór numer %d" % i, 180 + i * 7) for i in range(10)
  21. ]
  22. QUEUE[3] = ("Bardzo, bardzo długa nazwa wykonawcy", "Również bardzo długi tytuł utworu", 193)
  23. QUEUE[4] = ("Krótki", "Tytuł tak długi, że sam zajmie całą linię wyświetlacza", 402)
  24. QUEUE[5] = (None, None, None) # a stream
  25. def playlistinfo():
  26. out = ""
  27. for i, (artist, title, duration) in enumerate(QUEUE):
  28. if artist is None:
  29. out += "file: http://radio.example/stream\nName: Radio Testowe\n"
  30. else:
  31. out += "file: muzyka/%d.flac\nArtist: %s\nTitle: %s\n" % (i, artist, title)
  32. out += "Time: %d\nduration: %d.000\n" % (duration, duration)
  33. out += "Pos: %d\nId: %d\n" % (i, i + 1)
  34. return out + "OK\n"
  35. def record(line):
  36. with open(log_path, "a") as f:
  37. f.write(line + "\n")
  38. class Handler(socketserver.StreamRequestHandler):
  39. def send(self, text):
  40. self.wfile.write(text.encode())
  41. self.wfile.flush()
  42. def idle_event(self):
  43. with self.lock:
  44. if self.idling:
  45. self.idling = False
  46. self.send("changed: player\nOK\n")
  47. def handle(self):
  48. self.lock = threading.Lock()
  49. self.idling = False
  50. self.send("OK MPD 0.21.4\n")
  51. announced = False
  52. for raw in self.rfile:
  53. line = raw.decode("utf8").strip()
  54. cmd = line.split(" ")[0]
  55. if cmd != "idle":
  56. record(line)
  57. if cmd == "idle":
  58. # normally answered by the following noidle; optionally
  59. # announce a change once per connection, as a real server would
  60. with self.lock:
  61. self.idling = True
  62. if idle_event_s is not None and not announced:
  63. announced = True
  64. threading.Timer(idle_event_s, self.idle_event).start()
  65. continue
  66. if cmd == "noidle":
  67. # as MPD: ignored unless idle, then the idle gets its answer
  68. with self.lock:
  69. if self.idling:
  70. self.idling = False
  71. self.send("OK\n")
  72. continue
  73. if delay_s and cmd in ("next", "previous"):
  74. time.sleep(delay_s) # opening a song from a sleeping disk
  75. if cmd == "status":
  76. self.send(
  77. f"volume: {player.volume}\nrepeat: 1\nrandom: 1\nsingle: 0\n"
  78. f"consume: 0\nplaylist: 7\nplaylistlength: 10\n"
  79. f"state: {player.state}\nsong: {player.song}\nsongid: 3\n"
  80. "time: 83:296\nelapsed: 83.100\nbitrate: 320\nduration: 296.000\n"
  81. "OK\n"
  82. )
  83. elif cmd == "currentsong":
  84. self.send(
  85. "file: muzyka/test.flac\nArtist: Zażółć Gęślą\n"
  86. "Title: Jaźń – bardzo długi tytuł utworu, który na pewno "
  87. "nie zmieści się w jednej linii wyświetlacza\n"
  88. f"Pos: {player.song}\nId: 3\nOK\n"
  89. )
  90. elif cmd == "volume":
  91. player.volume += int(line.split(" ")[1].strip('"'))
  92. self.send("OK\n")
  93. elif cmd == "stop":
  94. player.state = "stop"
  95. self.send("OK\n")
  96. elif cmd == "playlistinfo":
  97. self.send(playlistinfo())
  98. elif cmd == "play":
  99. player.state = "play"
  100. if " " in line:
  101. player.song = int(line.split(" ")[1].strip('"'))
  102. self.send("OK\n")
  103. elif cmd == "pause":
  104. player.state = "pause" if player.state == "play" else "play"
  105. self.send("OK\n")
  106. elif cmd == "next":
  107. player.song += 1
  108. self.send("OK\n")
  109. elif cmd == "previous":
  110. player.song -= 1
  111. self.send("OK\n")
  112. elif cmd == "seekcur":
  113. self.send("OK\n")
  114. elif cmd == "close":
  115. return
  116. else:
  117. self.send(f"ACK [5@0] {{{cmd}}} unknown command\n")
  118. class Server(socketserver.ThreadingTCPServer):
  119. allow_reuse_address = True
  120. daemon_threads = True
  121. Server(("127.0.0.1", int(sys.argv[1])), Handler).serve_forever()