-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathuserOrganization.py
79 lines (75 loc) · 2.42 KB
/
userOrganization.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
import pandas as pd
from utils import FileWriter, GraphQL
class UserOrganization:
count = 0
query = """
{
user(login: "%s") {
organizations(first: 10, after: %s) {
pageInfo {
hasNextPage
startCursor
endCursor
}
edges {
cursor
node {
name
description
login
location
url
websiteUrl
membersWithRole {
totalCount
}
}
}
}
}
}
"""
endCursor = "null"
def __init__(self, login):
self.data = {}
self.reform = []
self.df = None
self.login = login
self.hasNextPage = False
def fetch(self):
while(True):
print("Request #%d" % (self.count+1))
query = self.query % (self.login, self.endCursor)
data = GraphQL.execute(query)
self.count = self.count+1
self.hasNextPage = data["user"]["organizations"]["pageInfo"]["hasNextPage"]
self.endCursor = "\"%s\"" % data["user"]["organizations"]["pageInfo"]["endCursor"]
if(self.data):
self.data["user"]["organizations"]["edges"] = self.data["user"]["organizations"]["edges"]+data["user"]["organizations"]["edges"]
self.data["user"]["organizations"]["pageInfo"] = data["user"]["organizations"]["pageInfo"]
else:
self.data = data
print("Finshed #%d" % (self.count+1))
if(self.hasNextPage == False):
print("No more data")
break
pass
def preprocessing(self):
print("Data preprocessing")
if self.data:
self.reform = self.data["user"]["organizations"]["edges"]
cursors = list(map(lambda x: x["cursor"], self.reform))
self.reform = list(map(lambda x: x["node"], self.reform))
for (index, i) in enumerate(self.reform):
self.reform[index]["cursor"] = cursors[index]
self.reform[index]["membersWithRole"] = i["membersWithRole"]["totalCount"]
if i["description"]:
self.reform[index]["description"] = i["description"].replace('\n', '').replace('\r', '')
def toDataFrame(self):
self.preprocessing()
self.df = pd.json_normalize(self.reform)
self.df["login"] = self.login
print(self.df)
def saveCSV(self, fileName, mode):
print("Save data")
FileWriter.writeFile(self.df, fileName, mode)