Python scripts: Post song to identi.ca & pidgin musictracker

Some days ago I wrote two python scripts: One that posts the current song beeing played by Banshee to identi.ca trought XMPP (using Pidgin) and a sort of musictracker for Pidgin also. Pidgin music tracker plugin crashes pidgin and it really does not work in Ubuntu Intrepid… or is it just me?

Both scripts are, well, pretty incomplete (thats being very nice to myself) and the code is not pretty, but I really didin”t have the time to improve them. Anyways, these where just to play around with python, dbus and to have fun! 🙂

These only work with Pidgin and Banshee, thought it’s pretty simple to make them work with other IM and Music players that have a dbus interface. I’ll post the scripts and instructions on how to use the, if there’s anyone crazy enought to use them :).

Post song to identi.ca

#!/usr/bin/env/python
 
# Copyright (C) 2009 Antonio Lima
# http://www.amrlima.info
# This program is free software; you can redistribute it and/or
# modify it under the terms of the GNU General Public License
# as published by the Free Software Foundation; either version 2
# of the License, or any later version.
 
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
 
import dbus
bus = dbus.SessionBus()
 
banshee = bus.get_object("org.bansheeproject.Banshee", "/org/bansheeproject/Banshee/PlayerEngine")
pidgin = bus.get_object("im.pidgin.purple.PurpleService", "/im/pidgin/purple/PurpleObject")
purple = dbus.Interface(pidgin, "im.pidgin.purple.PurpleInterface")
 
currentTrack = banshee.GetCurrentTrack()
 
artist = str(currentTrack[u'album-artist'])
album = str(currentTrack[u'album'])
track = str(currentTrack[u'name'])
 
trackinfo = " %s - %s  [%s]" %(track, artist, album )
print trackinfo
 
account = purple.PurpleAccountsFind('username@xmppserver.com/Home', '')
buddy = purple.PurpleFindBuddy(account, 'update@identi.ca')
PURPLE_CONV_TYPE_IM = 1
buddy_name = purple.PurpleBuddyGetName(buddy)
 
im = purple.PurpleConvIm(purple.PurpleConversationNew(PURPLE_CONV_TYPE_IM, account, buddy_name))
purple.PurpleConvImSend(im, "!listening to " + trackinfo)

If  you want to use this, you have to replace the username@xmppserver.com/Home field with your xmpp account (jabber, google-talk). You also need to have the XMPP activated in identi.ca. To run, just type:

$ python filename.py at the script directory.

This is a very simple script that gets the current track info from Banhsee and sends a message to update@identi.ca. It’s acctually very easy to add support to other music players that have a dbus interface. This is done using the python-dbus library.

Python musictracker

This was fun to code:

#!/usr/bin/env/python
 
# Copyright (C) 2009 Antonio Lima
# http://www.amrlima.info
# This program is free software; you can redistribute it and/or
# modify it under the terms of the GNU General Public License
# as published by the Free Software Foundation; either version 2
# of the License, or any later version.
 
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
 
import dbus, gobject
from dbus.mainloop.glib import DBusGMainLoop
loop = gobject.MainLoop()
 
DBusGMainLoop(set_as_default=True)
 
bus = dbus.SessionBus()
banshee = bus.get_object("org.bansheeproject.Banshee", "/org/bansheeproject/Banshee/PlayerEngine")
purple = bus.get_object("im.pidgin.purple.PurpleService", "/im/pidgin/purple/PurpleObject")
pidgin = dbus.Interface(purple, "im.pidgin.purple.PurpleInterface")
 
STATUS_AVAILABLE = dbus.UInt32(2)
STATUS_AWAY = dbus.UInt32(5)
 
def checkState(state):
	currentTrack = banshee.GetCurrentTrack()
	print "State Changed: %s" % state
	if state == 'playing':
		artist = str(currentTrack[u'album-artist'])
		album = str(currentTrack[u'album'])
		track = str(currentTrack[u'name'])
		trackinfo = " %s - %s  [%s]" %(track, artist, album)
		old_status = purple.PurpleSavedstatusGetCurrent()		# get current status
		status_type = purple.PurpleSavedstatusGetType(old_status)	# get current status type
		new_status = purple.PurpleSavedstatusNew("", STATUS_AVAILABLE)	# create new status with old status type
		purple.PurpleSavedstatusSetMessage(new_status, trackinfo)	# fill new status with status message
		purple.PurpleSavedstatusActivate(new_status, trackinfo)		# activate new status
	if state == 'paused':
		old_status = purple.PurpleSavedstatusGetCurrent()		# get current status
		status_type = purple.PurpleSavedstatusGetType(old_status)	# get current status type
		new_status = purple.PurpleSavedstatusNew("", STATUS_AWAY)	# create new status with old status type
		purple.PurpleSavedstatusSetMessage(new_status, "Banshee is paused. I'm probably not here now")	# fill new status with status message
		purple.PurpleSavedstatusActivate(new_status)			# activate new status
 
bus.add_signal_receiver(checkState, dbus_interface="org.bansheeproject.Banshee.PlayerEngine", signal_name="StateChanged")
 
mainloop = gobject.MainLoop()
mainloop.run()

This is very incomplete. It has some stupid problems which I still didn’t have time or motivation to fix. One of them is really stupid for a music tracker: After running the script, you need to pause and resume Banshee in order the script picks the track info and set your status message. Or you can wait untill the next song starts :).

It’s hardcoded to set the satus to “available” while playing and “away” while paused. It picks up the chages in the music player.

Non ASCII characters in track info will give an error (I’m going to try to fix it) but if the next song is ASCII it will continue all right.

Long post! If someone (crazy people) tries these scripts and runs into trouble let me know! Maybe I can help… or not :).

EDIT: To run the scrip without having to type: $python musictracker.py every time at the command line, you can create a very simple shell script:

#!/bin/sh
/usr/bin/python /scriptlocation/musictracker.py

Make it executable and if you use GNOME-DO or deskbar-applet they will index it and you can just tipe the script name and there you go, the music tracker script runs! Don’t forget to replace “scriptlocation” with the scriptl location… :).

I’m currently working (slowly) on some improvements to the script. But more on that latter.

Comments are welcome!

#banshee#dbus#identica#pidgin#pythoon#scripts

Comments

  1. escorts - 22 de Setembro de 2014 @ 14:48

    This site certainly has all of the information and facts I needed
    concerning this subject and didn’t know who to ask.

Deixe uma resposta

Este site utiliza o Akismet para reduzir spam. Fica a saber como são processados os dados dos comentários.