Thursday, March 4, 2010

Playing with Dbus & Songbird.... (Artwork Downloader for Songbird or any other Player supporting Dbus Interface)

Songbird comes from same family Firefox and Thunderbird. So, it would never disappoint. For everything you want, there is a addon. But, it still lacks one thing. It doesnt download the Album-Artwork automatically. When I first started using this, to add the artwork I used to search on GOOGLE IMAGES and then add them automatically.

But, then i thought its waste of time searching for each and every Album manually.

It just came to my mind that why not use DBus interface of songbird to get the current playing song TITLE and find its Album using an online service & then ultimately get its album-art.

My favourite being last.fm, I rushed to google homepage and searched for "last.fm API python" and got the result "pylast". So, i was going to use "pylast" for all my programming.

Basically i wrote a python script that contacts the Songbird for the current playing song title and artist, finds its Album through the last.fm provvided API, then downloads the corresponding ARTWORK available at last.fm... In addition, it also displays information like Album Release date.

I just love python. It could do all this in just 30 lines code....

#!/usr/bin/python

import pylast
import getpass
import dbus
import eyeD3
import subprocess
import string
bus = dbus.SessionBus()
player_obj = bus.get_object('org.mpris.songbird', '/Player')
status = player_obj.GetStatus()

if status[0] == 0:
 print "Songbird is playing...."

current_playing_track = player_obj.GetMetadata()

print 'Title: '+current_playing_track['title']+'\nArtist: '+current_playing_track['artist']

API_KEY="0381676636c5a36935b49595b352ad98"
API_SECRET="6c5bd32fd666b03f21fb3446ef559a9a"
username = "shadyabhi"
#password = getpass.getpass()
password_hash = '1552c1c2bee2fc2f472e02269417ac33'
network = pylast.get_lastfm_network(api_key = API_KEY, api_secret = API_SECRET, username = username, password_hash = password_hash)

#Getting Album Info
track = network.get_track(current_playing_track['artist'],current_playing_track['title'])
album = track.get_album()
if album is not None:
 print "Album: "+album.get_name()
 print "Release Date: "+album.get_release_date()
 album_uri = album.get_cover_image(size=3)

if album is None:
 album_uri = track.get_artist().get_cover_image()
 print "ALBUM Art could not be found on last.fm :(. Now, downloading Artist Image"


print album_uri
#Downloading the Album Art from last.fm
cmd = "wget "+album_uri+" -O artwork."+album_uri[-3:]
ret = subprocess.call(cmd.split())


"""
#Editing the MetaData
tag = eyeD3.Tag()
print tag.link(string.replace(string.replace(str(current_playing_track['location'])[7:],"%20"," "),"%27","'"))
tag.setVersion([2,3,0])
print tag.addImage(0x08,'artwork.jpg')
print tag.update()
"""

Future plans that I am gonna make Songbird addon, that also embeds the artwork into the mp3 file.

2 comments:

  1. And yes, make sure that you have the MPRIS addon installed in Songbird to have Songbird support the Dbus interface...

    ReplyDelete
  2. Great stuff! 'borrowing' your code right now...

    ReplyDelete