-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathGitStars.py
58 lines (45 loc) · 2.06 KB
/
GitStars.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
import requests
from time import sleep
from GitStarsException import GitStarsException
class GitStars:
def __init__(self, instance, token):
self.__instance = instance
self.__token = token
def get_all_star_repos(self, sleep_time):
repos = list()
page = 1
while True:
request = requests.get(f"{self.__instance}/user/starred",
params={'page': page, 'per_page': 100},
headers={'Authorization': 'token ' + self.__token,
'X-GitHub-Api-Version': '2022-11-28'})
data = request.json()
if request.status_code == 200:
if len(data) == 0:
return repos
else:
[repos.append(name['full_name']) for name in data]
else:
raise GitStarsException(request.status_code, data['message'])
page = page + 1
sleep(sleep_time)
def delete_star(self, repo_name):
request = requests.delete(f"{self.__instance}/user/starred/{repo_name}",
headers={'Authorization': 'token ' + self.__token,
'X-GitHub-Api-Version': '2022-11-28'})
if request.status_code == 204:
return True
elif request.status_code == 404:
return False
else:
raise GitStarsException(request.status_code, request.json()['message'])
def set_star(self, repo_name):
request = requests.put(f"{self.__instance}/user/starred/{repo_name}",
headers={'Authorization': 'token ' + self.__token,
'X-GitHub-Api-Version': '2022-11-28'})
if request.status_code == 204:
return True
elif request.status_code == 404:
return False
else:
raise GitStarsException(request.status_code, request.json()['message'])