-
Notifications
You must be signed in to change notification settings - Fork 24
/
githubQuery.py
129 lines (114 loc) · 2.96 KB
/
githubQuery.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
from string import Template
from requests import get, post
userInfoQuery = """
{
viewer {
login
id
}
}
"""
createContributedRepoQuery = Template("""
query {
user(login: "$username") {
repositoriesContributedTo(last: 100, includeUserRepositories: true) {
nodes {
isFork
name
owner {
login
}
}
}
}
}
""")
createCommittedDateQuery = Template("""
query {
repository(owner: "$owner", name: "$name") {
ref(qualifiedName: "master") {
target {
... on Commit {
history(first: 100, author: { id: "$id" }) {
edges {
node {
committedDate
}
}
}
}
}
}
}
}
""")
repositoryListQuery = Template("""
{
user(login: "$username") {
repositories(orderBy: {field: CREATED_AT, direction: ASC}, last: 100, affiliations: [OWNER, COLLABORATOR, ORGANIZATION_MEMBER], isFork: false) {
totalCount
edges {
node {
object(expression:"master") {
... on Commit {
history (author: { id: "$id" }){
totalCount
}
}
}
primaryLanguage {
color
name
id
}
stargazers {
totalCount
}
collaborators {
totalCount
}
createdAt
name
owner {
id
login
}
nameWithOwner
}
}
}
location
createdAt
name
}
}
""")
getLinesOfCodeQuery = Template("""/repos/$owner/$repo/stats/code_frequency""")
getProfileViewQuery = Template(
"""/repos/$owner/$repo/traffic/views""")
getProfileTrafficQuery = Template(
"""/repos/$owner/$repo/traffic/popular/referrers""")
class RunQuery():
def __init__(self, headers):
self.headers = headers
def runGithubAPIQuery(self, query):
request = get("https://api.github.com" + query, headers=self.headers)
if request.status_code == 200:
return request.json()
else:
raise Exception(
"Query failed to run by returning code of {}. {},... {}".format(
request.status_code, query, str(request.json())))
def runGithubGraphqlQuery(self, query) -> dict:
request = post("https://api.github.com/graphql",
json={"query": query}, headers=self.headers)
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 runGithubContributionsQuery(self, username):
request = get(
"https://github-contributions.now.sh/api/v1/" + username)
if request.status_code == 200:
return request.json()