iKnow の進捗を Python で取得するというエントリをみつけたので、さらにmatplotlibでグラフ書くようにしてみた。これを一日の終わりにcronで実行すれば記録できる。
#!/usr/bin/env python
import urllib
from BeautifulSoup import BeautifulSoup
import re
from sqlobject import *
from pylab import *
from matplotlib.dates import MONDAY, WeekdayLocator
import datetime,os
# --- Configure --- #
mydb = '/home/kzfm/iknow.db'
mygraph = '/var/www/html/images/lf/iknow'
myname = 'kzfm'
myconnect = 'sqlite://' + mydb
sqlhub.processConnection = connectionForURI(myconnect)
class RecKnow(SQLObject):
iknow_study = IntCol()
iknow_finish = IntCol()
dictation_study = IntCol()
dictation_finish = IntCol()
date = DateCol(default=datetime.date.today(), unique = True)
class Iknow:
def __init__(self,username):
b = BeautifulSoup(urllib.urlopen("http://www.iknow.co.jp/user/"+username))
iknow = b.find('div',id="study_badge_div").find('script').string
self.iknow_study = int(re.search(r"int_left.*?(\d+)", iknow).group(1))
self.iknow_finish = int(re.search(r"int_right.*?(\d+)", iknow).group(1))
dictation = b.find('div',id="dictation_badge_div").find('script').string
self.dictation_study = int(re.search(r"int_left.*?(\d+)", dictation).group(1))
self.dictation_finish = int(re.search(r"int_right.*?(\d+)", dictation).group(1))
def create_graph():
iknow_study = [i.iknow_study for i in RecKnow.select()]
iknow_finish = [i.iknow_finish for i in RecKnow.select()]
dictation_study = [i.dictation_study for i in RecKnow.select()]
dictation_finish = [i.dictation_finish for i in RecKnow.select()]
dates = [date2num(i.date) for i in RecKnow.select()]
mondays = WeekdayLocator(MONDAY)
fig = figure(figsize=(9,5))
ax = fig.add_subplot(111)
ax.plot_date(dates, iknow_study, 'co-')
ax.plot_date(dates, iknow_finish, 'mo-')
ax.plot_date(dates, dictation_study, 'yo-')
ax.plot_date(dates, dictation_finish, 'ko-')
ax.xaxis.set_major_locator(mondays)
ax.autoscale_view()
ax.grid(True)
fig.autofmt_xdate()
title('iKnow Daily chart')
xlabel('Date')
ylabel('Score')
legend(["iKnow study", "iKnow finished", "dict study", "dict finished"])
fig.savefig(mygraph)
if __name__ == '__main__':
if not os.path.isfile(mydb):
RecKnow.createTable()
iknow = Iknow(myname)
RecKnow(iknow_study=iknow.iknow_study,iknow_finish=iknow.iknow_finish,
dictation_study=iknow.dictation_study,dictation_finish=iknow.dictation_finish)
create_graph()