How to list all artists in a rhythmbox plugin
I'm trying to list all artists in the rhythmbox database from within a rhythmbox python plugin. The only solution I have found is to make the UI select all artists and all songs, loop through every song and add that song's artist name to a set.
The problem with this is (beside from the fact it's painfully inefficient) that I don't want to change the selected artist just because I want a list of all the artists in the database. I tried earlier to save the selected artist so that I could restore it when I'm done but that causes some problems due to the fact that the UI takes some time to be updated with the new information and the more information (i.e. more songs in the database), the more time it takes.
The code can be fetched with git clone git@github.com:sameltvom/dblister.git
Here is the code:
import rb
import rhythmdb
import gtk
class DblisterPlugin (rb.Plugin):
def __init__(self):
rb.Plugin.__init__(self)
def activate(self, shell):
self开发者_运维问答.shell = shell
print '##### dblister #####'
# choose all artists, this will choose all albums and songs as well
# get the lock for rhythmbox ui
gtk.gdk.threads_enter()
for p in self.shell.props.library_source.get_property_views():
if p.props.prop == rhythmdb.PROP_ARTIST:
p.set_selection([""])
break
gtk.gdk.threads_leave()
##################### Print all artists in database ######################
# loop through all songs currently selected (i.e. all songs since we did p.set_selection([""]) above
# for each song, try to add the artist name to the 'artists' set
artists = set() # unique keys, no duplicates
for row in self.shell.props.selected_source.props.query_model:
entry = row[0]
artist = self.shell.props.db.entry_get(entry, rhythmdb.PROP_ARTIST)
artists.add(artist)
print '--- artists ---'
for artist in artists:
print artist
##################### Print all songs in database ######################
print '--- songs ---'
# loop through all songs currently selected (i.e. all songs since we did p.set_selection([""]) above
# for each song, print artist name and title
for row in self.shell.props.selected_source.props.query_model:
entry = row[0]
artist = self.shell.props.db.entry_get(entry, rhythmdb.PROP_ARTIST)
song = self.shell.props.db.entry_get(entry, rhythmdb.PROP_TITLE)
print artist + ' - ' + song
def deactivate(self, shell):
del self.shell
print 'Bye world'
The reason I want to do this is because I'm developing a telnet interface to rhythmbox, https://github.com/sameltvom/rhythmcurse.
Happy for input!
Kind regards, Samuel
I found it! It was the property base_query_model I should use if I want to list all entries regardless of what is selected in the UI.
The code now looks like:
import rb
import rhythmdb
import gtk
class DblisterPlugin (rb.Plugin):
def __init__(self):
rb.Plugin.__init__(self)
def activate(self, shell):
self.shell = shell
print '##### dblister #####'
#################### Print all artists in the library ####################
artists = set() # unique keys, no duplicates
for row in self.shell.props.library_source.props.base_query_model:
entry = row[0]
artist = self.shell.props.db.entry_get(entry, rhythmdb.PROP_ARTIST)
artists.add(artist)
print '--- artists using library_source---'
for artist in artists:
print artist
del artists
##################### Print all artists in database ######################
artists = set() # unique keys, no duplicates
for row in self.shell.props.selected_source.props.base_query_model:
entry = row[0]
artist = self.shell.props.db.entry_get(entry, rhythmdb.PROP_ARTIST)
artists.add(artist)
print '--- artists ---'
for artist in artists:
print artist
##################### Print all songs in database ######################
print '--- songs ---'
for row in self.shell.props.selected_source.props.base_query_model:
entry = row[0]
artist = self.shell.props.db.entry_get(entry, rhythmdb.PROP_ARTIST)
song = self.shell.props.db.entry_get(entry, rhythmdb.PROP_TITLE)
print artist + ' - ' + song
def deactivate(self, shell):
del self.shell
print 'Bye world'
I also found another nice thing as well. If I use elf.shell.props.library_source.props.base_query_model instead of self.shell.props.selected_source.props.base_query_model I will still get output even though I might have changed view to e.g. Last.FM or Radio in the left side pane.
However, I still must loop through all songs to find all artists. But the main problem is gone.
精彩评论