Best Way to Interface with the MPD Server in Python
ProgrammingThis forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
Best Way to Interface with the MPD Server in Python
Hello, so I would like to incorporate MPD into the media player plugin for the liferea feed aggregator. The plugin is written in Python and it seems like a pretty simple task, but I can't find and MPD server (not client) modules for it online. I found a module called python-mpd-server but it's still in beta. Is there a native module for this or another way I can add this functionality?
The problem is that this is actual source code for a plugin. Using a bash script would be pretty unprofessional and inefficient. I'd need it to be like how Rhythmbox or GNOME MPV integrates with MPD.
I'm sorry, but I think we need to take a step back here. What exactly do you want to use the "server libraries" for? "Incorporate MPD" and "I need to integrate MPD into an application so that an MPD client can be used with it." (which is as specific as you got) isn't giving any of us an adequate picture, as I'm sure you've noticed by now.
As MPD is a server, there is normally no reason for "server libraries".
I mean, do you want the server library to:
start and stop MPD? (sorry, you'll have to do that yourself)
provide an API for clients to use to send commands to MPD? (that's what client libraries are for, and that includes what you linked in the top post)
I'm sorry, but I think we need to take a step back here. What exactly do you want to use the "server libraries" for? "Incorporate MPD" and "I need to integrate MPD into an application so that an MPD client can be used with it." (which is as specific as you got) isn't giving any of us an adequate picture, as I'm sure you've noticed by now.
As MPD is a server, there is normally no reason for "server libraries".
I mean, do you want the server library to:
start and stop MPD? (sorry, you'll have to do that yourself)
provide an API for clients to use to send commands to MPD? (that's what client libraries are for, and that includes what you linked in the top post)
Sorry for being vague. I am trying to add play/pause/next/previous key functionality to the plugin. From what I know that's an MPD thing.
I guess I'll need to do more research on the subject.
Then you use any of the three client libraries that have already been linked: the one you linked to in the top post, or the two that were linked later.
Here's a demo of using python-musicpd to play a add a song from the MPD library to the playlist, play it, stop playing, and remove it from the playlist:
Code:
>>> from musicpd import MPDClient
>>> from pprint import pprint
>>> client = MPDClient()
>>> client.connect('localhost', 6600)
>>> song = client.listallinfo()[-1]
>>> pprint(song)
{'album': 'Unearthed',
'artist': 'Johnny Cash',
'duration': '219.306',
'file': 'unearthed/79 - Hurt.flac',
'format': '44100:16:2',
'label': 'American Recordings',
'last-modified': '2018-05-24T14:57:25Z',
'musicbrainz_albumid': '4321855e-8e8e-4786-8506-28e6d69633b9',
'musicbrainz_artistid': 'd43d12a1-2dc9-4257-a2fd-0a3bb1081b86',
'time': '219',
'title': 'Hurt',
'track': '79'}
>>> songid = client.addid(song['file'])
>>> songid
'2'
>>> client.playid(songid)
>>> # I can hear the song playing now
>>> client.stop()
>>> # The music stops
>>> client.deleteid(songid)
>>> client.disconnect()
In MPD, playing a song is done in two steps: adding it to the playlist, and playing it from the playlist.
Also, you need to understand how MPD works and what the D means. It runs as a server, and it makes itself available either at a port that you can connect a socket to, or as a UNIX socket. Clients then connect to that by opening the appropriate type of socket, sending it commands through the socket, and receiving the results through the socket. If you have MPD running at port 6600, then you can actually telnet to port 6600 and just issue it commands from the keyboard.
If you're familiar with databases: that's how a lot of them work too.
And if I'm coming across as condescending by explaining something this "obvious": I'm currently writing my second MPD client, and let me tell you, this wasn't obvious to me for a long time.
Here's a demo of using python-musicpd to play a add a song from the MPD library to the playlist, play it, stop playing, and remove it from the playlist:
Code:
>>> from musicpd import MPDClient
>>> from pprint import pprint
>>> client = MPDClient()
>>> client.connect('localhost', 6600)
>>> song = client.listallinfo()[-1]
>>> pprint(song)
{'album': 'Unearthed',
'artist': 'Johnny Cash',
'duration': '219.306',
'file': 'unearthed/79 - Hurt.flac',
'format': '44100:16:2',
'label': 'American Recordings',
'last-modified': '2018-05-24T14:57:25Z',
'musicbrainz_albumid': '4321855e-8e8e-4786-8506-28e6d69633b9',
'musicbrainz_artistid': 'd43d12a1-2dc9-4257-a2fd-0a3bb1081b86',
'time': '219',
'title': 'Hurt',
'track': '79'}
>>> songid = client.addid(song['file'])
>>> songid
'2'
>>> client.playid(songid)
>>> # I can hear the song playing now
>>> client.stop()
>>> # The music stops
>>> client.deleteid(songid)
>>> client.disconnect()
In MPD, playing a song is done in two steps: adding it to the playlist, and playing it from the playlist.
Also, you need to understand how MPD works and what the D means. It runs as a server, and it makes itself available either at a port that you can connect a socket to, or as a UNIX socket. Clients then connect to that by opening the appropriate type of socket, sending it commands through the socket, and receiving the results through the socket. If you have MPD running at port 6600, then you can actually telnet to port 6600 and just issue it commands from the keyboard.
If you're familiar with databases: that's how a lot of them work too.
Thank you so much for the help. I was pretty confused about this, clearly, but now I think I understand. I'll see if I can implement this!
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.