You are not logged in.
Solved: For some reason, some track names were being returned as dicts, so I just put in a check that would extract the filename from the dict if that was the case. So, if you're reading this, have a MPD dynamic playlist script
I've been working on a simple dynamic playlists script for MPD that parses a config file in the user's home folder, and adds tracks that fit the specified criteria. It works fine, other than that sometimes it dies saying a file doesn't exist when it does. Here's the code:
#!/usr/bin/env python
# MPDDP: MPD Dynamic Playlists
# Call this and run it in the background (eg mpddp &>/dev/null &)
# This will check for a ~/.mpddp file containing rules in the following formats:
# path - Add tracks where the path contains the given value. Must contain a forward slash (/).
# playlist - Add tracks which are in the given playlist.
# path AND playlist - Add tracks where the path contains the given value and are in the given playlist. Path does not need to contain a forward slash.
import mpd, random, os, time
client = mpd.MPDClient()
host = "localhost" # The host MPD is operating upon
port = 6600 # The port MPD is operating upon
playlistlen = 10 # The len of the playlist
changeafter = 7 # The number of tracks before more are added/removed to/from the playlist
current = [] # The current X tracks, in the playlist.
remaining = [] # The tracks that are currently not in the playlist.
def pickNewTrack(): # Pick and remove a track from the remaining list, append it to the current list, and return the name.
global remaining
global current
global client
index = random.randint(0, len(remaining) - 1)
track = remaining[index]
remaining.remove(track)
current.append(track)
return track
def removeLastTrack(): # Remove the oldest track from the current list, append it to the remaining list, and return the name.
global remaining
global current
track = current[0]
current.remove(track)
remaining.append(track)
return track
def addNewTrackToPlaylist(): # Pick a new track, update the lists, and add it to the playlist.
global client
global host
global port
track = pickNewTrack()
print track
client.connect(host, port)
client.add(track)
client.disconnect()
def removeLastTrackFromPlaylist(): # Delete the oldest track from the playlist.
global client
global host
global port
removeLastTrack()
client.connect(host, port)
client.delete(0)
client.disconnect()
def getFilenamesFromMPD(playlist, path): # Gets the filenames from MPD of all files in the specified playlist or path. If both are specified, get tracks in both.
global remaining
global client
global host
global port
client.connect(host, port)
pathtracks = []
playlisttracks = []
tracks = []
if len(playlist) > 0:
playlisttracks = client.listplaylist(playlist)
tracks = playlisttracks
if len(path) > 0:
pathtracks = client.search("file", path)
tracks = pathtracks
if len(playlist) > 0 and len(path) > 0:
tracks = []
for track in playlisttracks:
if not pathtracks.count(track) == 0:
tracks.append(track)
client.disconnect()
return tracks
def parseConfigRule(rule): # Parse a rule from the configuration file and return what it means.
rule = rule.split("#")
rule = rule[0]
rule = rule.strip()
if len(rule) > 0:
parsed = []
if rule.count(" AND ") == 1: # This is a compound rule.
parsed = rule.split(" AND ")
elif rule.count("/") > 0: # This is a path name.
parsed = [rule, ""]
else: # This is a playlist name.
parsed = ["", rule]
return parsed
else:
return False
def parseConfigFile(): # Open the configuration file and parse the rules.
path = os.getenv("HOME") + "/.mpddp"
conf = open(path)
rules = []
for line in conf:
rules.append(parseConfigRule(line))
conf.close()
return rules
def populateLists(): # Parse the configuration file, and grab the tracks from MPD to populate the lists.
global remaining
global current
global playlistlen
global client
global host
global port
rules = parseConfigFile()
for rule in rules:
if not rule == False:
tracks = getFilenamesFromMPD(rule[1], rule[0])
for track in tracks:
if remaining.count(track) == 0:
remaining.append(track)
client.connect(host, port)
client.clear()
client.random(0)
client.disconnect()
for i in range(0, playlistlen):
addNewTrackToPlaylist()
client.connect(host, port)
client.play()
client.disconnect()
# Execute the program main loop
populateLists()
while True:
print "Looping..."
client.connect(host, port)
info = client.currentsong()
client.disconnect()
if len(info) > 0:
if info['file'] == current[changeafter - 1] or info['file'] == current[changeafter]:
addNewTrackToPlaylist()
removeLastTrackFromPlaylist()
time.sleep(60)
Last edited by Barrucadu (2009-02-23 20:25:38)
Offline