]> kaliko git repositories - python-musicpdaio.git/blob - doc/source/tutorial/tutorial-00.py
Add Sphinx documentation
[python-musicpdaio.git] / doc / source / tutorial / tutorial-00.py
1 import asyncio
2 import logging
3
4 from mpdaio.client import MPDClient
5
6 # Configure loggers
7 logging.basicConfig(level=logging.INFO, format='%(levelname)-8s %(message)s')
8 logging.getLogger("asyncio").setLevel(logging.WARNING)
9 # debug level level will show where defaults settings come from
10 log = logging.getLogger('mpdaio.client')
11 log.setLevel(logging.DEBUG)
12
13
14 async def run():
15     # Explicit host declaration
16     #client = MPDClient(host='example.org', port='6601')
17
18     # Use defaults
19     client = MPDClient()
20     # MPDClient use MPD_HOST/MPD_PORT env var if set
21     # else test ${XDG_RUNTIME_DIR}/mpd/socket for existence
22     # finnally fallback to localhost:6600
23
24     # Make an initial connection to MPD server
25     # The connection is kept open an reused for later commands
26     await client.ping()
27
28     status = await client.status()
29     if status.get('state') == 'play':
30         current_song_id = status.get('songid')
31         current_song = await client.playlistid(current_song_id)
32         log.info(f'Playing   : {current_song[0].get("file")}')
33         next_song_id = status.get('nextsongid', None)
34         if next_song_id:
35             next_song = await client.playlistid(next_song_id)
36             log.info(f'Next song : {next_song[0].get("file")}')
37     else:
38         log.info('Not playing')
39
40     # Closes any remaining connections to MPD server
41     await client.close()
42
43
44 if __name__ == '__main__':
45     asyncio.run(run())