-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.py
63 lines (47 loc) · 1.75 KB
/
app.py
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
"""
File: app.py
Author: Mark Rutherford
Created: 10/6/2021 5:29 PM
Flask entrypoint. Contains all routes.
"""
import flask
import utilities
app = flask.Flask(__name__)
# Index
@app.route('/', methods=['GET'])
def get_index():
"""
Serve the static index.html form to test the /getuserstats route.
Returns: static/index.html
"""
return flask.current_app.send_static_file('index.html')
# GitHub user aggregated stats API
@app.route('/getuserstats', methods=['GET'])
def get_user_stats():
"""
Provided a GitHub username, calculate and return aggregated statistics about the user's repositories.
Returns: The aggregated statistics
"""
username = utilities.get_request_arg(request=flask.request, arg_name='username', required=True)
show_forked = utilities.get_request_arg(request=flask.request, arg_name='forked')
# Return HTTP 400 if forked is set but not a boolean. Otherwise convert to boolean
if show_forked is None:
show_forked = True
elif show_forked.lower() != 'true' and show_forked.lower() != 'false':
flask.abort(400)
return
else:
show_forked = show_forked.lower() == 'true'
# Get all of the user's repositories
repositories = utilities.get_user_repositories(username, show_forked=show_forked)
# Create and return the statistics
user_stats = {
'Repositories': len(repositories),
'TotalStargazers': sum([repo.stargazers_count for repo in repositories]),
'TotalForkCount': sum([repo.forks_count for repo in repositories]),
'AverageRepoSize': utilities.get_average_repo_size(repositories),
'Languages': utilities.get_repo_languages(repositories),
}
return flask.jsonify(user_stats)
if __name__ == '__main__':
app.run()