def update(self): self.yield_func() mpd = mpdclient3.connect() mpd_tracks = mpd.do.listallinfo() found = {} for mpd_track in mpd_tracks: if not self.yield_func(): break if mpd_track['type'] == 'file': dir, filename = os.path.split(mpd_track['file']) dir = os.path.join(dir, '') dir_id = self.db.get_dir_id(None, dir) if not dir in found: found[dir] = [filename] else: found[dir].append(filename) if self.db.get_track_mtime(dir_id, filename) == 0: tag = MpdTagAbsorber(mpd_track) self.db.add_track(dir_id, filename, 1, tag) db_subdirs = self.db.get_dirs() for subdir in db_subdirs: if not subdir[1] in found.keys(): self.db.delete_dir_by_dir_id(subdir[0]) for dir, filenames in found.items(): dir_id = self.db.get_dir_id(None, dir) db_filenames = self.db.get_filenames_by_dir_id(dir_id) for filename in db_filenames: if not filename[0] in filenames: self.db.delete_track(dir_id, filename[0])
# Valid examples: # 'Some Artist': 1.5, # ('An Artist', "Hey It's An Album"): 5.0, # ('Artist', 'Album', 'the name of the song that I hate'): 0, # Enter such lines into a file (preferably ~/.mpd-weights if you want the # killsong.py script to cooperate), and pipe that file into this script. Like: # $ ./mpd_weighted_playlist.py < ~/.mpd-weights import mpdclient3, re, random, sys weights = eval('{'+sys.stdin.read()+'}') c = mpdclient3.connect() songs = filter(lambda x: x.type == 'file', c.do.listallinfo()) wasplaying = c.do.status().state == 'play' c.do.clear() for song in songs: weight = 1 keys = [ song.artist, (song.artist, song.album), (song.artist, song.album, song.title), ] for key in keys: weight *= weights.get(key, 1)