Package: pyfoobeef
Allows control of the Foobar2000 and DeaDBeeF media players through thebeefwebplugin API.Both asynchronous and synchronous clientsAn asynchronous event listener with callbacksFor Python 3.6 and upMIT LicenseInstallationInstall and configure thebeefwebplugin for your media player.Run:pip install pyfoobeefDocumentationDocumentation available athttps://pyfoobeef.readthedocs.io/en/latest/ExamplesSynchronous client:importpyfoobeeffromtimeimportsleepplayer=pyfoobeef.Client("localhost",8880)# Add a new playlist.new_playlist=player.add_playlist(title="My New Playlist")player.set_current_playlist(new_playlist)# Add items to the playlist.  Note that paths including drive letters# are case sensitive even on Windows due to limitations of the beefweb# plugin (so r"c:\Music" would not work here).player.add_playlist_items(new_playlist,items=[r"C:\Music"])player.play_specific(new_playlist,1)# Give the media player a bit of time to actually start playing.sleep(0.5)# Column maps represent the media data fields to retrieve and the names# to assign the returned data to.column_map={"%artist%":"artist","%title%":"title","%album% - %track number% - %title%":"my_custom_column",}status=player.get_player_state(column_map)ifstatus.active_item.has_columns():# Returned columns may be addressed by subscripting or as attributes.print(status.active_item.columns["artist"])print(status.active_item.columns.title)print(status.active_item.columns.my_custom_column)# Display the playback state (ex. "playing")print(status.playback_state)# Display information about the volume level (current, min, max, etc.)print(status.volume)The asynchronous client follows a very similar format:importpyfoobeefimportasyncioasyncdefexample():player=pyfoobeef.AsyncClient("localhost",8880)# Add a new playlist.new_playlist=awaitplayer.add_playlist(title="My New Playlist")awaitplayer.set_current_playlist(new_playlist)# Add items to the playlist.  Note that paths including drive letters# are case sensitive even on Windows due to limitations of the beefweb# plugin (so r"c:\Music" would not work here).awaitplayer.add_playlist_items(new_playlist,items=[r"C:\Music"])# sort items by lengthawaitplayer.sort_playlist_items(new_playlist,by="%length_seconds%")# Get information about the first 10 items in a playlist.items=awaitplayer.get_playlist_items(new_playlist,column_map=["%artist%","%title%","%length%"],offset=0,count=10,)foriteminitems:print(item)# Play a specific item.awaitplayer.play_specific(new_playlist,4)asyncio.run(example())The asynchronous event listener can automatically execute callbacks when certain events are received or the media players state can be determined from the EventListener object’s attributes:importpyfoobeefimportasynciodefprint_active_item(state):print("From player state callback.  Active item is:")print(state.active_item)defprint_playlists(playlists):print("From playlists callback.  Current playlists:")forplaylistinplaylists:print(playlist)asyncdefexample():listener=pyfoobeef.EventListener(base_url="localhost",port=8880,active_item_column_map={"%artist%":"artist","%title%":"title","%length%":"length",},)# Add callbacks for player events.listener.add_callback("player_state",print_active_item)listener.add_callback("playlists",print_playlists)# Start listening for events from the player.awaitlistener.connect(reconnect_time=1)awaitasyncio.sleep(10)# The last received information about the player state and playlists# can be accessed from the listener object itself.print("From the last player state object saved to listener.""  Active item is:")print(listener.player_state.active_item)print("Estimated playback position: ",listener.player_state.estimated_position_mmss())forplaylistinlistener.playlists:print(playlist)awaitasyncio.sleep(10)# The listener should always be disconnected when done.awaitlistener.disconnect()asyncio.run(example())