User:Mirjam Dissel/JSON audioscrobbler

From XPUB & Lens-Based wiki

http://pzwart3.wdka.hro.nl/~mdissel/cgi-bin/JSONlastfm.cgi


#!/usr/bin/python
#-*- coding:utf-8 -*-

import json, urllib2, random
import cgitb; cgitb.enable()

print "Content-Type: text/html; charset=utf-8"     # HTML is following
print                               # blank line, end of headers




i = 1

url = 'http://ws.audioscrobbler.com/2.0/?format=json&method=chart.gethypedartists&api_key=b25b959554ed76058ac220b7b2e0a026'
f = urllib2.urlopen(url)
data = json.load(f)

#print data ["artists"]["artist"][0]["name"]

print "<div style=\"margin:100px;\">"

for (i, r) in enumerate(data ["artists"]["artist"]):
	print "<img src=\"" + r ["image"][1]["#text"] + "\">"
	print "<b><span style=\"background-color:white; font-size:"+ str(random.randint(10, 100)) +"px\">" + str(i+1) + ". " + r ["name"].encode('utf-8') + "</span></b> <br>"

# displaying images
#	print "<img src=\"" + r ["image"][-1]["#text"] + "\"> <br>"

# setting an image as the background (but it does it inside the loop!)
#	print "<body background=\"" + r ["image"][-1]["#text"] + "\"> <br>"

# setting an image a a background
maxartists = len(data ["artists"]["artist"])
print "<body background=\"" + data ["artists"]["artist"][random.randint(1, maxartists)]["image"][-1]["#text"] + "\"> <br>"

print "</div>"


chmod 755 file.cgi

blablabla on server