Plugins: rhythmdbloader.py

File rhythmdbloader.py, 3.9 kB (added by eigenlambda@gmail.com, 1 year ago)

Loads your preexisting Rhythmbox library into quodlibet

Line 
1 #!/usr/bin/python
2 # RhythmDB Loader by Thomas Folz-Donahue
3 # This is GPL free software
4
5 import sys, string
6 from xml.dom import minidom
7 import gtk
8 from library import library
9 import config
10 import util.uri
11 from plugins.songsmenu import SongsMenuPlugin
12 from functools import partial
13 import urllib
14 import os.path
15
16 try: config.get("plugins", __name__)
17 except: config.set("plugins", __name__, False)
18
19
20 def passfun(key, value, name=None, pre=False, kind="string"):
21     if name != None:
22         key = name
23     elif pre:
24         key = "~#"+key.encode('ascii')
25     else:
26         key = key.encode('ascii')
27     if kind=="int":
28         value = int(value)
29     elif kind=="float":
30         value = float(value)
31     return (key,value)
32
33 song_import = {u'album':passfun,
34                u'rating':partial(passfun,pre=True,kind="float"),
35                u'artist':passfun,
36                u'first-seen':partial(passfun,name='~#added'),
37                u'title':passfun,
38                u'play-count':partial(passfun,name='~#playcount',kind="int"),
39                u'last-played':partial(passfun,name='~#lastplayed', kind='int'),
40                u'mtime':partial(passfun,pre=True,kind='int'),
41                u'genre':passfun,
42                }
43
44
45
46
47 class rhythmdbloader(SongsMenuPlugin):
48     PLUGIN_ID = "rhythmdbloader"
49     PLUGIN_NAME = _("RhythmDB Loader")
50     PLUGIN_DESC = _("Import your preexisting RhythmDB database")
51     PLUGIN_VERSION = "0.1"
52     PLUGIN_ICON = 'gtk-open'
53
54
55     @classmethod
56     def PluginPreferences(self, parent):
57         overwrite_button = gtk.CheckButton(_("overwrite QuodLibet data with RhythmDB data in case of conflicts"))
58         overwrite_button.connect('toggled', self.set_overwrite_mode)
59         justdoit_button = gtk.Button(_("import all songs now"))
60         justdoit_button.connect('pressed', self.import_all)
61         vbox = gtk.VBox()
62         vbox.add(overwrite_button)
63         vbox.add(justdoit_button)
64         return vbox
65
66     @classmethod
67     def set_overwrite_mode(self, button):
68         config.set("plugins", __name__, button.get_active())
69
70    
71     @classmethod
72     def import_all(self, foo):
73         rhythmdb = loadrhythmdb(os.path.expanduser("~/.gnome2/rhythmbox/rhythmdb.xml"))
74         for rdb_loc in rhythmdb:
75             if rdb_loc.startswith("file://"):
76                 try:
77                     ql_loc = util.uri.URI(rdb_loc).filename
78                 except ValueError:
79                     print "bad uri- %s"%rdb_loc
80                     continue
81             else:
82                 ql_loc = rdb_loc
83             if not os.path.isfile(ql_loc):
84                 print "%s - not a file, ignoring"%rdb_loc
85                 continue
86             if not config.get("plugins", __name__):
87                 if ql_loc in library:
88                     continue
89             libeme = {}
90             for key in rhythmdb[rdb_loc]:
91                 try:
92                     k,v = song_import[key](key,rhythmdb[rdb_loc][key])
93                     libeme[k] = v
94                 except KeyError:
95                     continue
96             library.add_filename(ql_loc)
97             if ql_loc in library:
98                 for key in libeme:
99                     library[ql_loc][key] = libeme[key]
100        
101 def loadrhythmdb(rhythmdbfile):
102     rhythmdb = minidom.parse(rhythmdbfile)
103     library = {}
104     entries = rhythmdb.getElementsByTagName("entry")
105     for entry in entries:
106         if entry.getAttribute("type") != u"song":
107             continue # not supporting ignore, iradio yet
108         libeme = {}
109         for child in entry.childNodes:
110            
111             if child.nodeType != minidom.Node.ELEMENT_NODE:
112                 continue
113             try:
114                 libeme[child.tagName] = child.firstChild.data
115             except AttributeError:
116                 continue
117         try:
118             location = libeme[u"location"]
119         except KeyError:
120             continue
121         del libeme[u"location"]
122         library[location] = libeme
123     return library