How to set a song rating in Rhythmbox 2.96?

I am trying to create a Python plugin that will set the rating of the current song in Rhythmbox 2.96. It seems that Rhythmbox 2.96 does not allow you to use APIs (Python modules) to set the song rating more; player-related actions were dropped in favor of MPRIS.

Then I tried to use dbus with MPRIS, but MPRIS has no specification for setting the song rating. After a lot of digging, I found this sample in the Rhythmbox database and adapted it into a test script.

This works, but the SetEntryProperties method causes the Rhythmbox to freeze for about 30 seconds. Here is a Python script.


Instruction:

  • Copy the code into a file called rate.py

  • Start the rhythm box from the terminal using

    rhythmbox -D rate
    
  • In Rhythmbox enable Python Console from plugins

  • Launch the Python console and run

       execfile('/path/to/rate.py')
    
  • You will see the printout in the terminal, and the Rhythmbox freezes for about 20-30 seconds.


# 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()

The exception that is printed is:

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

My knowledge of Python / dbus is limited, so I don't understand why this error occurs. I would appreciate any help with this.

Also, if you know the best way to rate a song in Rhythmbox via code, that would be a plus too!

I use Ubuntu 12.04 if that matters.

+5
source share
2 answers

Rating adjustment inside the plugin

Rhythmbox 2.9x provides an API for setting the rating - there is no need to call via dbus if you are not using an external program such as the Rhythmbox tray icon.

. RhythmDBEntry,

rating = entry.get_double(RB.RhythmDBPropType.RATING)

, rhythmDB entry_set:

db=self.shell.props.db
db.entry_set(entry, RB.RhythmDBPropType.RATING, rating)

CoverArt Browser (coverart_album.py)

+3

Icon Rhythmbox Tray github , Rhythmbox.

:

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 . , .

+1

All Articles