Created
June 23, 2009 02:32
-
-
Save jberkel/134322 to your computer and use it in GitHub Desktop.
create streamgraphs from delicious history
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env python | |
import lxml.etree | |
import xml.utils.iso8601 | |
from datetime import datetime, timedelta | |
from graphication import FileOutput, Series, SeriesSet, Label, SimpleScale, css, default_css as style | |
from graphication.scales.date import AutoWeekDateScale, AutoDateScale | |
from graphication.wavegraph import WaveGraph | |
""" | |
curl https://user:[email protected]/v1/posts/all > posts.xml | |
./posts.py | |
""" | |
def group_period(list, threshold=10): | |
grouped = {} | |
for time, tags in list: | |
year_week = "%s-%s" % (time.isocalendar()[0], time.isocalendar()[1]) | |
for t in tags: | |
if not t in grouped: grouped[t] = {} | |
if not year_week in grouped[t]: grouped[t][year_week] = 0 | |
grouped[t][year_week] += 1 | |
sorted = [(t, grouped[t]) for t in grouped] | |
sorted.sort(lambda x,y: cmp(len(y[1]), len(x[1]))) | |
return sorted[0:threshold] | |
def parse(file='posts.xml'): | |
return [[datetime.fromtimestamp(xml.utils.iso8601.parse(p.get('time'))), p.get('tag').split()] for p in lxml.etree.XML(open(file).read())] | |
def colorgen(colours): | |
while 1: | |
for c in colours: yield c | |
def get_series(grouped): | |
colour_loop = colorgen( | |
("#4D8963", | |
"#69A583", | |
"#E1B378", | |
"#E0CC97", | |
"#EC799A", | |
"#9F0251") | |
) | |
series_set = SeriesSet() | |
for (t, list) in grouped: | |
data = dict([(datetime.strptime(k+'0', "%Y-%W%w") ,v) for (k,v) in list.iteritems()]) | |
series_set.add_series(Series(t, data, colour_loop.next())) | |
return series_set | |
def create_graph(filename="tags", threshold=15): | |
series_set = get_series(group_period(parse(), threshold=threshold)) | |
scale = AutoWeekDateScale(series_set) | |
output = FileOutput() | |
output.add_item(WaveGraph(series_set, scale, None, label_curves=True), x=0, y=30, width=20*len(series_set.keys()), height=500) | |
output.write("png", ("%s.png" % filename)) | |
output.write("pdf", ("%s.pdf" % filename)) | |
create_graph(threshold=10) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment