Question

I am trying to create a Python plugin which will set the rating of the currently playing song in Rhythmbox 2.96. It appears that Rhythmbox 2.96 doesn't let you use the API (Python modules) to set the rating of a song anymore; player related actions have been dropped in favor of MPRIS.

I then tried looking at using dbus with MPRIS but MPRIS doesn't have a spec for setting the rating of a song either. After a lot of digging, I found this sample in the Rhythmbox codebase and adapted it into a test script.

It works, but the SetEntryProperties method is causing Rhythmbox to freeze for about 30 seconds. Here is the Python script.


Instructions:

  1. Copy the code into a file called rate.py

  2. Start rhythmbox from a terminal using

    rhythmbox -D rate
    
  3. In Rhythmbox, enable the Python Console from the plugins

  4. Start the Python Console and run

       execfile('/path/to/rate.py')
    
  5. You will see the print output in the terminal and 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 gets 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 that error is occurring. I'd appreciate any help with it.

Also, if you know of a better way of setting the rating of a song in Rhythmbox through code, it would be welcome too!

I am using Ubuntu 12.04, if it makes a difference.

Was it helpful?

Solution

Setting the Rating within a plugin

Rhythmbox 2.9x does provide an API to set the Rating - there is no need to call via dbus unless you are using an external program such as the Rhythmbox Tray Icon.

Ratings are held as double type values in its internal database. Using a RhythmDBEntry you can obtain the Rating with

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

To set the rating you'll need the RhythmDB entry_set function:

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

Example code for getting and setting ratings can be found in the CoverArt Browser plugin (coverart_album.py)

OTHER TIPS

The Rhythmbox Tray Icon plugin on github does manage to set the song rating, but it does it from outside the Rhythmbox execution environment.

From 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"

If I tried to run that code from directly inside a Rhythmbox plugin, it would freeze again. However, running it from outside the Rhythmbox environment worked perfectly fine. I found this good enough so I'll mark this as the answer.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top