-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathapp.py
More file actions
114 lines (82 loc) · 3.15 KB
/
app.py
File metadata and controls
114 lines (82 loc) · 3.15 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
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
import os
import json
from datetime import datetime, timedelta
import requests
from flask import Flask, render_template
# Import the configuration settings for the script
# TODO: Maybe handle this in a more efficient way?
# TODO: Add hashing for password so it's not stored in plaintext
try:
from config import (
GITHUB_REPO,
GITHUB_USERNAME,
GITHUB_PASSWORD,
DEBUG
)
except ImportError:
print('No config file set!')
print('Read the README for details on how to setup the config file')
exit(1)
GITHUB_URL = 'https://api.github.com/{}'
CACHE_FILENAME = 'records.json'
app = Flask(__name__, template_folder='assets/templates')
def get_dev_stats():
modified_since_header = None
if os.path.exists(CACHE_FILENAME):
# Get modified_since header to send in request
modified_since = datetime.utcnow() - timedelta(minutes=15)
date_format = '%a, %d %b %Y %H:%M:%S GMT'
modified_since_header = modified_since.strftime(date_format)
headers = {
'User-Agent': GITHUB_USERNAME,
'Accept': 'application/vnd.github.v3+json',
'If-Modified-Since': modified_since_header,
}
auth = (GITHUB_USERNAME, GITHUB_PASSWORD)
endpoint = 'repos/{}/commits'.format(GITHUB_REPO)
url = GITHUB_URL.format(endpoint)
# Make request to Github API
print('Making request to URL: {}'.format(url))
try:
resp = requests.get(url, headers=headers, auth=auth)
resp.raise_for_status()
print('Response Code: {}'.format(resp.status_code))
# If contents have not been modified, read from cache
if resp.status_code == 304:
with open(CACHE_FILENAME, 'r') as json_file:
records = json.load(json_file)
# Else get records from API response and write to cache
else:
records = resp.json()
with open(CACHE_FILENAME, 'w') as json_file:
json.dump(records, json_file)
# Parse response from Github API
devs = {}
for record in records:
# Skip over commits that are just merge commits
if 'Merge pull request' in record['commit']['message']:
continue
username = record['commit']['author']['name']
if not devs.get(username):
avatar_url = record['author']['avatar_url']
devs[username] = {}
devs[username]['login'] = username
devs[username]['avatar'] = avatar_url
devs[username]['commits'] = 1
else:
devs[username]['commits'] += 1
return devs
except requests.exceptions.ConnectionError as conn_error:
error_type = conn_error
url = conn_error.request.url
print('Caught a ConnectionError: {}'.format(error_type))
print('Trying to get URL: {}'.format(url))
raise
@app.route('/')
def index():
dev_table = get_dev_stats()
# Sort devs by number of commits
devs = sorted(dev_table.values(), key=lambda x: x['commits'], reverse=True)
return render_template('display.html', devs=devs)
if __name__ == '__main__':
app.run(debug=DEBUG)