12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182 |
- import asyncio
- import argparse
- from mopidy_async_client import MopidyClient
- parser = argparse.ArgumentParser(description="Mopdiy FloppyPlayer")
- parser.add_argument("URI_string", help="Mopdiy URI to Album/Playlist.")
- args = parser.parse_args()
- inputURI = args.URI_string
- async def playback_started_handler(data):
- print(data)
- async def all_events_handler(event, data):
- print(event, data)
- async def main3():
- if inputURI == "stop":
- await stop()
- elif inputURI == "current_track":
- await current_track()
- else:
- await main1()
- async def stop():
- mopidy = await MopidyClient().connect()
- await mopidy.playback.stop()
- await mopidy.disconnect()
- async def current_track():
- mopidy = await MopidyClient().connect()
- res = await mopidy.playback.get_current_track()
- current_data = {
- "Artist": res["artists"][0]["name"],
- "Album": res["album"]["name"],
- "Song": res["name"],
- }
- print(current_data)
- await mopidy.disconnect()
- async def main1():
- link = inputURI.split(" - ")[1]
- async with MopidyClient(
- url="ws://127.0.0.1:6680/mopidy/ws"
- ) as mopidy: # close connection explicit
- await mopidy.playback.stop()
- await mopidy.tracklist.clear()
- await mopidy.tracklist.add(uris=[link])
- await mopidy.playback.play()
- async def main2():
- mopidy = await MopidyClient().connect()
- await mopidy.playback.stop()
- await mopidy.tracklist.clear()
- await mopidy.tracklist.add(uris=[inputURI])
- await mopidy.playback.play()
- mopidy.listener.bind("track_playback_started", playback_started_handler)
- mopidy.listener.bind("*", all_events_handler)
- # your app logic
- for i in range(10):
- # res = await mopidy.tracklist.get_tracks()
- res = await mopidy.playback.get_current_track()
- print(res)
- await asyncio.sleep(5)
- # end your app logic
- await mopidy.disconnect() # close connection implicit
- # asyncio.run(main1())
- # or
- # asyncio.run(main2())
- asyncio.run(main3())
|