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:
Copy the code into a file called rate.py
Start rhythmbox from a terminal using
rhythmbox -D rate
In Rhythmbox, enable the Python Console from the plugins
Start the Python Console and run
execfile('/path/to/rate.py')
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.