This repository has been archived by the owner on Jun 24, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
utils.py
137 lines (121 loc) · 3.64 KB
/
utils.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
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
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
import csv
import json
from datetime import datetime
import time
from os import system, name
def clear():
if name == 'nt':
_ = system('cls')
else:
_ = system('clear')
def get_data_query(file_name):
"""
Get GH query for supplied GH organization
"""
with open(file_name, 'r') as graphql_query:
return graphql_query.read()
def parse_repo_data(node):
return {
'name': node['name'],
'owner': node['owner']['login'],
'issues': node['issues']['totalCount'],
'forks': node['forks']['totalCount'],
'stargazers': node['stargazers']['totalCount'],
'watchers': node['watchers']['totalCount'],
'forkCount': node['forkCount'],
'full_name': node['nameWithOwner'],
'created_at': node['createdAt'],
'isPrivate': node['isPrivate'],
}
def parse_issue_data(node):
return {
'type': node['__typename'],
'owner': node['repository']['owner']['login'],
'repo_name': node['repository']['name'],
'title': node['title'],
'created_at': node['createdAt'],
'last_edit_date': node['lastEditedAt'],
'state': node['state'],
'updated_at': node['updatedAt'],
}
def parse_data(node):
"""
Parse Github node data.
"""
if node['__typename'] == 'Repository':
return parse_repo_data(node)
if node['__typename'] == 'Issue' or node['__typename'] == 'PullRequest':
return parse_issue_data(node)
def create_csv(file_name, data, fields):
"""
Create a CSV file from the supplied data and fields
"""
with open(f'data/{file_name}', 'w') as csv_file:
writer = csv.DictWriter(csv_file, fieldnames=fields)
writer.writeheader()
writer.writerows(data)
def write_issues_csv(owner, run_time, data):
"""
Write issues to a CSV file
"""
fields = [
'type',
'owner',
'repo_name',
'title',
'created_at',
'last_edit_date',
'state',
'updated_at',
]
create_csv(f'github-{owner}-issues-data-{run_time}.csv', data, fields)
def write_repos_csv(owner, run_time, data):
"""
Write repos to a CSV file
"""
fields = [
'name',
'owner',
'issues',
'forks',
'stargazers',
'watchers',
'forkCount',
'full_name',
'created_at',
'isPrivate',
]
create_csv(f'github-{owner}-repos-data-{run_time}.csv', data, fields)
def write_stars_forks(owner, run_time, stars, forks):
fields = ['owner', 'repo', 'createdAt']
create_csv(f'github-{owner}-stars-data-{run_time}.csv', stars, fields)
create_csv(f'github-{owner}-forks-data-{run_time}.csv', forks, fields)
def get_rate_limit(client):
"""
Get the Github API rate limit current state for the used token
"""
query = '''query {
rateLimit {
limit
remaining
resetAt
}
}'''
response = client.execute(query)
json_response = json.loads(response)
return json_response['data']['rateLimit']
def handle_rate_limit(rate_limit):
"""
Handle Github API rate limit and wait times
"""
remaining = rate_limit['remaining']
limit = rate_limit['limit']
percent_remaining = remaining / limit
reset_at = rate_limit['resetAt']
if percent_remaining < 0.15:
reset_at = datetime.strptime(reset_at, '%Y-%m-%dT%H:%M:%SZ')
current_time = datetime.now()
time_diff = reset_at - current_time
seconds = time_diff.total_seconds()
print(f'Rate Limit hit. Waiting for reset.\nProcess will continue at: {reset_at}')
time.sleep(seconds)