This is a very small script to display Rhythmbox' currently playing song's rating in a notification. It also allows to change the rating.
This script is placed in the public domain. No copyright, just copy it, it's trivial.
Usage: ./rhrating.py [NEWRATING 0..5]
#!/usr/bin/python
# -*- encoding: UTF-8 -*-
import os
import sys
import dbus
bus = dbus.Bus()
program = "Rhythmbox"
service_name = "org.gnome.%s" % program.title()
pobj_name = "/org/gnome/%s/Player" % program.title()
piface_name = "org.gnome.%s.Player" % program.title()
sobj_name = "/org/gnome/%s/Shell" % program.title()
siface_name = "org.gnome.%s.Shell" % program.title()
def get_playing_uri():
searchobj = bus.get_object(service_name, pobj_name)
player = dbus.Interface(searchobj, piface_name)
playing_uri = player.getPlayingUri()
return playing_uri
def get_properties(uri):
searchobj = bus.get_object(service_name, sobj_name)
shell = dbus.Interface(searchobj, siface_name)
props = shell.getSongProperties(uri)
return dict(props)
print "Song %s rated %s" % ( props["title"], props["rating"])
def set_rating(uri, rating):
searchobj = bus.get_object(service_name, sobj_name)
shell = dbus.Interface(searchobj, siface_name)
shell.setSongProperty(uri, "rating", float(rating))
def get_rating_string(props):
skull = u'\u2620'
star = u'\u2605'
rating = int(props["rating"])
rstr = skull if rating == 1 else star*rating
return rstr
def send_rating_notification(info, stars=None, time=2000):
if not stars:
info["stars"] = get_rating_string(info)
else:
info["stars"] = stars
info["time"] = time
cmd = (u'notify-send -t %(time)s -i audio-x-generic -- "%(title)s %(stars)s"'
u' "by <i>%(artist)s</i> from <i>%(album)s</i>"') % info
pipe = os.popen(cmd.encode("UTF-8"))
pipe.read()
if __name__ == '__main__':
uri = get_playing_uri()
if not uri:
raise SystemExit
if len(sys.argv) > 1:
rating = int(sys.argv[1])
bprops = get_properties(uri)
rbefore = get_rating_string(bprops)
set_rating(uri, rating)
aprops = get_properties(uri)
rafter = get_rating_string(aprops)
send_rating_notification(aprops, u"%s \u2192 %s" % (rbefore, rafter))
else:
send_rating_notification(get_properties(uri))