Package: raveberry-mopidyapi
MopidyAPIMopidyAPIis a Python (3.6+) library for interacting withMopidyvia itsJSON RPC
API.MopidyAPIuses HTTP to call RPC methods, and websockets to listen for events.It is compatible with all functions present in Mopidy 3.0.1.Installationpip install mopidyapiUsageMopidyAPIcontains functions mapping to each ofthe Mopidy v3.0.1 core API functions.For examplemopidy.core.PlaybackController.pause()in the documentation maps toMopidyAPI.playback.pause()here.Quick example>>>frommopidyapiimportMopidyAPI>>>m=MopidyAPI()>>>tracks=m.tracklist.get_tracks()>>>tracks[0].name'I'veSeenFootage'>>>tracks[0].artists[Artist(name='Death Grips',uri='spotify:artist:5RADpgYLOuS2ZxDq7ggYYH')]>>>tracks[0]._fields('album','name','disc_no','uri','length','track_no','artists','date','bitrate')Connecting to MopidyTo connect to Mopidy, you need to instantiate aMopidyAPIobject.
By default, it will connect to Mopidy atlocalhost:6680,
so you might not need to give the constructor any arguments.frommopidyapiimportMopidyAPIm=MopidyAPI(host='my.mopidy.host.com',port=6680)You can also passuse_websockets=False, to prevent starting the websocket listener,
which runs in a separate thread. However, event listening (described below) won't work with this set.Calling Mopidy functionsAll of the functions described in theMopidy 3.0.1 core API documentationare available inMopidyAPI.Functions named in the Mopidy docs ascore.<ControllerName>Controller.<functionname>(),
will be under the nameMopidyAPI.<controllername>.<functionname>()For example, you can pause the music by callingm.playback.pause(),
or you could search for a song by calling e.g.m.library.search(artist='Rick Astley'), wherem = MopidyAPI().Functions will returnPython nativenamedtuplerepresentations of the returned data.Event listeningYou can use function decorators to mark specific functions to be called when an event arrives. See example below.The events used are described inMopidy's core events documentation.frommopidyapiimportMopidyAPIm=MopidyAPI()@m.on_event('volume_changed')defprint_volume(event):print(f"Volume changed to:{event.volume}")@m.on_event('track_playback_started')defprint_newtracks(event):print(f"Started playing track:{event.track.name}")Like with function calls, the events passed arenamedtuples.If you need to add and remove callbacks more dynamically, you can use theadd_callbackanddel_callbackfunctions.del_callbackaccepts two keyword arguments:event: an event name string to remove all callbacks forf: a callback function to removeYou can use one or both. Here's an example of passing a function todel_callback:frommopidyapiimportMopidyAPIm=MopidyAPI()defprint_volume(event):""" Print volume only once. Delete callback after first call """print(f"Volume changed to:{event.volume}")m.del_callback(f=print_volume)m.add_callback('volume_changed',print_volume)An important caveat with both the event listener decorators and add methods,
is that it will not warn you in any way, if you use an invalid event name (e.g. you misspell the event name).Note on the choice ofnamedtuplesWhynamedtuples?The choice of namedtuples might seem unusual (or even inconvenient),
but they have a number of advantages over dictionaries for this application:1. Less verbose than dicts.event.tl_track.track.album.nameis shorter and easier on the eyes thanevent['tl_track']['track']['album']['name']2. They print much more neatly.Artist(name='Death Grips', uri='spotify:artist:5RADpgYLOuS2ZxDq7ggYYH')is much better than{'name': 'Death Grips', 'uri': 'spotify:artist:5RADpgYLOuS2ZxDq7ggYYH'}.3.namedtuplesaccurately represent the immutable nature of the data.Being allowed to mutate the data coming from Mopidy might give one the idea that this would change the data inside Mopidy, which is obviously not the case....but, I know dicts!Okay, so if you need.keys(), you can use._fields()instead,
and if you absolutely need a dict, you can use._asdict(),
which will return an actual dict.ContributingPlease do tell me about bugs via thegithub issue tracker.Also feel free to write, if you're just in the mood to help me improve this project. I don't bite :)LicenseThis project is licensed under the GPLv3.
See theLICENSEfile for details.