How To Tweets Your Current iTunes Track

Well, it’s nothing. Last Saturday I’ve stuck in office, have nothing to do but arranging my iTunes collection. Been awake since Friday night, and it seems my sleep disorder haven’t been healed yet :( So while i’m twittering i want also to use my current track played in my iTunes to be posted in twitter.

I remember i’ve made a small python script to do this couple years ago but lost the source code *facepalm* Hmmm, I think i gave the codes to @cothat and @rara79. But it will be rude to wake up those guys just for asking source codes :P

I rewrite it all over again, just to get me bored and hopefully sleep after that :P

Anyway, these are the codes that make it happen :)

You can make it simple and more robust though. I don’t put any exception to check whether my iTunes is running or not (yet) to prevent flooding my timeline with probably blank message :P

My previous version will run a loop and sleep couple of seconds. And within that loop it will check is the iTunes playing same song as previous or not. It will post to twitter if new song is detected :) Useful if you like to skip song in the middle.

[cc lang=’python’ ] “””
simple Python Script to get current track being played in iTunes
and tweet it (and also update your FB status)
– py-appscript, Control AppleScriptable applications from Python
– tweepy, Twitter API for Python

Author: Nuri Abidin
Date: 2012-02-04
from appscript import *
from decimal import *
from ConfigParser import ConfigParser
from optparse import OptionParser
from sys import exit
from time import strftime, sleep, localtime
import string, os, re, sys, tweepy

class nuyTwiTunes:
def __init__(self, config_file=””):
config = self.getconf(config_file)
self.twitter_consumer_key = config[‘consumer_key’] self.twitter_consumer_secret = config[‘consumer_secret’] self.twitter_access_token = config[‘access_token’] self.twitter_token_secret = config[‘token_secret’] self.print_console(‘Initiating the application…’)

# just printing formatted text to console
def print_console(self, s):
t = strftime(‘%Y-%m-%d – %X’)
print “%s %s” % (t,s)

# read the config file
def getconf(self, config_file):
param = {}
c = os.path.expanduser( config_file )

if not os.path.exists(c):
self.print_console(‘ERROR: No configuration file: %s’ % c)

conf = ConfigParser()

#read config items to dictionary
for section in conf.sections():
for items in conf.items(section):
param[items[0]] = items[1] return param

# ‘read’ the current track being played in iTunes
def get_itunes_track(self):
myApp = app(‘iTunes’)
mySong = (“%s – %s” % (myApp.current_track.artist(), )
songTime = myApp.current_track.time();
timeChunks = string.split(songTime, “:”)
playingTime = (Decimal(timeChunks[0]) * 60) + Decimal(timeChunks[1])
return mySong, playingTime

# post to twitter
def post_tweet(self, the_tweet=”Hai ^_^”):
auth = tweepy.OAuthHandler(self.twitter_consumer_key, self.twitter_consumer_secret)
auth.set_access_token(self.twitter_access_token, self.twitter_token_secret)
api = tweepy.API(auth)

# post current iTunes track to twitter
def tweet_the_tunes(self):
# play and sleep until track changes
while True:
(track_name, play_time) = self.get_itunes_track()
tweet = “iPlay: %s ^_^ #fb” % (track_name)
self.print_console(“%s –%s seconds–” % (tweet,play_time) )

def main():
usage = “usage: %prog [options]”
parser = OptionParser(usage=usage)
parser.add_option(‘-c’, ‘–config’,
(options, args) = parser.parse_args()

if options.config_file:
config_file = options.config_file
config_file = “./nuy_tunes.conf”

my_tunes = nuyTwiTunes( config_file )

if __name__ == “__main__”:

and the configuration file is simply like this

[cc lang=’python’ ] [twitter] consumer_key = your_twitter_consumer_key
consumer_secret = your_twitter_consumer_secret
access_token = your_twitter_access_token
token_secret = your_twitter_token_secret

Once more thing …
You need to use a Mac to do this :P
Having one is a good thing :P

and blogging this script make me feel sleepy already …

Leave a Reply

Next ArticleHello World, Nikon D800