【问题标题】:How can I set the rating of a song playing in Rhythmbox 2.96?如何设置在 Rhythmbox 2.96 中播放的歌曲的评分?
【发布时间】:2012-05-21 01:37:43
【问题描述】:

我正在尝试创建一个 Python 插件,该插件将在 Rhythmbox 2.96 中设置当前播放歌曲的评分。 Rhythmbox 2.96 似乎不再允许您使用 API(Python 模块)来设置歌曲的评分;已放弃与播放器相关的操作,转而支持 MPRIS。

然后我尝试查看将 dbus 与 MPRIS 结合使用,但 MPRIS 也没有设置歌曲评级的规范。经过大量挖掘,我在 Rhythmbox 代码库中找到了this sample,并将其改编为测试脚本。

它有效,但 SetEntryProperties 方法导致 Rhythmbox 冻结大约 30 秒。这是 Python 脚本。


说明:

  1. 将代码复制到名为 rate.py 的文件中

  2. 使用

    从终端启动节奏盒
    rhythmbox -D rate
    
  3. 在 Rhythmbox 中,通过插件启用 Python 控制台

  4. 启动 Python 控制台并运行

       execfile('/path/to/rate.py')
    
  5. 您将在终端中看到打印输出,并且 Rhythmbox 会冻结大约 20-30 秒。


# rhythmbox -D rate
# Rhythmbox: Edit > Plugins > Python Console enabled
# Play a song
# Open Rhythmbox Python Console
# execfile('/path/to/rate.py')

import sys
import rb
from gi.repository import Gtk, Gdk

def rateThread(rating):
        try:
            currentSongURI = shell.props.shell_player.get_playing_entry().get_playback_uri()
            print "Setting rating for " + currentSongURI

            from gi.repository import GLib, Gio
            bus_type = Gio.BusType.SESSION
            flags = 0
            iface_info = None

            print "Get Proxy"
            proxy = Gio.DBusProxy.new_for_bus_sync(bus_type, flags, iface_info,
                                                   "org.gnome.Rhythmbox3",
                                                   "/org/gnome/Rhythmbox3/RhythmDB",
                                                   "org.gnome.Rhythmbox3.RhythmDB", None)

            print "Got proxy"
            rating = float(rating)
            vrating = GLib.Variant("d", rating)
            print "SetEntryProperties"
            proxy.SetEntryProperties("(sa{sv})", currentSongURI, {"rating": vrating})
            print "Done"
        except:
            print sys.exc_info()

        return False

def rate():
        if shell.props.shell_player.get_playing_entry():
            Gdk.threads_add_idle(100, rateThread, 3)

rate()

打印出来的异常是:

 Desktop/test2.py:41: (<class 'gi._glib.GError'>, GError('Timeout was
 reached',),  <traceback object at 0x913e554>)

我对 Python/dbus 的了解有限,所以我不明白为什么会发生该错误。如有任何帮助,我将不胜感激。

另外,如果您知道通过代码在 Rhythmbox 中设置歌曲评分的更好方法,也欢迎!

我使用的是 Ubuntu 12.04,如果它有所作为的话。

【问题讨论】:

  • 刚刚看到你在我的问题上留下的评论。我对此感兴趣...如果您今晚还没有发现,我保证会搜索它(当然是出于我自己的自私原因:))。如果您已经知道,请回答您自己的问题,以便其他人可以看到解决方案。 :D
  • @Mamsaac - 谢谢你提醒我,我已经更新了答案,虽然不完美。

标签: python ubuntu gio rhythmbox


【解决方案1】:

在插件中设置评分

Rhythmbox 2.9x 确实提供了一个 API 来设置 Rating - 除非您使用 Rhythmbox Tray Icon 等外部程序,否则无需通过 dbus 调用。

评级在其内部数据库中保存为双精度类型值。使用 RhythmDBEntry,您可以获得评分

评级 = entry.get_double(RB.RhythmDBPropType.RATING)

要设置评分,您需要 RhythmDB entry_set 函数:

db=self.shell.props.db db.entry_set(条目,RB.RhythmDBPropType.RATING,评级)

获取和设置评分的示例代码可以在CoverArt Browser 插件 (coverart_album.py) 中找到

【讨论】:

    【解决方案2】:

    github 上的 Rhythmbox Tray Icon plugin 确实可以设置歌曲评级,但它是在 Rhythmbox 执行环境之外进行的

    来自here

    def SetSongRating(self, rating):
        """
        Sets the current song rating in Rhythmbox.
        """
    
        try:
            currentSongURI = self.GetSongURI()
    
            if currentSongURI:
    
                busType = Gio.BusType.SESSION
                flags = 0
                ratingInterface = None
    
                proxy = Gio.DBusProxy.new_for_bus_sync(busType, flags, ratingInterface,
                                                       "org.gnome.Rhythmbox3",
                                                       "/org/gnome/Rhythmbox3/RhythmDB",
                                                       "org.gnome.Rhythmbox3.RhythmDB", None)
    
                variantRating = GLib.Variant("d", float(rating))
                proxy.SetEntryProperties("(sa{sv})", currentSongURI, {"rating": variantRating})
        except:
            print "Failed to set a rating"
    

    如果我尝试直接从 Rhythmbox 插件中运行该代码,它会再次冻结。但是,从 Rhythmbox 环境之外运行它工作得非常好。我发现这已经足够好了,所以我将其标记为答案。

    【讨论】:

      猜你喜欢
      • 2012-08-10
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2023-04-03
      • 1970-01-01
      相关资源
      最近更新 更多