-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcodeforces.py
149 lines (116 loc) · 4.58 KB
/
codeforces.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
from re import fullmatch
from constants import CODEFORCES_URL
import requests
import json
from constants import *
class CodeforcesUserData:
def __init__(self, username=None):
self.__username = username
def get_personal_details(self):
full_url = f'user.info?handles={self.__username}'
url = CODEFORCES_URL+full_url
result = {'status': STATE_PENDING}
try:
response = requests.get(url)
json_object = response.json()
data = json_object['result'][0]
result['status'] = STATE_PASS
result['name'] = data['firstName'] + ' ' + data['lastName']
result['username'] = self.__username
result['rank'] = data['rank']
result['rating'] = data['rating']
result['avatar'] = 'https:'+data['titlePhoto']
result['city'] = data['city']
result['country'] = data['country']
result['organization'] = data['organization']
except:
result = {'status': STATE_ERROR_INVALID_USER,
'username': self.__username}
finally:
return result
def get_friend_details(self):
full_url = f'user.info?handles={self.__username}'
url = CODEFORCES_URL+full_url
friend_list = self.__username.split(';')
result = {'status': STATE_PENDING}
try:
response = requests.get(url)
json_object = response.json()
for data, friend in zip(json_object['result'], friend_list):
try:
temp_result = {}
temp_result['status'] = STATE_PASS
temp_result['name'] = data['firstName'] + \
' ' + data['lastName']
temp_result['username'] = friend
temp_result['rank'] = data['rank']
temp_result['rating'] = data['rating']
temp_result['avatar'] = 'https:'+data['titlePhoto']
temp_result['city'] = data['city']
temp_result['country'] = data['country']
temp_result['organization'] = data['organization']
result[friend] = temp_result
except:
temp_result = {
'status': STATE_ERROR_INVALID_USER, 'username': friend}
result[friend] = temp_result
except:
result = {'status': STATE_ERROR_INVALID_USER,
'username': self.__username}
finally:
return result
def get_past_submissions(self):
full_url = f'user.status?handle={self.__username}&from=1&count=100'
url = CODEFORCES_URL+full_url
result = {'status': STATE_PENDING}
try:
response = requests.get(url)
json_object = response.json()
result['status'] = STATE_PASS
result['result'] = json_object['result']
except:
result = {'status': STATE_ERROR_INVALID_USER,
'username': self.__username}
finally:
return result
def get_basic_details(self):
full_url = f'user.rating?handle={self.__username}'
url = CODEFORCES_URL+full_url
result = {'status': STATE_PENDING}
try:
response = requests.get(url)
json_object = response.json()
result['status'] = STATE_PASS
result['result'] = json_object['result']
except:
result = {'status': STATE_ERROR_INVALID_USER,
'username': self.__username}
finally:
return result
def get_contest_details(self):
ful_url = f'user.rating?handle={self.__username}'
url = CODEFORCES_URL+ful_url
result = {'status': STATE_PENDING}
try:
response = requests.get(url)
json_object = response.json()
result['status'] = STATE_PASS
result['contest'] = json_object['result']
except:
result = {'status': STATE_ERROR_INVALID_USER,
'username': self.__username}
finally:
return result
def get_problems_by_tags(tags):
full_url = f'problemset.problems?tags={tags}'
url = CODEFORCES_URL+full_url
result = {'status': STATE_PENDING}
try:
response = requests.get(url)
json_object = response.json()
result['status'] = STATE_PASS
result['problems'] = json_object['result']
except:
result = {'status': STATE_ERROR_IN_CODEFORCES_TAGS}
finally:
return result