Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

mpd mpc query language

Tags:

mpd

I can't figure out how to properly query mpd using mpc.

For example: I know how to list all albums

mpc list album

But I want to get more than the name.

How can I query mpd for album, path, artist, track number, duration and so on? Preferably in one query but multiple queries are ok as well.

I tried to read the mpc man page and the official documentation of mpd but can't figure it out.

like image 624
arkhon Avatar asked Jul 17 '15 08:07

arkhon


1 Answers

 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

like image 137
youngdero Avatar answered Oct 07 '22 20:10

youngdero