-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgetUserRating.py
More file actions
52 lines (41 loc) · 2.08 KB
/
getUserRating.py
File metadata and controls
52 lines (41 loc) · 2.08 KB
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
from catboost import CatBoostRegressor
import requests
from source.Event.mapper import map_submission, map_rating_update, map_blog
from source.User.mapper import map_user
from datetime import datetime
from source.extractFeatures import extract_features
import sys
import pandas as pd
def get_user_rating_6m_in_the_future(user_handle: str) -> int | str:
response = requests.get(f'https://codeforces.com/api/user.info?handles={user_handle}')
if response.json()['status'] != 'OK':
return f'The user does {user_handle} not exist'
user = map_user(response.json()['result'][0])
response = requests.get(f'https://codeforces.com/api/user.rating?handle={user_handle}').json()
rating_updates = response['result']
if len(rating_updates) <= 5:
return 'The user needs to participate in at least 5 rated contests to be able to predict their rating'
for rating_update in rating_updates:
rating_update_event = map_rating_update(rating_update)
user.add_event(rating_update_event)
response = requests.get(f'https://codeforces.com/api/user.status?handle={user_handle}&from=1&count=10000').json()
submissions = response['result']
for submission in submissions:
if 'rating' not in submission['problem'] or 'participantType' not in submission['author']:
continue
submission_event = map_submission(submission)
user.add_event(submission_event)
response = requests.get(f'https://codeforces.com/api/user.blogEntries?handle={user_handle}').json()
if 'result' in response:
blogs = response['result']
for blog in blogs:
blog_event = map_blog(blog)
user.add_event(blog_event)
current_timestamp = int(datetime.now().timestamp())
features = extract_features(user, current_timestamp)
model = CatBoostRegressor()
model.load_model('data/catboost_rating_predictor.cbm')
prediction = model.predict(pd.DataFrame([features]))
return int(prediction[0]) + user.get_rating_at_time(current_timestamp)
if __name__ == '__main__':
print(get_user_rating_6m_in_the_future(sys.argv[1]))