mpd mpc query language
import audioscrobbler
import mpd
import random
import time
lastsong = {}
def timer_control():
get_similar()
time.sleep(10)
timer_control()
def get_similar():
audioscrobbler
client = mpd.MPDClient()
client.connect("localhost", 6600)
mpdstatus = client.status()
prevsonginfo = client.currentsong()
global lastsong
if mpdstatus['state'] == "stop": return
if prevsonginfo == lastsong: return
lastsong = prevsonginfo
similarartists = ""
song = prevsonginfo
#if not song: break #No song, do nothing
prevartist = song['artist']
# Is the info already cached?
similar_cache = {}
if similar_cache.has_key(prevartist):
similarartists = similar_cache[prevartist]
else:
#Not cached so fetch from Audioscrobbler
try:
similarartists = [artist.name for artist in audioscrobbler.AudioScrobblerQuery(artist=prevartist).similar()]
# Cache search results and save some time next search
similar_cache[prevartist] = similarartists
except audioscrobbler.AudioScrobblerError:
similar_cache[prevartist] = None # Empty cache
return # Do nothing!
if not similarartists: return # Empty list
# Split list in half and sort upper half
# this means good matches will have priority
# but makes sure artist A does not always result in artist B
half_idx = len(similarartists)/2
upperhalf = similarartists[:half_idx]
lowerhalf = similarartists[half_idx:]
random.shuffle(upperhalf)
artistlist = upperhalf
artistlist.extend(lowerhalf)
# Try each artist in order
for artist in artistlist:
try:
print "Trying:",artist
songs = client.search("artist", artist)
if not songs: continue
selected_song = random.sample(songs, 1)[0]
client.add(selected_song['file'])
print "Added", selected_song['title'],"by",selected_song['artist']
# Delete old song from playlist?
break
except mpd.MPDError, e:
print "MPDError", e.message
continue
except ValueError, e:
print "ValueError:",e.message
continue
timer_control()
follow this article for more info https://bbs.archlinux.org/viewtopic.php?id=49765 http://manpages.ubuntu.com/manpages/intrepid/man1/mpc.1.html
mpc
does not (as of this writing, v0.27-1) have any commands that give you all the metadata for a song, identified by uri. It's surprising that it doesn't, but it doesn't.
The MPD communication protocol, however, does return full information about each song.
If I issue mpc current
I only get this response:
Afro Celt Sound System - Release
However, if I send the MPD (not MPC) command currentsong
then I get this response from MPD:
file: gavin/Compilations/Volume 2 _ Release/01 Release.m4a
Last-Modified: 2005-03-02T14:16:51Z
Artist: Afro Celt Sound System
Album: Volume 2 : Release
Title: Release
Track: 1/11
Genre:World
Date: 1999
Composer: Simon Emerson, James McNally, Iarla O Lionaird, Martin Russell & Sinead O'Connor/Sinéad O'Connor
Disc: 1/1
Time: 456
Pos: 0
Id: 53616
If I send the MPD command playlistid 49312
I get this response:
file: gavin/ATB/Future Memories/16 Missing 1.m4a
Last-Modified: 2013-11-17T21:14:43Z
Artist: ATB
Album: Future Memories
Title: Missing
Track: 16/29
Genre: Dance
Date: 2009-05-01T07:00:00Z
Composer: Tracey Thorn & Ben Watt
Disc: 1/1
AlbumArtist: ATB
Time: 256
Pos: 10
Id: 49312
If I send the command search file surfing
I get this response (different songs are delimited by a new file:
):
file: doza/Air/06 Surfing On a Rocket.m4a
Last-Modified: 2015-11-20T15:56:00Z
Time: 223
Artist: Air
Album: Talkie Walkie
Title: Surfing On a Rocket
Track: 6/10
Genre: Electronic
Date: 2004-01-26T08:00:00Z
Disc: 1/1
AlbumArtist: Air
file: gavin/Air/Surfing On a Rocket EP/06 Surfing on a rocket (remixed by Joakim).m4a
Last-Modified: 2015-01-13T15:31:39Z
Time: 393
Artist: Air
Album: Surfing On a Rocket EP
Title: Surfing on a rocket (remixed by Joakim)
Track: 6/7
Genre: Dance
Date: 2004-09-10T07:00:00Z
Composer: Jean-Benoit Dunckel & Nicolas Godin
Disc: 1/1
AlbumArtist: Air
file: gavin/The Beach Boys/Greatest Surfing Songs!/02 Little Deuce Coupe.mp3
Last-Modified: 2009-09-10T04:32:49Z
Time: 111
Artist: The Beach Boys
Title: Little Deuce Coupe
Album: Greatest Surfing Songs!
Track: 2
Genre: Sunshine Pop
And if I send the MPD command listplaylistinfo Thump
, I get this response:
file: gavin/Muse/The 2nd Law (Deluxe Version)/02 Madness.m4a
Last-Modified: 2013-11-17T22:30:54Z
Artist: Muse
Album: The 2nd Law (Deluxe Version)
Title: Madness
Track: 2/13
Genre: Alternative
Date: 2012-10-01T07:00:00Z
Disc: 1/1
AlbumArtist: Muse
Time: 280
file: gavin/U2/The Best Of 1990-2000/15 Numb (New Mix).mp3
Last-Modified: 2003-09-24T14:31:05Z
Artist: U2
Title: Numb (New Mix)
Album: The Best Of 1990-2000
Track: 15/16
Date: 2002
Genre: Rock
Composer: U2
Time: 264
file: gavin/Massive Attack/Mezzanine/03 Teardrop.m4a
Last-Modified: 2012-09-05T19:38:14Z
Artist: Massive Attack
Album: Mezzanine
Title: Teardrop
Track: 3/11
Genre: Electronic
Date: 1998-04-20T07:00:00Z
Disc: 1/1
AlbumArtist: Massive Attack
Time: 331
file: gavin/Massive Attack/Mezzanine/01 Angel.m4a
Last-Modified: 2012-09-05T19:38:17Z
Artist: Massive Attack
Album: Mezzanine
Title: Angel
Track: 1/11
Genre: Electronic
Date: 1998-04-20T07:00:00Z
Disc: 1/1
AlbumArtist: Massive Attack
Time: 380
file: gavin/Eels/Shrek 2/07 I Need Some Sleep.mp3
Last-Modified: 2005-01-14T21:24:25Z
Artist: Eels
Title: I Need Some Sleep
Album: Shrek 2
Track: 7/14
Date: 2004
Genre: Soundtrack
Time: 147
I personally use the ruby-mpd
library to provide a convenient interface to communicating with MPD, in a rich, full-featured way.