-
Notifications
You must be signed in to change notification settings - Fork 0
/
query.py
65 lines (57 loc) · 2.18 KB
/
query.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
import os, requests, time
from dotenv import load_dotenv
# 1. create a .env file
# 2. define GITHUB_TOKEN=<your github token> inside .env
load_dotenv()
class Query:
def __init__(self, location:str, date_range:str, cursor=None):
self.location = location
self.date_range = date_range
self.cursor = cursor
GITHUB_TOKEN = os.getenv("GITHUB_TOKEN")
self.headers = {"Authorization": f"Bearer {GITHUB_TOKEN}"}
self.variables = {"queryString": f"location:{self.location} created:{self.date_range}", "afterCursor": self.cursor}
def get_response(self):
query = """
query ($queryString: String!, $afterCursor: String) {
search(query: $queryString, type: USER, first: 100, after: $afterCursor) {
userCount
pageInfo {
endCursor
hasNextPage
}
edges {
node { ... on User {
login
name
location
company
followers {
totalCount
}
following {
totalCount
}
repositories(first: 1) {
totalCount
}
createdAt
updatedAt}
}
}
}
}
"""
response = requests.post("https://api.github.com/graphql", json={"query": query, "variables": self.variables}, headers=self.headers)
return response
def get_response_json(self):
response = self.get_response()
return response.json()
def get_wait_time(self):
response = self.get_response()
limit_remaining = int(response.headers["X-RateLimit-Remaining"])
if limit_remaining <= 0:
reset_time = int(response.headers["X-RateLimit-Reset"])
wait_time = reset_time - int(time.time()) + 5
return wait_time
return 0