Python Script to tweet scrobbles

A silly conceit, but fun. The script is pretty simple (read: I’m an unsophisticated programmer), but it works. Because it hooks into, anything you scrobble (Roon, Spotify, iTunes, your player of choice) shows up.

I run it manually right now, but it could be cron’d.

#!/usr/bin/env /Library/Frameworks/Python.framework/Versions/3.7/bin/python3

# Installed Python 3.7 for Mac from

import os
import json
import sys
import twitter # Installed python-twitter (
import requests

# Twitter API Setup -- the twitter API information has to come from your Twitter account

consumer_key = '[Twitter Consumer Key]'
consumer_secret = '[Twitter Consumer Secret]'
access_key = '[Twitter Access Key]'
access_secret = '[Twitter Access Secret]'
api = twitter.Api(consumer_key=consumer_key, consumer_secret=consumer_secret,
                      access_token_key=access_key, access_token_secret=access_secret)
# Files we use -- this is where my files live, but you will need to use your own filepath

scrobbleDir =  os.path.join('/', 'Users', 'onlylooksharmless', 'Documents','Scripts', 'Scrobble to Twitter')
lastScrobbleFileLoc = os.path.join(scrobbleDir, 'lastscrobble.txt')
tweetErrFileLoc = os.path.join(scrobbleDir, 'tweeterr.txt')

# Get last date from stored file, increment by one, reset to string
# Consider using shelve module ( in next version
# The very first time you run it, I'd recommend using a browser to pull the last NN scrobbles, and you can pick out a UTS to put in the file

lastScrobbleFile = open(lastScrobbleFileLoc)
startTime =
startTime = str(int(startTime) + 1)

# Get Scrobbles and set up iteration
# Block Comment is what I used to test against a downloaded JSON file

'''samplePath = os.path.join('/', 'Users', 'onlylooksharmless', 'Documents', 'Scripts', 'Scrobble to Twitter', 'Dev Process', 'Sample Scrobble.json')
sampleFile = open(samplePath)
scrobbleListAsPython = json.loads(scrobbleList)'''

# API call using startTime
# It works to increment last dateStamp by 1, append &from=[incremented] at end
# You have to get your own user name and api key 

scrobbleURL ='[USERNAME]&api_key=[APIKEY]&format=json&from=%s' % (startTime)
	scrobbleList = requests.get(scrobbleURL)
	tweetErrFile = open(tweetErrFileLoc, 'a')
	tweetErrFile.write(' unavailable')
scrobbleListAsPython = json.loads(scrobbleList.text) # I used JSON for no better reason that it's what "Automate the Boring Stuff worked with."
trackList = scrobbleListAsPython['recenttracks']['track']
trackListLen = len(trackList)

# Check to make sure Twitter is up; bail if not

	twitterTest = requests.get('')
	tweetErrFile = open(tweetErrFileLoc, 'a')
	tweetErrFile.write('twitter unavailable')

# Create the blank tweet list
# Goal of creating uniques is to reduce Twitter duplicate tweet errors
# Maybe use shelve module in future version to keep a history of NN tweets

# Setting up blanks here (a) prevents erroring out, and (b) allows for a test below

tweetList = []
dateStamp = ''

# Iterate in reverse chron order through scrobbles to create a list of (for this session) uniques

for i in reversed(range(trackListLen)): 
	currentTrack = trackList[i]
	currentAlbum = currentTrack['album']['#text']
	currentArtist = currentTrack['artist']['#text']
	currentName = currentTrack['name']
	currentURL = currentTrack['url']
	dateStamp = currentTrack['date']['uts'] #this already shows as a string, so no need to convert
	nowPlaying = ('#nowplaying ' + currentArtist + ' – \"' + currentName + '\" from \"' + currentAlbum + '\"' + ' ' + currentURL)
	if nowPlaying not in tweetList:

# Then run through the list in the new order -- it's now chronologically ordered
for i in range(len(tweetList)):
		status = api.PostUpdate(tweetList[i])
		print("{0} just posted: {1}".format(, status.text))
		tweetErrFile = open(tweetErrFileLoc, 'a')
	# I was running 1 minute intervals to get through the first block of scrobbles, and Twitter assumed it was spam after about 40
	# Could have been extenuating circumstances, but two minutes seems to work. 

# Stash the last scrobble datestamp (using the blank dateStamp to avoid blanking the file datestamp if there's no scrobbles)

if dateStamp:
	lastScrobbleFile = open(lastScrobbleFileLoc, 'w')