Skip to content

Instantly share code, notes, and snippets.

Created October 9, 2013 17:41
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save anonymous/11d297e5d1db5b312a2f to your computer and use it in GitHub Desktop.
Save anonymous/11d297e5d1db5b312a2f to your computer and use it in GitHub Desktop.
import collections
import pygal
import requests
import sys
from bs4 import BeautifulSoup
from dateutil import parser
user = sys.argv[1]
all_datetimes = []
days_of_the_week = ['Lundi', 'Mardi', 'Mercredi', 'Jeudi', 'Vendredi', 'Samedi', 'Dimanche']
next_page = "users/%s/comments" % user
while next_page:
req = requests.get("http://linuxfr.org/%s" % next_page)
soup = BeautifulSoup(req.text)
for time in soup.find_all('time', class_='updated'):
all_datetimes.append(parser.parse(time['datetime']))
next_ = soup.find('a', rel='next')
if next_:
next_page = next_['href']
else:
next_page = None
stats = collections.defaultdict(lambda: collections.defaultdict(int))
for dt in all_datetimes:
stats[dt.weekday()][dt.hour] += 1
dot_chart = pygal.Dot(x_label_rotation=0)
dot_chart.title = '%s Punchcard' % user
dot_chart.x_labels = map(str, range(24))
for day in range(len(days_of_the_week)):
by_hour = []
for hour in xrange(24):
by_hour.append(stats[day][hour])
dot_chart.add(days_of_the_week[day], by_hour)
dot_chart.render_to_file("%s_punchcard.svg" % user)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment