我正在尝试从节奏盒 python 插件中列出节奏盒数据库中的所有艺术家。我发现的唯一解决方案是让 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'
我想这样做的原因是因为我正在开发节奏盒的 telnet 接口,https://github.com/sameltvom/rhythmcurse。
很高兴输入!
亲切的问候,塞缪尔