-
Notifications
You must be signed in to change notification settings - Fork 3
/
fetch.py
58 lines (43 loc) · 1.4 KB
/
fetch.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
import requests
"""
Parameters:
@username - the user, whose commits to be fetched
@repo - name of repo whose commits are to be counter
@owner - owner of the repo
Return:
A list containing commits of user @username in the repo
@repo, whose owner is @owner. Each commit will be a dict
having following properites:
{sha, message, date, verified}
"""
def fetch(username, repo, owner):
response = requests.get(f"https://api.github.com/repos/{owner}/{repo}/commits")
# if status is not 200, i.e. response is not valid
if response.status_code != 200:
return None
data = response.json()
# filtering the commits of user 'username'
filtered = filter(lambda x: x["commit"]["author"]["name"] == username, data)
# mapping each commit to the previously defined object
processed = map(map_commit, filtered)
return list(processed)
"""
Parameters:
@commit - A raw commit dict
Returns:
A commit dict with the following fields kept:
@commit['sha']
@commit['message']
@commit['date']
@commit['verified']
All other fields are dropped.
"""
def map_commit(commit):
return {
"sha": commit["sha"],
"message": commit["commit"]["message"],
"date": commit["commit"]["author"]["date"],
"verified": commit["commit"]["verification"]["verified"],
}
def simple_test():
print(fetch("leeg8", "github-leaderboard", "jacksonet00"))