Skip to content

Commit 3b5113f

Browse files
committed
Added get_tag method and fixed error in CodeforcesAPI class
Resolving #5
1 parent 80a65a9 commit 3b5113f

File tree

4 files changed

+32
-2
lines changed

4 files changed

+32
-2
lines changed

codeforces_api/api_requests.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,10 +13,11 @@ class CodeforcesApi(CodeforcesApiRequestMaker):
1313

1414
session = None
1515

16-
def __init__(self):
16+
def __init__(self, api_key=None, secret=None, random_number=1000000):
1717
"""
1818
Initializing class. All we will need is session to optimize performance.
1919
"""
20+
super().__init__(api_key, secret, random_number)
2021
self.session = requests.Session()
2122

2223
def blog_entry_comments(self, blog_entry_id):

codeforces_api/parse_methods.py

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,11 +3,13 @@
33
"""
44
import requests
55
from lxml import html
6+
from codeforces_api.api_requests import CodeforcesApi
67

78

89
class CodeforcesParser:
910

1011
session = None
12+
problem_tags = dict()
1113

1214
def __init__(self):
1315
"""
@@ -36,3 +38,27 @@ def get_solution(self, contest_id, submit_id):
3638
if len(code) == 0:
3739
raise ValueError("Incorrect contest_id or submit_id " + str(code))
3840
return code[0].replace("\r", "")
41+
42+
def get_tags(self, contest_id, index):
43+
"""
44+
Get tags of given problem.
45+
46+
contest_id is number of contest.
47+
48+
index is number of problem, better to be capital letter. Also could be integer or lowercase letter.
49+
"""
50+
if self.problem_tags == dict():
51+
cf_api = CodeforcesApi()
52+
for problem in cf_api.problemset_problems()["result"]["problems"]:
53+
if str(problem["contestId"]) not in self.problem_tags.keys():
54+
self.problem_tags[str(problem["contestId"])] = dict()
55+
self.problem_tags[str(problem["contestId"])][
56+
str(problem["index"])
57+
] = problem["tags"]
58+
if isinstance(index, int):
59+
index = chr(ord("A") + index)
60+
elif isinstance(index, str):
61+
if index.isnumeric():
62+
index = chr(ord("A") + int(index))
63+
index = index.capitalize()
64+
return self.problem_tags[str(contest_id)][index]

setup.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55

66
setup(
77
name="CodeforcesApiPy",
8-
version="1.4.0",
8+
version="1.4.1",
99
description="Implementation of codeforces.com API",
1010
platforms="any",
1111
url="https://github.com/VadVergasov/CodeforcesApiPy",

tests/main.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,8 @@
88

99
MAIN = codeforces_api.CodeforcesApi(conf.API_KEY, conf.API_SECRET)
1010

11+
ANONIM_API = codeforces_api.CodeforcesApi()
12+
1113
PARSER = codeforces_api.CodeforcesParser()
1214

1315
COMMENTS = MAIN.blog_entry_comments(74185)
@@ -31,5 +33,6 @@
3133
USER_STATUS = MAIN.user_status("VadVergasov")
3234

3335
SOLUTION = PARSER.get_solution(1322, 72628149)
36+
TAGS = PARSER.get_tags(1322, "D")
3437

3538
print("No errors found!")

0 commit comments

Comments
 (0)