Skip to content

Commit a4b7371

Browse files
committed
add: add Activity.py part and Exceptions.py
1 parent 9a9ce09 commit a4b7371

File tree

2 files changed

+169
-0
lines changed

2 files changed

+169
-0
lines changed

extendingPython/GiteeAPI/Activity.py

Lines changed: 159 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,159 @@
1+
import pyhttpx
2+
3+
import Exceptions
4+
5+
6+
def getTheUserWhoStarTheRepo(owner: str, repo: str, page: int = -1, access_token: str = '', per_page: int = 100):
7+
"""
8+
:param owner: str, the owner of the repo
9+
:param repo: str, the repo name
10+
:param page: int, the page number
11+
:param access_token: str, the access token
12+
:param per_page: int, the number of users per page
13+
:return: list of users
14+
"""
15+
session = pyhttpx.HttpSession()
16+
url = f"https://gitee.com/api/v5/repos/{owner}/{repo}/stargazers"
17+
if access_token == '':
18+
if page == -1:
19+
params = {
20+
"owner": owner,
21+
"repo": repo,
22+
"per_page": per_page
23+
}
24+
else:
25+
params = {
26+
"owner": owner,
27+
"repo": repo,
28+
"page": page,
29+
"per_page": per_page
30+
}
31+
else:
32+
if page == -1:
33+
params = {
34+
"access_token": access_token,
35+
"owner": owner,
36+
"repo": repo,
37+
"per_page": per_page
38+
}
39+
else:
40+
params = {
41+
"access_token": access_token,
42+
"owner": owner,
43+
"repo": repo,
44+
"page": page,
45+
"per_page": per_page
46+
}
47+
res = session.get(url, params=params)
48+
res = res.json()
49+
if len(res) != 0 and res.get("message") == "Not Found Project":
50+
raise Exceptions.NotFoundProject("The project is not found or your project is private")
51+
elif len(res) != 0 and res.get("message") == "401 Unauthorized: Access token does not exist":
52+
raise Exceptions.AccessTokenNotExist("Access token does not exist")
53+
return res
54+
55+
56+
def getTheUserWhoWatchTheRepo(owner: str, repo: str, page: int = -1, access_token: str = '', per_page: int = 100):
57+
"""
58+
:param owner: str, the owner of the repo
59+
:param repo: str, the repo name
60+
:param page: int, the page number
61+
:param access_token: str, the access token
62+
:param per_page: int, the number of users per page
63+
:return: list of users
64+
"""
65+
session = pyhttpx.HttpSession()
66+
url = f"https://gitee.com/api/v5/repos/{owner}/{repo}/subscribers"
67+
if access_token == '':
68+
if page == -1:
69+
params = {
70+
"owner": owner,
71+
"repo": repo,
72+
"per_page": per_page
73+
}
74+
else:
75+
params = {
76+
"owner": owner,
77+
"repo": repo,
78+
"page": page,
79+
"per_page": per_page
80+
}
81+
else:
82+
if page == -1:
83+
params = {
84+
"access_token": access_token,
85+
"owner": owner,
86+
"repo": repo,
87+
"per_page": per_page
88+
}
89+
else:
90+
params = {
91+
"access_token": access_token,
92+
"owner": owner,
93+
"repo": repo,
94+
"page": page,
95+
"per_page": per_page
96+
}
97+
res = session.get(url, params=params)
98+
res = res.json()
99+
try:
100+
if res.get("message") == "Not Found Project":
101+
raise Exceptions.NotFoundProject("The project is not found or your project is private")
102+
elif res.get("message") == "401 Unauthorized: Access token does not exist":
103+
raise Exceptions.AccessTokenNotExist("Access token does not exist")
104+
except AttributeError:
105+
pass
106+
return res
107+
108+
109+
def getTheMassageFromTheRepo(owner: str, repo: str, access_token: str = '', limit: int = 100, prev_id: int = -1):
110+
"""
111+
:param owner: str, the owner of the repo
112+
:param repo: str, the repo name
113+
:param access_token: str, the access token
114+
:param limit: int, the number of messages
115+
:param prev_id: int, the previous message id
116+
:return: list of messages
117+
"""
118+
session = pyhttpx.HttpSession()
119+
url = f"https://gitee.com/api/v5/repos/{owner}/{repo}/events"
120+
if access_token == '':
121+
if prev_id == -1:
122+
params = {
123+
"owner": owner,
124+
"repo": repo,
125+
"limit": limit
126+
}
127+
else:
128+
params = {
129+
"owner": owner,
130+
"repo": repo,
131+
"limit": limit,
132+
"prev_id": prev_id
133+
}
134+
else:
135+
if prev_id == -1:
136+
params = {
137+
"access_token": access_token,
138+
"owner": owner,
139+
"repo": repo,
140+
"limit": limit
141+
}
142+
else:
143+
params = {
144+
"access_token": access_token,
145+
"owner": owner,
146+
"repo": repo,
147+
"limit": limit,
148+
"prev_id": prev_id
149+
}
150+
res = session.get(url, params=params)
151+
res = res.json()
152+
try:
153+
if res.get("message") == "Not Found Project":
154+
raise Exceptions.NotFoundProject("The project is not found or your project is private")
155+
elif res.get("message") == "401 Unauthorized: Access token does not exist":
156+
raise Exceptions.AccessTokenNotExist("Access token does not exist")
157+
except AttributeError:
158+
pass
159+
return res
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
class NotFoundProject(Exception):
2+
def __init__(self, message):
3+
self.message = message
4+
super().__init__(self.message)
5+
6+
7+
class AccessTokenNotExist(Exception):
8+
def __init__(self, message):
9+
self.message = message
10+
super().__init__(self.message)

0 commit comments

Comments
 (0)