Created
March 5, 2014 23:14
-
-
Save raidzero/9378777 to your computer and use it in GitHub Desktop.
D-bus Audacious Pidgin Status Daemon
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/python | |
# Set pidgin status to currently playing audacious track with configurable display. | |
import re, dbus, time, sys | |
CONFIGFILE="~/.config/pypiaud.conf" | |
current_message = '' | |
def get_player_interface(): | |
return dbus.SessionBus().get_object('org.mpris.audacious', '/Player') | |
def get_tracklist_interface(): | |
return dbus.SessionBus().get_object('org.mpris.audacious', '/TrackList') | |
def get_purpleobject_interface(): | |
return dbus.SessionBus().get_object("im.pidgin.purple.PurpleService", "/im/pidgin/purple/PurpleObject") | |
def get_purple_interface(): | |
return dbus.Interface(get_purpleobject_interface(), "im.pidgin.purple.PurpleInterface") | |
def get_metadata(key, pos='default'): | |
result = 'Invalid' | |
valid_keys = ['album', 'artist', 'audio-bitrate', 'codec', 'genre', 'location', 'mtime', 'quality', 'title', 'tracknumber', 'year'] | |
for item in valid_keys: | |
if item == key: | |
try: | |
# unicode because some files have weird characters | |
if pos == 'default': | |
result = unicode(get_player_interface().GetMetadata()[key]) | |
else: | |
result = unicode(get_tracklist_interface().GetMetadata(pos)[key]) | |
except KeyError: | |
pass | |
if result == 'Invalid' or result == '': | |
if key == 'album': result = "Unknown Album" | |
if key == 'artist': result = "Unknown Artist" | |
if key == 'audio-bitrate': result == "Unknown Bitrate" | |
if key == 'codec': reuslt = "Unknown Codec" | |
if key == 'genre': result = "Music" | |
if key == 'location': result = "Unknown Location" | |
if key == 'mtime': result = "Unknown mtime" | |
if key == 'quality': result = "Unknown Quality" | |
if key == 'title': result = "Unknown Title" | |
return result | |
def get_year_string(): | |
year = get_metadata('year') | |
if year != 'Invalid' and year != '0': | |
year_string = ' (%s)' % year | |
else: | |
year_string = '' | |
return year_string | |
def get_entire_playlist(): # generator | |
for i in range(int(get_tracklist_interface().GetLength())): | |
p_artist = unicode(get_metadata('artist', i)) | |
p_album = unicode(get_metadata('album', i)) | |
p_title = unicode(get_metadata('title', i)) | |
yield ' %d - %s - %s - %s' % (i+1, p_artist, p_album, p_title) | |
def get_album_playlist(): | |
i = 0 | |
for item in get_entire_playlist(): | |
item_list = item.split(' - ') | |
print item_list | |
del item_list[1] # delete artist | |
item = ' - '.join(item_list) | |
print item | |
album_string = '%s -' % get_metadata('album') | |
if album_string in item: | |
i += 1 | |
item_name = re.sub('^\s*[0-9]+ ', '', item) | |
yield '%d - %s' % (i, item_name) | |
def get_album_position(): | |
for item in get_album_playlist(): | |
item_list = item.split(' - ') | |
#del item_list[1] # kill artist | |
del item_list[1] # kill album | |
item = ' - '.join(item_list) | |
if get_metadata('title') in item: | |
match = re.search('^(\d+)\s*', item) | |
if match: | |
current_position=match.group(1) | |
if current_position: | |
return current_position | |
def get_album_length(): | |
i = 0 | |
for item in get_album_playlist(): | |
i += 1 | |
return str(i) | |
def get_tracks_info(): | |
position = get_album_position() | |
total = get_album_length() | |
if total == 1: | |
# no need to show anything if there's only 1 trck in the album | |
return '' | |
if total != 'Invalid' and position != 'Invalid': | |
return '(Track %s/%s)' % (position, str(total)) | |
def get_player_status(): | |
# try, it may not even be running | |
try: | |
Player = get_player_interface() | |
except dbus.exceptions.DBusException: | |
return "Inactive" | |
# paused? | |
if Player.GetStatus()[0] == 1: | |
return "Paused" | |
# stopped? | |
elif Player.GetStatus()[0] == 2: | |
return "Stopped" | |
# playing a song! | |
else: | |
return "Playing" | |
def set_message(new_message, DEBUG=False): | |
global current_message | |
if current_message == new_message: | |
# don't do anything | |
return | |
if DEBUG: | |
print "(DEBUG) message set to: \"%s\"" % new_message | |
current_message = new_message | |
return | |
#current message is different from new_message | |
print "setting message to: \"%s\"" % new_message | |
try: | |
status = get_purple_interface().PurpleSavedstatusNew("", get_purple_interface().PurpleSavedstatusGetType(get_purple_interface().PurpleSavedstatusGetCurrent())) | |
# Create new transient status and activate it | |
get_purple_interface().PurpleSavedstatusSetMessage(status, new_message) | |
get_purple_interface().PurpleSavedstatusActivate(status) | |
current_message = new_message | |
except: | |
# pidgin must not be running or is being stupid, so just return | |
return | |
#debug loop, just print from dbus | |
if __name__ == '__main__': | |
while True: | |
Playing = get_player_status() | |
if Playing == 'Stopped' or Playing == 'Paused': | |
message_string = 'Music: %s' % Playing | |
if Playing == 'Inactive': | |
message_string = '' | |
if Playing == 'Playing': | |
# read the configuration file if it exists | |
""" | |
:config file legend: | |
%a = artist | |
%b = album | |
%y = year string ' (1996)' | |
will display nothing if no year found | |
%w = raw year '1996' | |
%t = title | |
%p = current track number | |
%z = last track number in album | |
%i = tracks string '(Track 1/12)' | |
will display nothing if no track info is found | |
%r = audio-bitrate | |
%d = codec | |
%g = genre | |
%l = location on disk | |
%m = mtime | |
%q = quality | |
some of these items will be unavailable on some files, and will result in 'Unknown ITEM' | |
""" | |
if len(sys.argv) > 1: | |
try: | |
with open(CONFIGFILE, 'r') as f: | |
for line in f: | |
line = line.rstrip() | |
if line.startswith('##'): | |
pass | |
else: | |
message_string = unicode(line.\ | |
replace('%a', get_metadata('artist')).\ | |
replace('%b', get_metadata('album')).\ | |
replace('%y', get_year_string()).\ | |
replace('%w', get_metadata('year')).\ | |
replace('%t', get_metadata('title')).\ | |
replace('%p', get_album_position()).\ | |
replace('%z', get_album_length()).\ | |
replace('%i', get_tracks_info()).\ | |
replace('%r', get_metadata('audio-bitrate')).\ | |
replace('%d', get_metadata('codec')).\ | |
replace('%g', get_metadata('genre')).\ | |
replace('%l', get_metadata('location')).\ | |
replace('%m', get_metadata('mtime')).\ | |
replace('%q', get_metadata('quality'))) | |
except IOError: | |
#use the default | |
message_string = '%s [%s%s] %s %s' % (get_metadata('artist'), get_metadata('album'), get_year_string(), get_metadata('title'), get_tracks_info()) | |
else: | |
message_string = '%s [%s%s] %s %s' % (get_metadata('artist'), get_metadata('album'), get_year_string(), get_metadata('title'), get_tracks_info()) | |
#print "(message) %s" % message_string | |
set_message(message_string, DEBUG=False) | |
current_message = message_string | |
time.sleep(3) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment