如何在rhythmbox插件中列出所有艺术家
我正在尝试从rhythmbox的数据库中列出所有艺术家,这个操作是在一个rhythmbox的python插件里进行的。我找到的唯一解决办法是让界面选择所有艺术家和所有歌曲,然后逐首遍历每首歌,把每首歌的艺术家名字加到一个集合里。
这样做的问题是(除了效率低下之外),我不想因为想要获取数据库中所有艺术家的列表而改变当前选中的艺术家。我之前尝试过保存当前选中的艺术家,以便完成后能恢复,但这会导致一些问题,因为界面更新新信息需要时间,而数据库里的信息越多(比如歌曲越多),所需的时间就越长。
你可以通过以下命令获取代码: git clone git@github.com:sameltvom/dblister.git
以下是代码:
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'
我想这样做的原因是因为我正在为rhythmbox开发一个telnet接口,https://github.com/sameltvom/rhythmcurse。
欢迎大家提供意见!
祝好,
Samuel
1 个回答
1
我找到了!如果我想列出所有的条目,不管在界面上选择了什么,我应该使用属性 base_query_model。
现在代码看起来是这样的:
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'
我还发现了另一个好东西。如果我使用 elf.shell.props.library_source.props.base_query_model,而不是 self.shell.props.selected_source.props.base_query_model,即使我可能在左侧面板把视图切换到了比如 Last.FM 或 Radio,我仍然能得到输出。
不过,我还是需要遍历所有的歌曲来找到所有的艺术家。但主要的问题已经解决了。