]> kaliko git repositories - python-musicpdaio.git/blobdiff - doc/source/tutorial/tutorial-00.py
Add Sphinx documentation
[python-musicpdaio.git] / doc / source / tutorial / tutorial-00.py
diff --git a/doc/source/tutorial/tutorial-00.py b/doc/source/tutorial/tutorial-00.py
new file mode 100644 (file)
index 0000000..dea5826
--- /dev/null
@@ -0,0 +1,45 @@
+import asyncio
+import logging
+
+from mpdaio.client import MPDClient
+
+# Configure loggers
+logging.basicConfig(level=logging.INFO, format='%(levelname)-8s %(message)s')
+logging.getLogger("asyncio").setLevel(logging.WARNING)
+# debug level level will show where defaults settings come from
+log = logging.getLogger('mpdaio.client')
+log.setLevel(logging.DEBUG)
+
+
+async def run():
+    # Explicit host declaration
+    #client = MPDClient(host='example.org', port='6601')
+
+    # Use defaults
+    client = MPDClient()
+    # MPDClient use MPD_HOST/MPD_PORT env var if set
+    # else test ${XDG_RUNTIME_DIR}/mpd/socket for existence
+    # finnally fallback to localhost:6600
+
+    # Make an initial connection to MPD server
+    # The connection is kept open an reused for later commands
+    await client.ping()
+
+    status = await client.status()
+    if status.get('state') == 'play':
+        current_song_id = status.get('songid')
+        current_song = await client.playlistid(current_song_id)
+        log.info(f'Playing   : {current_song[0].get("file")}')
+        next_song_id = status.get('nextsongid', None)
+        if next_song_id:
+            next_song = await client.playlistid(next_song_id)
+            log.info(f'Next song : {next_song[0].get("file")}')
+    else:
+        log.info('Not playing')
+
+    # Closes any remaining connections to MPD server
+    await client.close()
+
+
+if __name__ == '__main__':
+    asyncio.run(run())