player.py 2.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182
  1. import asyncio
  2. import argparse
  3. from mopidy_async_client import MopidyClient
  4. parser = argparse.ArgumentParser(description="Mopdiy FloppyPlayer")
  5. parser.add_argument("URI_string", help="Mopdiy URI to Album/Playlist.")
  6. args = parser.parse_args()
  7. inputURI = args.URI_string
  8. async def playback_started_handler(data):
  9. print(data)
  10. async def all_events_handler(event, data):
  11. print(event, data)
  12. async def main3():
  13. if inputURI == "stop":
  14. await stop()
  15. elif inputURI == "current_track":
  16. await current_track()
  17. else:
  18. await main1()
  19. async def stop():
  20. mopidy = await MopidyClient().connect()
  21. await mopidy.playback.stop()
  22. await mopidy.disconnect()
  23. async def current_track():
  24. mopidy = await MopidyClient().connect()
  25. res = await mopidy.playback.get_current_track()
  26. current_data = {
  27. "Artist": res["artists"][0]["name"],
  28. "Album": res["album"]["name"],
  29. "Song": res["name"],
  30. }
  31. print(current_data)
  32. await mopidy.disconnect()
  33. async def main1():
  34. link = inputURI.split(" - ")[1]
  35. async with MopidyClient(
  36. url="ws://127.0.0.1:6680/mopidy/ws"
  37. ) as mopidy: # close connection explicit
  38. await mopidy.playback.stop()
  39. await mopidy.tracklist.clear()
  40. await mopidy.tracklist.add(uris=[link])
  41. await mopidy.playback.play()
  42. async def main2():
  43. mopidy = await MopidyClient().connect()
  44. await mopidy.playback.stop()
  45. await mopidy.tracklist.clear()
  46. await mopidy.tracklist.add(uris=[inputURI])
  47. await mopidy.playback.play()
  48. mopidy.listener.bind("track_playback_started", playback_started_handler)
  49. mopidy.listener.bind("*", all_events_handler)
  50. # your app logic
  51. for i in range(10):
  52. # res = await mopidy.tracklist.get_tracks()
  53. res = await mopidy.playback.get_current_track()
  54. print(res)
  55. await asyncio.sleep(5)
  56. # end your app logic
  57. await mopidy.disconnect() # close connection implicit
  58. # asyncio.run(main1())
  59. # or
  60. # asyncio.run(main2())
  61. asyncio.run(main3())