This repository has been archived by the owner on Apr 11, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 7
/
fetchdata.py
222 lines (191 loc) · 6.56 KB
/
fetchdata.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
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
import requests
import queue
import time
import datapersistence as dp
GITHUB_API_URL = 'https://api.github.com/graphql'
TOKEN = 'use your own token'
global SEARCH_NODE_ID
SEARCH_NODE_ID = 'MDEwOlJlcG9zaXRvcnk2MzE0ODcxNQ=='
def load_token():
global TOKEN
with open('token', 'r') as f:
TOKEN = f.readline()
def run_query(query):
request = requests.post(GITHUB_API_URL, json={'query': query}, headers={'Authorization': TOKEN})
if request.status_code == 200:
return request.json()
else:
raise Exception("Query failed to run by returning code of {}. {}".format(request.status_code, query))
def get_user_stars(node_id):
if node_id in dp.USER_STAR_REPOSITORIES:
return dp.USER_STAR_REPOSITORIES[node_id]
if node_id not in dp.NODE_ID_CONTENT:
get_node_content(node_id)
if node_id not in dp.NODE_ID_CONTENT:
return None
node_content = dp.NODE_ID_CONTENT[node_id]
if 'login' not in node_content:
return None
next_cursor = ''
has_next = True
all_star_repos = []
def fetch_id_from_edge(edge):
if edge is None:
return None
node = edge['node']
dp.NODE_ID_CONTENT[node['id']] = {'owner': node['owner']['login'], 'name': node['name']}
return node['id']
while has_next:
query_ql = '''
query {
user(login: \"''' + node_content['login'] + '''\") {
starredRepositories(first: 100''' + next_cursor + ''') {
edges {
node {
id
owner {
login
}
name
}
}
pageInfo {
endCursor
hasNextPage
}
}
}
}
'''
response = run_query(query_ql)
if response['data']['user'] is not None:
repos = response['data']['user']['starredRepositories']
edges = repos['edges']
if len(edges) > 0:
nodes = list(map(fetch_id_from_edge, edges))
all_star_repos += nodes
page_info = repos['pageInfo']
next_cursor = ', after: \"' + page_info['endCursor'] + '\"'
has_next = page_info['hasNextPage']
else:
get_node_content(node_id)
node_content = dp.NODE_ID_CONTENT[node_id]
has_next = False
dp.USER_STAR_REPOSITORIES[node_id] = all_star_repos
dp.save_data()
return all_star_repos
def get_repo_stargazers(node_id):
if node_id in dp.REPOSITORY_STARGAZERS:
return dp.REPOSITORY_STARGAZERS[node_id]
if node_id not in dp.NODE_ID_CONTENT:
get_node_content(node_id)
if node_id not in dp.NODE_ID_CONTENT:
return None
node_content = dp.NODE_ID_CONTENT[node_id]
if 'owner' not in node_content:
return None
next_cursor = ''
has_next = True
all_stargazers = []
def fetch_id_from_edge(edge):
if edge is None:
return None
node = edge['node']
dp.NODE_ID_CONTENT[node['id']] = {'login': node['login']}
return node['id']
while has_next:
query_ql = '''
query {
repository(owner: \"''' + node_content['owner'] + '''\", name: \"''' + node_content['name'] + '''\") {
stargazers(first: 100''' + next_cursor + ''') {
edges {
node {
id
login
}
}
pageInfo {
endCursor
hasNextPage
}
}
}
}
'''
response = run_query(query_ql)
if response['data']['repository'] is not None:
stargazers = response['data']['repository']['stargazers']
edges = stargazers['edges']
if len(edges) > 0:
nodes = list(map(fetch_id_from_edge, edges))
all_stargazers += nodes
page_info = stargazers['pageInfo']
next_cursor = ', after: \"' + page_info['endCursor'] + '\"'
has_next = page_info['hasNextPage']
else:
get_node_content(node_id)
node_content = dp.NODE_ID_CONTENT[node_id]
has_next = False
dp.REPOSITORY_STARGAZERS[node_id] = all_stargazers
dp.save_data()
return all_stargazers
def get_node_content(node_id):
query_ql = '''
query {
node(id: \"''' + node_id + '''\") {
... on Repository {
owner {
login
}
name
}
... on User {
login
}
}
}
'''
response = run_query(query_ql)
node = response['data']['node']
if node is None:
return
if 'owner' in node and 'name' in node:
dp.NODE_ID_CONTENT[node_id] = {'owner': node['owner']['login'], 'name': node['name']}
elif 'login' in node:
dp.NODE_ID_CONTENT[node_id] = {'login': node['login']}
else:
print('node id: ' + node_id + ' not found')
def bfs_users_star_repos(node_id, max_level):
visited = [node_id]
q = queue.Queue()
q.put(node_id)
level = 1
current_level_node_count_left = 1
next_level_node_count = 0
while not q.empty():
current_node = q.get()
print(time.strftime("%Y-%m-%d %H:%M:%S", time.localtime())
+ ' bfs current level:' + format(level)
+ ' current node:' + current_node
+ ' ' + format(current_level_node_count_left) + ' nodes left')
result = get_user_stars(current_node)
if result is None:
result = get_repo_stargazers(current_node)
if result is not None:
for sub_node_id in result:
if sub_node_id is not None and sub_node_id not in visited:
visited.append(sub_node_id)
q.put(sub_node_id)
next_level_node_count += 1
current_level_node_count_left -= 1
if current_level_node_count_left == 0:
level += 1
if level > max_level:
return
current_level_node_count_left = next_level_node_count
next_level_node_count = 0
if __name__ == '__main__':
load_token()
dp.load_data()
bfs_users_star_repos(SEARCH_NODE_ID, 2)
dp.save_data()