01-04-2008

Nerd bytes

Se me habia olvidado ya que tenia un blog nerdyyii...

Os dejo un scripcillo que se conecta a un music player daemon y publica lo que se esta todando ahi, en una cuenta MSN que este siendo usada por algun programa que use telepatia.

Probablemente sea buena idea transformarlo en un plugin...

simple, preciso y feo.. :P


#!/usr/bin/env python

import dbus
import time
import mpdclient
import unicodedata

#Configs
MSN_ACCOUNT="your_msn_account@hotmail.com"
STRING="%s: %s [%s] Some more strings..."
NO_SONG_NAME="When no tag can be found in the song"
SLEEP=3
HOST='host.of.your.mpd'
PORT=6600

#Globals
NAME='org.freedesktop.Telepathy.Connection.butterfly.msn.'+ MSN_ACCOUNT.replace('@','_40').replace('.','_2e')
SERVER='/org/freedesktop/Telepathy/Connection/butterfly/msn/'+MSN_ACCOUNT.replace('@','_40').replace('.','_2e')
msn = None
mpd = None
last_song = None

def connect_dbus():
global msn
bus = dbus.SessionBus()
msn = bus.get_object(NAME, SERVER)

def connect_mpd():
global mpd
try:
mpd = mpdclient.MpdController(HOST, PORT)
except mpdclient.MpdConnectionPortError, e:
print "Cannot connect: %s" %e

def song_name(song):
if (len(song.artist)<2):
return NO_SONG_NAME
utf8 = STRING %(song.artist,song.title,song.album)
out = ""
for s in utf8.decode('utf-8'):
dec = unicodedata.decomposition(s)
if (len(dec)<1):
out += s
else:
out += chr(int(dec.split(' ')[0],16))
return out

while (1):

time.sleep(SLEEP)

try:
current_song = mpd.getCurrentSong()

except (mpdclient.MpdStoredError), e:
print e
continue
except (mpdclient.MpdNoResponseError, AttributeError, mpdclient.MpdError), e:
print "Not connection to mpd, reconnecting..."
connect_mpd()
continue

if current_song is False:
print "Nothing to see.."
continue

song = song_name(current_song)
if (last_song==song):
continue

print "New song: %s" %song
last_song = song

try:
msn.SetAliases( {1: song} )
except (dbus.exceptions.DBusException, AttributeError), e:
print "No connection to dbus, reconnecting..."
connect_dbus()
last_song = None