-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathrepositoryTopic.py
67 lines (66 loc) · 2.32 KB
/
repositoryTopic.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
from utils import GraphQL, FileWriter
import pandas as pd
class RepositoryTopic:
query = """
query {
repository(name: "%s", owner: "%s") {
repositoryTopics(first: 20, after: %s) {
pageInfo {
endCursor
hasNextPage
hasPreviousPage
startCursor
}
edges {
cursor
node {
topic {
name
}
}
}
}
}
}
"""
def __init__(self, name, owner):
self.data = {}
self.name = name
self.owner = owner
self.endCursor = "null"
self.count = 0
def fetch(self):
while(True):
print("Request #%d" % (self.count+1))
query = self.query % (self.name, self.owner, self.endCursor)
data = GraphQL.execute(query)
self.count = self.count+1
self.hasNextPage = data["repository"]["repositoryTopics"]["pageInfo"]["hasNextPage"]
self.endCursor = "\"%s\"" % data["repository"]["repositoryTopics"]["pageInfo"]["endCursor"]
if(self.data):
self.data["repository"]["repositoryTopics"]["edges"] = self.data["repository"]["repositoryTopics"]["edges"]+data["repository"]["repositoryTopics"]["edges"]
self.data["repository"]["repositoryTopics"]["pageInfo"] = data["repository"]["repositoryTopics"]["pageInfo"]
else:
self.data = data
print("Finshed #%d" % (self.count+1))
if(self.hasNextPage == False):
print("No more data")
break
def preprocessing(self):
print("Data preprocessing")
if self.data:
self.reform = self.data["repository"]["repositoryTopics"]["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]["topic"] = i["topic"]["name"]
def toDataFrame(self):
self.preprocessing()
self.df = pd.json_normalize(self.reform)
self.df["owner"] = self.owner
self.df["name"] = self.name
print(self.df)
def saveCSV(self, fileName, mode):
print("Save data")
FileWriter.writeFile(self.df, fileName, mode)