Skip to content

Commit

Permalink
Small fixes (#7)
Browse files Browse the repository at this point in the history
* Renamed variables to not conflict with Python reserved words.

* Removed wildcard import.

* Added Tests passing in README.

* Code refactor.
  • Loading branch information
VadVergasov authored Mar 17, 2021
1 parent c0613af commit 4c812c4
Show file tree
Hide file tree
Showing 8 changed files with 140 additions and 129 deletions.
2 changes: 2 additions & 0 deletions .github/workflows/codestyle.yml
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,11 @@ on:
push:
branches:
- master
- develop
pull_request:
branches:
- master
- develop

jobs:
build:
Expand Down
2 changes: 2 additions & 0 deletions .github/workflows/test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,11 @@ on:
push:
branches:
- master
- develop
pull_request:
branches:
- master
- develop

jobs:
build:
Expand Down
3 changes: 2 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,9 @@ Codeforces API
[![Stars](https://img.shields.io/github/stars/VadVergasov/CodeforcesApiPy)](https://github.com/VadVergasov/CodeforcesApiPy/stargazers)
[![Forks](https://img.shields.io/github/forks/VadVergasov/CodeforcesApiPy)](https://github.com/VadVergasov/CodeforcesApiPy/network/members)
[![Issues](https://img.shields.io/github/issues/VadVergasov/CodeforcesApiPy)](https://github.com/VadVergasov/CodeforcesApiPy/issues)
[![Publish to PyPI and TestPyPI](https://github.com/VadVergasov/CodeforcesApiPy/workflows/Publish%20to%20PyPI%20and%20TestPyPI/badge.svg?branch=master)](https://pypi.org/project/CodeforcesApiPy/)
[![Publish to PyPI and creating Release](https://github.com/VadVergasov/CodeforcesApiPy/workflows/Publish%20to%20PyPI%20and%20TestPyPI/badge.svg?branch=master)](https://pypi.org/project/CodeforcesApiPy/)
[![Generate wiki](https://github.com/VadVergasov/CodeforcesApiPy/workflows/Generate%20wiki/badge.svg?branch=master)](https://github.com/VadVergasov/CodeforcesApiPy/wiki)
[![Testing](https://github.com/VadVergasov/CodeforcesApiPy/actions/workflows/test.yml/badge.svg?branch=master)](https://github.com/VadVergasov/CodeforcesApiPy/actions/workflows/test.yml)
[![Downloads](https://static.pepy.tech/personalized-badge/codeforcesapipy?period=total&units=international_system&left_color=black&right_color=blue&left_text=Total%20downloads)](https://pepy.tech/project/codeforcesapipy)
![Codestyle](https://img.shields.io/badge/code%20style-black-000000.svg)
==========
Expand Down
20 changes: 10 additions & 10 deletions codeforces_api/api_request_maker.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,9 +23,9 @@

class CodeforcesApiRequestMaker:

api_key = ""
secret = ""
rand = 0
_api_key = ""
_secret = ""
_rand = 0
assigned_rand = False
anonimus = False

Expand All @@ -46,10 +46,10 @@ def __init__(self, api_key=None, secret=None, random_number=1000000):
if api_key is None and secret is None:
self.anonimus = True
else:
self.api_key = api_key
self.secret = secret
self._api_key = api_key
self._secret = secret
self.anonimus = False
self.rand = random_number
self._rand = random_number

def generate_request(self, method_name, **fields):
"""
Expand All @@ -65,9 +65,9 @@ def generate_request(self, method_name, **fields):
self.renew_rand()

current_time = time.time()
fields["apiKey"] = str(self.api_key)
fields["apiKey"] = str(self._api_key)
fields["time"] = str(int(current_time))
api_signature = str(self.rand) + "/" + method_name + "?"
api_signature = str(self._rand) + "/" + method_name + "?"
fields = collections.OrderedDict(sorted(fields.items()))
for i in fields:
api_signature += str(i) + "="
Expand All @@ -78,9 +78,9 @@ def generate_request(self, method_name, **fields):
api_signature += str(fields[i])
api_signature += "&"
api_signature = api_signature[:-1]
api_signature += "#" + str(self.secret)
api_signature += "#" + str(self._secret)
hashed_signature = hashlib.sha512(api_signature.encode("utf-8"))
fields["apiSig"] = str(self.rand) + str(hashed_signature.hexdigest())
fields["apiSig"] = str(self._rand) + str(hashed_signature.hexdigest())
return {"request_url": request_url, "data": fields}

def check_return_code(self, response):
Expand Down
19 changes: 14 additions & 5 deletions codeforces_api/api_requests.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,19 @@
import requests

from codeforces_api.api_request_maker import CodeforcesApiRequestMaker
from codeforces_api.types import *
from codeforces_api.types import (
User,
BlogEntry,
Comment,
RecentAction,
RatingChange,
Contest,
Problem,
ProblemStatistic,
Submission,
Hack,
RanklistRow,
)


class CodeforcesApi(CodeforcesApiRequestMaker):
Expand Down Expand Up @@ -155,9 +167,6 @@ def contest_standings(
handles_str = ""
for handle in handles:
handles_str += str(handle) + ";"
request_data = self.generate_request(
"user.info", **{"handles": handles_str}
)
parameters["handles"] = handles_str
if room != -1:
parameters["room"] = str(room)
Expand Down Expand Up @@ -185,7 +194,7 @@ def contest_status(self, contest_id, handle="", start=-1, count=-1):
Returns parsed response from codeforces.com.
"""
if contest_id == None:
if contest_id is None:
raise TypeError("Contest_id is required")
parameters = {"contestId": str(contest_id)}
if handle != "":
Expand Down
47 changes: 22 additions & 25 deletions codeforces_api/types.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,6 @@
You should have received a copy of the GNU General Public License
along with this program. If not, see <https://www.gnu.org/licenses/>.
"""
"""
Source of inspiration: https://github.com/eternnoir/pyTelegramBotAPI/blob/master/telebot/types.py
"""
import json
Expand Down Expand Up @@ -60,10 +58,9 @@ def check_json(json_type):
"""
if isinstance(json_type, dict):
return json_type
elif isinstance(json_type, str):
if isinstance(json_type, str):
return json.loads(json_type)
else:
raise ValueError("json_type should be a json dict or string.")
raise ValueError("json_type should be a json dict or string.")

def __str__(self):
d = {}
Expand Down Expand Up @@ -194,7 +191,7 @@ def de_json(cls, json_string):
if json_string is None:
return None
obj = cls.check_json(json_string)
id = obj["id"]
identifier = obj["id"]
original_locale = obj["originalLocale"]
creation_time_seconds = obj["creationTimeSeconds"]
author_handle = obj["authorHandle"]
Expand All @@ -206,7 +203,7 @@ def de_json(cls, json_string):
rating = obj["rating"]
content = obj.get("content")
return cls(
id,
identifier,
original_locale,
creation_time_seconds,
author_handle,
Expand All @@ -221,7 +218,7 @@ def de_json(cls, json_string):

def __init__(
self,
id,
identifier,
original_locale,
creation_time_seconds,
author_handle,
Expand All @@ -233,7 +230,7 @@ def __init__(
rating,
content=None,
):
self.id = id
self.id = identifier
self.original_locale = original_locale
self.creation_time_seconds = creation_time_seconds
self.author_handle = author_handle
Expand Down Expand Up @@ -267,15 +264,15 @@ def de_json(cls, json_string):
if json_string is None:
return None
obj = cls.check_json(json_string)
id = obj["id"]
identifier = obj["id"]
creation_time_seconds = obj["creationTimeSeconds"]
commentator_handle = obj["commentatorHandle"]
locale = obj["locale"]
text = obj["text"]
rating = obj["rating"]
parent_comment_id = obj.get("parentCommentId")
return cls(
id,
identifier,
creation_time_seconds,
commentator_handle,
locale,
Expand All @@ -286,15 +283,15 @@ def de_json(cls, json_string):

def __init__(
self,
id,
identifier,
creation_time_seconds,
commentator_handle,
locale,
text,
rating,
parent_comment_id=None,
):
self.id = id
self.id = identifier
self.creation_time_seconds = creation_time_seconds
self.commentator_handle = commentator_handle
self.locale = locale
Expand Down Expand Up @@ -401,7 +398,7 @@ def de_json(cls, json_string):
if json_string is None:
return None
obj = cls.check_json(json_string)
id = obj["id"]
identifier = obj["id"]
name = obj["name"]
contest_type = obj["type"]
phase = obj["phase"]
Expand All @@ -419,7 +416,7 @@ def de_json(cls, json_string):
city = obj.get("city")
season = obj.get("season")
return cls(
id,
identifier,
name,
contest_type,
phase,
Expand All @@ -440,7 +437,7 @@ def de_json(cls, json_string):

def __init__(
self,
id,
identifier,
name,
contest_type,
phase,
Expand All @@ -458,7 +455,7 @@ def __init__(
city=None,
season=None,
):
self.id = id
self.id = identifier
self.name = name
self.contest_type = contest_type
self.phase = phase
Expand Down Expand Up @@ -650,7 +647,7 @@ def de_json(cls, json_string):
if json_string is None:
return None
obj = cls.check_json(json_string)
id = obj["id"]
identifier = obj["id"]
creation_time_seconds = obj["creationTimeSeconds"]
relative_time_seconds = obj["relativeTimeSeconds"]
problem = Problem.de_json(obj["problem"])
Expand All @@ -664,7 +661,7 @@ def de_json(cls, json_string):
verdict = obj.get("verdict")
points = obj.get("points")
return cls(
id,
identifier,
creation_time_seconds,
relative_time_seconds,
problem,
Expand All @@ -681,7 +678,7 @@ def de_json(cls, json_string):

def __init__(
self,
id,
identifier,
creation_time_seconds,
relative_time_seconds,
problem,
Expand All @@ -695,7 +692,7 @@ def __init__(
verdict=None,
points=None,
):
self.id = id
self.id = identifier
self.creation_time_seconds = creation_time_seconds
self.relative_time_seconds = relative_time_seconds
self.problem = problem
Expand Down Expand Up @@ -733,7 +730,7 @@ def de_json(cls, json_string):
if json_string is None:
return None
obj = cls.check_json(json_string)
id = obj["id"]
identifier = obj["id"]
creation_time_seconds = obj["creationTimeSeconds"]
hacker = Party.de_json(obj["hacker"])
defender = Party.de_json(obj["defender"])
Expand All @@ -742,7 +739,7 @@ def de_json(cls, json_string):
test = obj.get("test")
judge_protocol = obj.get("judgeProtocol")
return cls(
id,
identifier,
creation_time_seconds,
hacker,
defender,
Expand All @@ -754,7 +751,7 @@ def de_json(cls, json_string):

def __init__(
self,
id,
identifier,
creation_time_seconds,
hacker,
defender,
Expand All @@ -763,7 +760,7 @@ def __init__(
test=None,
judge_protocol=None,
):
self.id = id
self.id = identifier
self.creation_time_seconds = creation_time_seconds
self.hacker = hacker
self.defender = defender
Expand Down
2 changes: 1 addition & 1 deletion codeforces_api/version.py
Original file line number Diff line number Diff line change
@@ -1 +1 @@
__version__ = "2.0.5"
__version__ = "2.0.6"
Loading

0 comments on commit 4c812c4

Please sign in to comment.