]> projects.mako.cc - iron-blogger/blob - report.py
show-week: use a mako template
[iron-blogger] / report.py
1 import yaml
2 import feedparser
3 import datetime
4 from dateutil.parser import parse
5 import dateutil.tz as tz
6
7 with open('bloggers.yml') as f:
8     users = yaml.safe_load(f.read())
9
10 log = {}
11
12 START = datetime.datetime(2009, 12, 21, 6)
13
14 def parse_published(pub):
15     return parse(pub).astimezone(tz.tzlocal()).replace(tzinfo=None)
16
17 def get_date(post):
18     if 'published' in post:
19         return post.published
20     return post.updated
21
22 def get_link(post):
23     if 'href' in post:
24         return post.href
25     return post.links[0]['href']
26
27 def parse_feeds(weeks, uri):
28     feed = feedparser.parse(uri)
29     for post in feed.entries:
30         date = parse_published(get_date(post))
31
32         if date < START:
33             continue
34         wn = (date - START).days / 7
35
36         while len(weeks) <= wn:
37             weeks.append([])
38         weeks[wn].append(dict(
39                 date=date,
40                 title=post.title,
41                 url=get_link(post)))
42
43 for (username, u) in users.items():
44     weeks = []
45     print "[%s]" % (username)
46     for l in u['links']:
47         parse_feeds(weeks, l[2])
48     log[username] = weeks
49     for (i, w) in enumerate(weeks):
50         print " [%d]: %s" % (i, w)
51
52 with open('out/report.yml', 'w') as f:
53     yaml.safe_dump(log, f)

Benjamin Mako Hill || Want to submit a patch?