Skip to content

Commit

Permalink
Using POST requests
Browse files Browse the repository at this point in the history
Fully resolved #5
Creating Release on tag
  • Loading branch information
VadVergasov committed Jan 20, 2021
1 parent 3b5113f commit b69734f
Show file tree
Hide file tree
Showing 8 changed files with 148 additions and 98 deletions.
18 changes: 17 additions & 1 deletion .github/workflows/publish.yml
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
name: Publish to PyPI
name: Publish to PyPI and create Release.

on:
push:
Expand All @@ -25,3 +25,19 @@ jobs:
uses: pypa/gh-action-pypi-publish@master
with:
password: ${{ secrets.pypi_password }}
build:
name: Create Release
runs-on: ubuntu-latest
steps:
- name: Checkout code
uses: actions/checkout@v2
- name: Create Release
id: create_release
uses: actions/create-release@v1
env:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
with:
tag_name: ${{ github.ref }}
release_name: ${{ github.event.head_commit.message }}
draft: false
prerelease: false
3 changes: 1 addition & 2 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,5 @@ dist/
build/
CodeforcesApiPy.egg-info
tests/conf.py
tests/__pycache__/
.pylintrc
codeforces_api/__pycache__/
__pycache__/
2 changes: 1 addition & 1 deletion LICENSE
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
MIT License

Copyright (c) 2020 VadVergasov ([email protected])
Copyright (c) 2021 VadVergasov ([email protected])

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
Expand Down
4 changes: 4 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,10 @@ anonim_cf_api = codeforces_api.CodeforcesApi() #Unauthorized access to api.
parser = codeforces_api.CodeforcesParser() #Create parser.
```

Wiki
--------
Here is link to the [wiki](https://github.com/VadVergasov/CodeforcesApiPy/wiki) for more details.

Examples
---------

Expand Down
42 changes: 13 additions & 29 deletions codeforces_api/api_request_maker.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
"""
Class for generating request URLs, which includes working with random, unpacking parameters, calculating hashes.
"""
import random
import time
import hashlib
Expand All @@ -24,7 +27,7 @@ def __init__(self, api_key=None, secret=None, random_number=1000000):
self.assigned_rand = True
elif random_number < 100000 and random_number > 999999:
raise Exception(
"Non 6-digit number passed as random_number for API Signature",
"The non-6-digit number passed as random_number for API Signature",
random_number,
)
if api_key is None and secret is None:
Expand All @@ -35,11 +38,13 @@ def __init__(self, api_key=None, secret=None, random_number=1000000):
self.anonimus = False
self.rand = random_number

def generate_url(self, method_name, **fields):
def generate_request(self, method_name, **fields):
"""
Generates request URL for API.
"""

request_url = "https://codeforces.com/api/" + str(method_name)

if not self.anonimus:

# Renew Rand
Expand All @@ -62,31 +67,8 @@ def generate_url(self, method_name, **fields):
api_signature = api_signature[:-1]
api_signature += "#" + str(self.secret)
hashed_signature = hashlib.sha512(api_signature.encode("utf-8"))

request_url = "https://codeforces.com/api/" + str(method_name) + "?"
for i in fields:
request_url += str(i) + "="
if isinstance(fields[i], list):
for j in fields[i]:
request_url += str(j) + ";"
else:
request_url += str(fields[i])
request_url += "&"
request_url += (
"apiSig=" + str(self.rand) + str(hashed_signature.hexdigest())
)
else:
request_url = "https://codeforces.com/api/" + str(method_name) + "?"
for i in fields:
request_url += str(i) + "="
if isinstance(fields[i], list):
for j in fields[i]:
request_url += str(j) + ";"
else:
request_url += str(fields[i])
request_url += "&"
request_url = request_url[:-1]
return request_url
fields["apiSig"] = str(self.rand) + str(hashed_signature.hexdigest())
return {"request_url": request_url, "data": fields}

def check_return_code(self, response):
"""
Expand All @@ -110,7 +92,7 @@ def renew_rand(self, random_number=1000000):
random_number = random.randint(100000, 999999)
elif random_number < 100000 and random_number > 999999:
raise Exception(
"Non 6-digit number passed as random_number for renew_rand",
"The non-6-digit number passed as random_number for renew_rand",
random_number,
)

Expand All @@ -120,4 +102,6 @@ def get_response(self, request):
self.check_return_code(response)
return response
except json.decoder.JSONDecodeError as error:
raise ValueError("A lot of users, try to reduce number of users in list")
raise ValueError(
"A lot of users, try to reduce the number of users in the list"
)
Loading

0 comments on commit b69734f

Please sign in to comment.