3

我正在尝试从节奏盒 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

很高兴输入!

亲切的问候,塞缪尔

4

1 回答 1

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窗格。

但是,我仍然必须遍历所有歌曲才能找到所有艺术家。但是主要问题已经消失了。

于 2011-01-19T20:55:33.027 回答