-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgit.py
More file actions
executable file
·37 lines (31 loc) · 1.6 KB
/
git.py
File metadata and controls
executable file
·37 lines (31 loc) · 1.6 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
import requests
import xml.etree.ElementTree as ET
# Takes Github username, password/access token[, and HTTP headers dict]
# Returns a HEAD request for given users events.
def get_user_events_head(user, password, headers={}):
url = 'https://api.github.com/users/' + user + '/events'
auth = (user, password)
return requests.head(url, auth=auth, headers=headers)
# Takes Github username and password/access token.
# Returns a polling function which will return true once a new user event exists.
def create_events_poller(user, password):
e_tag = get_user_events_head(user, password).headers['ETag']
headers = { 'If-None-Match': e_tag }
def poller():
return get_user_events_head(user, password, headers).status_code == 200
return poller
# Takes svg contributions calendar as text.
# Returns multidimensional List representation of contributions.
def parse_contrib_svg(html_response_text):
clean_response = html_response_text.replace('data-repository-hovercards-enabled', '')
root_el = ET.fromstring(clean_response)
calendar_el = root_el.find('div').find('div').find('div').find('svg')
week_g_els = calendar_el.find('g').findall('g')
weeks_of_rects_els = [week_g.findall('rect') for week_g in week_g_els]
return [[int(day_el.attrib['data-count']) for day_el in week] for week in weeks_of_rects_els]
# Takes Github username.
# Returns user contributions data organised as a multidimensional list of weeks/days.
def get_user_contrib_data(user):
user_contrib_url = 'https://github.com/users/' + user + '/contributions'
response = requests.get(user_contrib_url)
return parse_contrib_svg(response.text)