#!/usr/bin/python #RhythmDB Loader, by Thomas Folz-Donahue #This is GPL free software import sys, string from xml.dom import minidom import gtk from library import library import config from plugins.songsmenu import SongsMenuPlugin from functools import partial import urllib import os.path try: config.get("plugins", __name__) except: config.set("plugins", __name__, False) def passfun(key, value, name=None, pre=False, kind="string"): if name != None: key = name elif pre: key = "~#"+key.encode('ascii') else: key = key.encode('ascii') if kind=="int": value = int(value) elif kind=="float": value = float(value) return (key,value) song_import = {u'album':passfun, u'rating':partial(passfun,pre=True,kind="float"), u'artist':passfun, u'first-seen':partial(passfun,name='~#added'), u'title':passfun, u'play-count':partial(passfun,name='~#playcount',kind="int"), u'last-played':partial(passfun,name='~#lastplayed', kind='int'), u'mtime':partial(passfun,pre=True,kind='int'), u'genre':passfun, } class rhythmdbloader(SongsMenuPlugin): PLUGIN_ID = "rhythmdbloader" PLUGIN_NAME = _("RhythmDB Loader") PLUGIN_DESC = _("Import your preexisting RhythmDB database") PLUGIN_VERSION = "0.1" PLUGIN_ICON = 'gtk-open' @classmethod def PluginPreferences(self, parent): overwrite_button = gtk.CheckButton(_("overwrite QuodLibet data with RhythmDB data in case of conflicts")) overwrite_button.connect('toggled', self.set_overwrite_mode) justdoit_button = gtk.Button(_("import all songs now")) justdoit_button.connect('pressed', self.import_all) vbox = gtk.VBox() vbox.add(overwrite_button) vbox.add(justdoit_button) return vbox @classmethod def set_overwrite_mode(self, button): config.set("plugins", __name__, button.get_active()) @classmethod def import_all(self, foo): print "lol" rhythmdb = loadrhythmdb(os.path.expanduser("~/.gnome2/rhythmbox/rhythmdb.xml")) for rdb_loc in rhythmdb: if rdb_loc[:7] == "file://": ql_loc = urllib.unquote(rdb_loc[7:]) else: ql_loc = rdb_loc if not config.get("plugins", __name__): if ql_loc in library: print "%s already exists"%ql_loc continue libeme = {} for key in rhythmdb[rdb_loc]: try: k,v = song_import[key](key,rhythmdb[rdb_loc][key]) libeme[k] = v except KeyError: continue print "adding %s:%s to library"%(ql_loc,libeme) library.add_filename(ql_loc) if ql_loc in library: for key in libeme: library[ql_loc][key] = libeme[key] # while True: # try: # print input(">") # except: # continue # lolxml def loadrhythmdb(rhythmdbfile): rhythmdb = minidom.parse(rhythmdbfile) library = {} entries = rhythmdb.getElementsByTagName("entry") for entry in entries: if entry.getAttribute("type") != u"song": continue # not supporting ignore, iradio yet libeme = {} for child in entry.childNodes: if child.nodeType != minidom.Node.ELEMENT_NODE: continue try: libeme[child.tagName] = child.firstChild.data except AttributeError: continue try: location = libeme[u"location"] except KeyError: continue del libeme[u"location"] library[location] = libeme return library