forked from kehsihba19/CP-Badges
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdata.py
207 lines (183 loc) · 5.79 KB
/
data.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
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
import re
import json
import requests
from bs4 import BeautifulSoup as bs
def get_info(handle, website):
website = website.lower()
if website == 'codechef':
return get_cc(handle)
elif website == 'codeforces':
return get_cf(handle)
elif website == 'atcoder':
return get_at(handle)
elif website == 'topcoder':
return get_top(handle)
elif website == 'yukicoder':
return get_yuki(handle)
elif website == 'uri':
return get_uri(handle)
elif website == 'leetcode':
return get_leetcode(handle)
elif website == 'leetcode-cn':
return get_leetcode_cn(handle)
else:
raise ValueError('wrong platform website name')
def get_cf(user):
"""
Returns rating and color of the codeforces user.
Parameters:
user (str): handle of the codeforces user
Returns:
[rating, col] (int, str): Current rating and the associated color of the given user.
Returns [-1, "#00000"] (-1, black) if the API is not responding.
"""
url = f'https://codeforces.com/api/user.info?handles={user}'
try:
response = requests.get(url)
profile_data = response.json().get('result')[0]
except:
return [-1, "#00000"]
rating = int(profile_data.get('rating', 0))
y = rating
if y <= 1199:
col = '#cec8c1'
elif 1199 < y <= 1399:
col = '#43A217'
elif 1399 < y <= 1599:
col = "#22C4AE"
elif 1599 < y <= 1899:
col = "#1427B2"
elif 1899 < y <= 2099:
col = "#700CB0"
elif 2099 < y <= 2299:
col = "#F9A908"
elif 2299 < y <= 2399:
col = "#FBB948"
else:
col = "#FF0000"
return [rating, col]
def get_cc(user):
url = f'https://www.codechef.com/users/{user}'
page = requests.get(url)
soup = bs(page.text, 'html.parser')
rating = soup.find_all('small')
if(len(rating)==5):
rating = (re.findall(r'\d+', rating[-2].text))
else:
rating = (re.findall(r'\d+', rating[-1].text))
col = 'red'
y = int(rating[0])
if (y <= 1399):
col = '#6A6860'
elif (y > 1399 and y <= 1599):
col = '#3D8C0B'
elif (y > 1599 and y <= 1799):
col = "#347FBD"
elif (y > 1799 and y <= 1999):
col = "#7A4AAF"
elif (y > 1999 and y <= 2199):
col = "#FFC300"
elif (y > 2199 and y <= 2499):
col = "#FF9E1B"
else:
col = "#FF1B1B"
return [rating[0], col]
def get_at(user):
url = f'https://atcoder.jp/users/{user}'
page = requests.get(url)
soup = bs(page.text, 'html.parser')
x = soup.find_all('table', class_='dl-table')
y = x[1].find_all('span')
y = [i.text for i in y]
val = y[y.index('―') - 1]
col = 'red'
a = int(val)
if (a <= 399):
col = '#8E8E81'
elif (a > 399 and a <= 799):
col = '#81501B'
elif (a > 799 and a <= 1199):
col = '#5CB01E'
elif (a > 1199 and a <= 1599):
col = '#16E5D8'
elif (a > 1599 and a <= 1999):
col = '#1642E5'
elif (a > 1999 and a <= 2399):
col = '#CFE115'
elif (a > 2399 and a <= 2799):
col = '#FF8700'
else:
col = '#FF0000'
return [val, col]
def get_top(user):
url = f'http://api.topcoder.com/v2/users/{user}'
json_data = requests.get(url).json()
rating = None
for kind in json_data['ratingSummary']:
if kind['name'] == 'Algorithm':
rating = kind['rating']
color = None
if rating < 900:
color = "#8E8E81"
elif rating < 1200:
color = "#5CB01E"
elif rating < 1500:
color = "#1642E5"
elif rating < 2200:
color = "#CFE115"
else:
color = "#FF0000"
return [rating, color]
def get_yuki(user):
url = f'https://yukicoder.me/api/v1/user/name/{user}'
json_data = requests.get(url).json()
level = str(json_data['Level'])
color = '#2ecc71'
return [level, color]
def get_uri(user_id):
url = f'https://www.urionlinejudge.com.br/judge/pt/profile/{user_id}'
r = requests.get(url).text
soup = bs(r, 'html.parser')
s = soup.find('ul', class_='pb-information')
s = [word.lower() for word in s.text.split()]
points = 0
if 'pontos:' in s:
strpoints = s[s.index('pontos:') + 1].replace('.', '')
points = int(strpoints[:strpoints.index(',')])
elif 'points:' in s:
strpoints = s[s.index('points:') + 1].replace('.', '')
points = int(strpoints[:strpoints.index(',')])
return [points, '#F9A908']
def get_leetcode(username):
url = 'http://leetcode.com/graphql'
queryString = '''query getContestRankingData($username: String!) {
userContestRankingHistory(username: $username) {
rating
}
}'''
variables = {
"username": username,
}
r = requests.get(url, json={'query': queryString, 'variables': variables})
json_data = r.json()
rankings = max([d['rating']
for d in json_data['data']['userContestRankingHistory']])
rankings = int(rankings)
return [rankings, '#FFA116']
def get_leetcode_cn(username):
url = 'https://leetcode-cn.com/graphql'
r = requests.post(
url,
json={
"operationName":
"userPublicProfile",
"variables": {
"userSlug": username
},
"query":
'query userPublicProfile($userSlug: String!) {userProfilePublicProfile(userSlug: $userSlug){profile{ranking{ratingProgress}}}}'
})
json_data = r.json()
rankings = max(d for d in json_data['data']['userProfilePublicProfile']
['profile']['ranking']['ratingProgress'])
return [rankings, '#FFA116']