Skip to content

Instantly share code, notes, and snippets.

@manics
Last active December 19, 2015 12:59
Show Gist options
  • Save manics/5959304 to your computer and use it in GitHub Desktop.
Save manics/5959304 to your computer and use it in GitHub Desktop.
Create a simple HTML visualisation of similarity scores between last.fm users. Replace API_KEY with your Last.fm api key, and USERS_* with a list of usernames.
# Create a simple HTML visualisation of similarity scores between last.fm users.
# Replace API_KEY with your Last.fm api key, and USERS_* with a list of usernames.
import sys
import time
import urllib2
import xml.dom.minidom
api_key = API_KEY
def score(u1, u2):
# Just in case we go over the rate limit
time.sleep(0.25)
url = 'http://ws.audioscrobbler.com/2.0/?method=tasteometer.compare&type1=user&type2=user&value1=%s&value2=%s&api_key=%s' % (u1, u2, api_key)
x = urllib2.urlopen(url)
r = x.read()
p = xml.dom.minidom.parseString(r)
score = p.getElementsByTagName('score')
assert(len(score) == 1)
s = float(score[0].firstChild.nodeValue)
return s
def xscore(u1, u2):
# Use this for testing without hitting the last.fm API
import random
return random.random()
def getMatrix(users):
n = len(users)
C = [[0.] * n for i in xrange(n)]
for i in xrange(n):
for j in xrange(i + 1):
if i == j:
C[i][j] = 1.
else:
C[i][j] = score(users[i], users[j])
C[j][i] = C[i][j]
sys.stderr.write('%s %s: %f\n' % (users[i], users[j], C[i][j]))
return C
def getColor(s):
r = s * 255
g = 0
#b = (255 - s * 255) / 2
b = 0
return 'white', 'rgb(%d,%d,%d)' % (r, g, b)
def formatUser(u):
return '<a href="http://www.last.fm/user/%s">%s</a>' % (u, u)
def printHtml(users, C):
print '<!DOCTYPE html>'
print '<html><head></head><body>'
n = len(users)
print '<table>'
print '<tr>'
print '<td></td>'
for i in xrange(n):
print '<td>%s</td>' % formatUser(users[i])
print '</tr>'
for i in xrange(n):
print '<tr>'
print '<td>%s</td>' % formatUser(users[i])
for j in xrange(n):
textcol, bgcol = getColor(C[i][j])
print '<td style="color:%s;background-color:%s">%.3f</td>' % (
textcol, bgcol, C[i][j])
print '</tr>'
print '</table>'
print '</body></html>'
users = [
USER_1,
USER_2,
]
C = getMatrix(users)
printHtml(users, C)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment