import xml.dom.minidom import urllib import sys, os import operator import datetime from appscript import * username = sys.argv[1] MSEC_PER_PIXEL = 50000 TOP_ARTISTS_COUNT = 50 WEEK_COUNT = -1 #-1 for all COLORS = [(1,.5,.5),(.5,1,.5),(.5,.5,1),(1,.5,1),(.5,1,1)] allartists = {} alltotals = {} allheights = {} alldepths = {} weeklychartlist = xml.dom.minidom.parseString(urllib.urlopen("http://ws.audioscrobbler.com/1.0/user/%s/weeklychartlist.xml" % username).read()) charts = weeklychartlist.getElementsByTagName('chart')[:WEEK_COUNT] for chart in charts: fromdate = long(chart.getAttribute('from')) todate = long(chart.getAttribute('to')) print "%s-%s" % (fromdate, todate) weeklyartistchart = xml.dom.minidom.parseString(urllib.urlopen("http://ws.audioscrobbler.com/1.0/user/%s/weeklyartistchart.xml?from=%s&to=%s" % (username, fromdate, todate)).read()) #weeklyartistchart = xml.dom.minidom.parse("localchart.txt") #print weeklyartistchart.toxml().encode('utf-8') artists = weeklyartistchart.getElementsByTagName('artist') for artist in artists: name = artist.getElementsByTagName('name')[0].firstChild.toxml() playcount = int(artist.getElementsByTagName('playcount')[0].firstChild.toxml()) print "%s: %s" % (name, playcount) if name in allartists.keys(): allartists[name][fromdate] = playcount else: allartists[name] = {fromdate : playcount} if fromdate not in allheights.keys(): allheights[fromdate] = 0 print allartists print allheights alldepths = allheights.copy() graffle = app('OmniGraffle Professional 5') diagram = graffle.documents.end.make(new=k.document) mindate = sorted(allheights.keys())[0] #add up total playcounts for each artist totals = {} for artist in allartists.keys(): for date in allartists[artist].keys(): if artist in totals.keys(): totals[artist] += allartists[artist][date] else: totals[artist] = allartists[artist][date] #only get the top artists totals = sorted(totals.items(), key=operator.itemgetter(1), reverse=True) if TOP_ARTISTS_COUNT > len(totals): TOP_ARTISTS_COUNT = len(totals) totals = (i[0] for i in totals[:TOP_ARTISTS_COUNT]) print totals #draw horizontal axis labels for date in sorted(allheights.keys()): readabledate = str(datetime.date.fromtimestamp(date)) diagram.canvases[1].graphics.end.make(new=k.shape, with_properties={k.origin: ((date-mindate)/MSEC_PER_PIXEL, 200), k.size: (8, 8), k.text: readabledate, k.draws_shadow: 0, k.draws_stroke: 0, k.fill: k.no_fill}) #draw the chart! count = 0 for artist in totals: pointList = [] for date in sorted(allheights.keys()): if count == 0: if date in allartists[artist].keys(): bottom = allheights[date] - (allartists[artist][date] / 2) else: bottom = 0 alldepths[date] = bottom elif count % 2: bottom = allheights[date] else: bottom = alldepths[date] for i in range(0,3): pointList.append(((date-mindate)/MSEC_PER_PIXEL, bottom)) for date in sorted(allheights.keys(), reverse=True): if count == 0: if date in allartists[artist].keys(): top = allheights[date] + (allartists[artist][date] / 2) else: top = 0 allheights[date] = top elif count % 2: if date in allartists[artist].keys(): top = allheights[date] + allartists[artist][date] else: top = allheights[date] allheights[date] = top else: if date in allartists[artist].keys(): top = alldepths[date] - allartists[artist][date] else: top = alldepths[date] alldepths[date] = top for i in range(0,3): pointList.append(((date-mindate)/MSEC_PER_PIXEL, top)) color = COLORS[count%len(COLORS)] diagram.canvases[1].graphics.end.make(new=k.shape, with_properties={k.point_list: pointList, k.stroke_color: [i-.2 for i in color], k.fill_color: color, k.notes: artist, k.draws_shadow: 0}) count += 1