我正在尝试从rhythmbox python插件中列出rhythmbox数据库中的所有艺术家。我找到的唯一解决方案是让UI选择所有艺术家和所有歌曲,循环播放每首歌曲,并将该歌曲的艺术家名称添加到一组。
这个问题是(除了它的效率非常低效之外),我不想仅仅因为我想要一个数据库中所有艺术家的列表来改变所选择的艺术家。我之前尝试过保存选定的艺术家,这样我就可以在完成后恢复它,但由于UI需要一些时间来更新新信息和更多信息(即更多信息),因此会导致一些问题数据库),花费的时间越多。
可以使用提取代码 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 https://github.com/sameltvom/rhythmcurse开发一个telnet接口。
很高兴输入!
亲切的问候, 塞缪尔
答案 0 :(得分:1)
我找到了!如果我想列出所有条目而不管UI中选择了什么,那么我应该使用属性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。
然而,我仍然必须遍历所有歌曲才能找到所有艺术家。但主要问题已经消失。