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.
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)
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With