一直很喜欢Github的contributions记录的绿色方块,想搬到博客上,于是就有了这么一个东西,利用d3.js生成svg矢量图来显示出来。不过Github上的提交记录实在惨不忍睹,感觉用来记录每天的工作时长效果会更好一点,不过嘛具体怎么优雅地使用这个东西有时间再想。
下面这个图是用脚本从Github上爬来的Github提交数据显示出来的,当然从颜色上也能看出我有多么的懒_(:3」∠)_
用来爬数据的脚本是随手写的最初版的脚本是随手写的,不过后来做了些改进每天定时执行两次用于自动更新calendarmap.js;感觉BeautifulSoup好好用的说~
# Author: Hamster<[email protected]>
# Created on 14:03 2016/1/22
import BeautifulSoup
import os
import requests
def prase(item, js, crlf):
date = item['data-date']
year = date[0:4]
month = date[5:7]
day = date[8:10]
cur = -1
flag = False
datastr = ''
datastr += ' \"' + year + month + day + '\":' + item['data-count'] + ',' + crlf
for fstr in js:
if(datastr[0:len(datastr)-len(crlf)-1] + crlf == fstr or datastr == fstr):
flag = True
break
if(not flag):
for fstr in js:
cur += 1
if(fstr == '};'+crlf):
js.insert(cur, datastr)
break
def check(js, crlf):
cur = -1
for line in js:
cur += 1
if(line == '};'+crlf):
js[cur-1] = js[cur-1][0:len(js[cur-1])-len(crlf)-1] + crlf
break
else:
if(js[cur][len(js[cur])-len(crlf)-1:len(js[cur])] != ','+crlf and cur != 0):
js[cur] = js[cur][0:len(js[cur])-len(crlf)] + ',' + crlf
def main():
# Open session of github contribution page
session = requests.Session()
url = "https://github.com/users/iHamsterball/contributions"
response = session.get(url)
s = BeautifulSoup.BeautifulSoup(response.text)
l = s.findAll('rect')
# Open file for rw
crlf = ''
if(os.name == 'nt'):
crlf = '\n'
if(os.getcwd() != 'C:\Users\Hamster\Documents\GitHub\ihamsterball.github.io\pyscript'):
fp = open('js\calendarmap.js','r+')# Called by 'Jekyll Build.bat' script, different run path
else:
fp = open('..\js\calendarmap.js','r+')# Debug mode
else:
crlf = '\r\n'
if(os.name == 'posix'):
fp = open('/usr/local/nginx/site/js/calendarmap.js','r+')
else:
fp = open('/usr/local/nginx/site/js/calendarmap.js','r+')
js = fp.readlines()
# Reading count and add data
for item in l:
if item['data-count']!='0':
prase(item, js, crlf)
# Format datalist
check(js, crlf)
# Write to js file
# WTF, r+ mode cannot clear the file in this way
#fp.truncate()
fp.seek(0)
for line in js:
fp.write(line)
print 'Successfully update calendarmap.js'
fp.close()
if __name__ == "__main__":
main()
不过嘛刚才也提到了这个问题就是Github的惨淡数据,所以,可以换一个思路比如来显示每天工作的时间。这个工作时间嘛说起来倒也是想怎么算都可以的所以还是最好有一个统计工具,比如我用了一段时间的RescueTime软件,可以很方便的统计日常使用电脑的活动,虽然用了的话依旧每周的娱乐时间超标除了考试周。具体要怎么处理以后有时间再考虑,毕竟还要涉及到从RescueTime的统计网页上扒数据再格式化所以也是有点任务量的。
嗯嗯,就这样~