Script to update last.fm (audioscrobbler) from a NAS

alphac
Posts:2
Joined:Sat Jun 12, 2010 1:05 pm
AV Hardware:Raidsonic 4220B
Script to update last.fm (audioscrobbler) from a NAS

Post by alphac » Sat Jun 12, 2010 6:10 pm

This script will update a last.fm with the last song played

it requires a NAS with python or a chrooted debian with python
script requires elementtree library and also this file in the same folder as the script: http://github.com/offmessage/pyscrobble ... robbler.py

modify TWONKY_URL (fill with your last played songs rss, go to http://nasip:9000/rss -> playlists -> last played )
modify LASTFMUSER
modify LASTFMPASSWORD

launch with "python scriptname.py &"

Code: Select all

from urllib import urlopen
import datetime
import time

try:
	from  audioscrobbler import AudioScrobblerPost
except:
	print "audioscrobbler.py not present"
	quit

try:
    from xml.etree.ElementTree import ElementTree
except ImportError:
    # we aren't python2.5, you'll need elementtree installed
    # see http://effbot.org/zone/element-index.htm for more info
    from elementtree.ElementTree import ElementTree



TWONKY_URL = 'http://192.168.30.20:9000/rss/feed/1$11$207905894.xml'
LASTFMUSER = 'user'
LASTFMPASSWORD = 'pass'


OLDTITLE = 'old'


def parseLastPlayedandUpdate(lastplayedUrl):
	rss = ElementTree()
	ElementTree.parse(rss,urlopen(lastplayedUrl))
	
	global OLDTITLE

	title=''	
	description = ''
	author = ''
	duration = '' 
	
	
	for node in rss.getiterator():
		try:
			if (node.tag == "description"):
				description = node.text
			if (node.tag == "author"):
				author = node.text
			if (node.tag == "duration"):
				duration = int(node.text)/1000
			if (node.tag == "title"):
				title = node.text
		except: 
			pass
		
	

	track = dict(artist_name=author,
		song_title=title,
		length=duration,
		date_played=datetime.datetime.now().strftime("%Y-%m-%d %H:%M:%S"),
		album=description,
		mbid=""
		)

	if (title != OLDTITLE):
		post = AudioScrobblerPost(username=LASTFMUSER, password=LASTFMPASSWORD, verbose=True)
		print "Sending " + title
		post(**track)	
	else:
		print "Skipping already updated"

	OLDTITLE = title

while True: 
	parseLastPlayedandUpdate(TWONKY_URL)
	time.sleep(60)


Post Reply