forked from Zfour/python_github_calendar_api
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.py
More file actions
38 lines (37 loc) · 1.28 KB
/
index.py
File metadata and controls
38 lines (37 loc) · 1.28 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
# -*- coding: UTF-8 -*-
import requests
import re
from http.server import BaseHTTPRequestHandler
import json
def list_split(items, n):
return [items[i:i + n] for i in range(0, len(items), n)]
def getdata(name):
gitpage = requests.get("https://github.com/" + name)
data = gitpage.text
datadatereg = re.compile(r'data-date="(.*?)" data-level')
datacountreg = re.compile(r'data-count="(.*?)" data-date')
datadate = datadatereg.findall(data)
datacount = datacountreg.findall(data)
datacount = list(map(int, datacount))
contributions = sum(datacount)
datalist = []
for index, item in enumerate(datadate):
itemlist = {"date": item, "count": datacount[index]}
datalist.append(itemlist)
datalistsplit = list_split(datalist, 7)
returndata = {
"total": contributions,
"contributions": datalistsplit
}
return returndata
class handler(BaseHTTPRequestHandler):
def do_GET(self):
path = self.path
user = path.split('?')[1]
data = getdata(user)
self.send_response(200)
self.send_header('Access-Control-Allow-Origin', '*')
self.send_header('Content-type', 'application/json')
self.end_headers()
self.wfile.write(json.dumps(data).encode('utf-8'))
return