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