codeforces_api_abuser / tools /codeforces.py
evapilotno17's picture
Update tools/codeforces.py
a1bd46a verified
Raw
History Blame Contribute Delete
8.09 kB
import requests
from urllib.parse import urlencode
from typing import List, Dict, Set
BASE_URL = "https://codeforces.com/api/"
import requests
from urllib.parse import urlencode
from typing import List, Dict, Set
from smolagents.tools import tool # Ensure this is the correct import
BASE_URL = "https://codeforces.com/api/"
@tool
def get_contest_data(cid:int, show_unofficial:bool=False)-> Dict: #it's import to specify the return type
#Keep this format for the description / args / args description but feel free to modify the tool
"""Fetches the contest standings data for a given contest ID
Args:
cid: The contest ID
show_unofficial: Whether to include unofficial participants
"""
params = {
"contestId": str(cid),
"showUnofficial": str(show_unofficial).lower(),
}
response = requests.get(f"{BASE_URL}contest.standings?{urlencode(params)}").json()
return response.get("result", {})
@tool
def get_rating_data(handle:str)-> List[Dict]: #it's import to specify the return type
#Keep this format for the description / args / args description but feel free to modify the tool
"""Fetches rating change history for a given user handle
Args:
handle: The Codeforces user handle
"""
response = requests.get(f"{BASE_URL}user.rating?handle={handle}").json()
return response.get("result", [])
@tool
def count_of_rated_matches(handle:str)-> int: #it's import to specify the return type
#Keep this format for the description / args / args description but feel free to modify the tool
"""Counts the number of rated contests a user has participated in
Args:
handle: The Codeforces user handle
"""
return len(get_rating_data(handle))
@tool
def get_user_info(handle:str)-> Dict: #it's import to specify the return type
#Keep this format for the description / args / args description but feel free to modify the tool
"""Fetches user information from Codeforces
Args:
handle: The Codeforces user handle
"""
response = requests.get(f"{BASE_URL}user.info?handles={handle}").json()
return response.get("result", [{}])[0]
@tool
def get_handle(standings_obj:Dict)-> str: #it's import to specify the return type
#Keep this format for the description / args / args description but feel free to modify the tool
"""Extracts the handle from a standings object
Args:
standings_obj: A dictionary representing a user's contest standings
"""
return standings_obj["party"]["members"][0]["handle"]
@tool
def solve_cutoff(cid:int, show_unofficial:bool, L:int, R:int)-> List[str]: #it's import to specify the return type
#Keep this format for the description / args / args description but feel free to modify the tool
"""Returns all handles that solved between L and R number of problems in a contest
Args:
cid: The contest ID
show_unofficial: Whether to include unofficial participants
L: The minimum number of problems solved
R: The maximum number of problems solved
"""
standings = get_contest_data(cid, show_unofficial)
return [
get_handle(entry)
for entry in standings.get("rows", [])
if L <= entry.get("points", 0) <= R
]
@tool
def get_all_rated_users()-> List[List]: #it's import to specify the return type
#Keep this format for the description / args / args description but feel free to modify the tool
"""Fetches all rated users with their highest rating
"""
response = requests.get(
f"{BASE_URL}user.ratedList?activeOnly=true&includeRetired=false"
).json()
return [[user["handle"], user["maxRating"]] for user in response.get("result", [])]
@tool
def filter_rated_users(L:int, R:int)-> List[str]: #it's import to specify the return type
#Keep this format for the description / args / args description but feel free to modify the tool
"""Filters users who have a max rating within a given range
Args:
L: The minimum rating
R: The maximum rating
"""
all_handles = get_all_rated_users()
return [handle for handle, max_rating in all_handles if L <= max_rating <= R]
@tool
def intersect(sets:List[List[str]])-> List[str]: #it's import to specify the return type
#Keep this format for the description / args / args description but feel free to modify the tool
"""Finds the intersection of multiple lists of handles
Args:
sets: A list of lists containing handles
"""
sorted_lists = sorted(sets, key=len)
result = set(sorted_lists[0]) if sorted_lists else set()
for s in sorted_lists:
result &= set(s)
return list(result)
from typing import Dict
import requests
# Assume BASE_URL is defined elsewhere; for example:
BASE_URL = "https://codeforces.com/api/"
@tool
def get_user_info(handle: str) -> Dict:
"""Fetches user information from Codeforces
Args:
handle: The Codeforces user handle
"""
response = requests.get(f"{BASE_URL}user.info?handles={handle}").json()
return response.get("result", [{}])[0]
@tool
def get_contribution(handle: str) -> int:
"""Fetches the contribution of a Codeforces user.
Args:
handle: The Codeforces user handle
"""
user_info = get_user_info(handle)
return user_info.get("contribution", 0)
@tool
def get_lastOnlineTimeSeconds(handle: str) -> int:
"""Fetches the last online time (in seconds) of a Codeforces user.
Args:
handle: The Codeforces user handle
"""
user_info = get_user_info(handle)
return user_info.get("lastOnlineTimeSeconds", 0)
@tool
def get_organization(handle: str) -> str:
"""Fetches the organization of a Codeforces user.
Args:
handle: The Codeforces user handle
"""
user_info = get_user_info(handle)
return user_info.get("organization", "")
@tool
def get_rating(handle: str) -> int:
"""Fetches the current rating of a Codeforces user.
Args:
handle: The Codeforces user handle
"""
user_info = get_user_info(handle)
return user_info.get("rating", 0)
@tool
def get_friendOfCount(handle: str) -> int:
"""Fetches the friend count of a Codeforces user.
Args:
handle: The Codeforces user handle
"""
user_info = get_user_info(handle)
return user_info.get("friendOfCount", 0)
@tool
def get_titlePhoto(handle: str) -> str:
"""Fetches the title photo URL of a Codeforces user.
Args:
handle: The Codeforces user handle
"""
user_info = get_user_info(handle)
return user_info.get("titlePhoto", "")
@tool
def get_rank(handle: str) -> str:
"""Fetches the rank of a Codeforces user.
Args:
handle: The Codeforces user handle
"""
user_info = get_user_info(handle)
return user_info.get("rank", "")
@tool
def get_handle(handle: str) -> str:
"""Fetches the handle (username) of a Codeforces user.
Args:
handle: The Codeforces user handle
"""
user_info = get_user_info(handle)
return user_info.get("handle", "")
@tool
def get_maxRating(handle: str) -> int:
"""Fetches the maximum rating of a Codeforces user.
Args:
handle: The Codeforces user handle
"""
user_info = get_user_info(handle)
return user_info.get("maxRating", 0)
@tool
def get_avatar(handle: str) -> str:
"""Fetches the avatar URL of a Codeforces user.
Args:
handle: The Codeforces user handle
"""
user_info = get_user_info(handle)
return user_info.get("avatar", "")
@tool
def get_registrationTimeSeconds(handle: str) -> int:
"""Fetches the registration time (in seconds) of a Codeforces user.
Args:
handle: The Codeforces user handle
"""
user_info = get_user_info(handle)
return user_info.get("registrationTimeSeconds", 0)
@tool
def get_maxRank(handle: str) -> str:
"""Fetches the maximum rank achieved by a Codeforces user.
Args:
handle: The Codeforces user handle
"""
user_info = get_user_info(handle)
return user_info.get("maxRank", "")