Automatically Posting your Top 3 Artists from onto Twitter (with Python!)

I wrote this code a while back because a website that does the same thing seemed to miss my posts every other week.  So I figured I’d write my own in python to do the same thing to me.  Then I just put it into a cron job to automatically run it every Sunday.  I’m going to be posting the code on my GPL code page.  Here it is for you to see and for Google to index.  Just fill in the appropriate variables with the secret keys you get from each site’s API.


__author__ = “Eric Mesa”

__version__ = “v0.1”

__license__ = “GNU GPL v3.0”

__copyright__ = “(c) 2010 Eric Mesa”

__email__ = “ericsbinaryworld at gmail dot com”

import pylast

import twitter


API_KEY = “”







network = pylast.get_lastfm_network(api_key = API_KEY, api_secret = API_SECRET)

user = network.get_user(“username”)

topartists = user.get_top_artists(period=’7day’)

post = “My top 3 weekly #lastfm artists: %s (%s), %s (%s), %s (%s)” % (topartists[0].item, topartists[0].weight, topartists[1].item, topartists[1].weight, topartists[2].item, topartists[2].weight )

#twitter part

api = twitter.Api(consumer_key=CONSUMER_KEY,

consumer_secret=CONSUMER_SECRET, access_token_key=ACCESS_TOKEN_KEY, access_token_secret=ACCESS_TOKEN_SECRET)

status = api.PostUpdate(post)

Author: Eric Mesa

To find out a little more about me, see About Me