Giter Club home page Giter Club logo

codeforcesapipy's Introduction

Hi there ๐Ÿ‘‹

  • ๐Ÿ”ญ I'm currently working on Codeforces API python
  • ๐ŸŒฑ I'm currently learning Python, C++, also C# for educational purposes
  • ๐Ÿ’ผ I'm currently working at the Yandex
  • ๐Ÿ“ซ How to reach me: Telegram, Email, LinkedIn

codeforcesapipy's People

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar

codeforcesapipy's Issues

Wiki

I think adding a Wiki for usage instructions would make the library a lot more accessible.
(I would like to volunteer myself)

Get gym solution for parser

Would be cool to add a parser function similar to get_solution, but for mashup contests created in gym.
Implementation should be similar, except for /gym/ in the url path

def get_gym_solution(self, contest_id, submit_id):
    solutionPage = self.session.get(
        "https://codeforces.com/gym/"
        + str(contest_id)
        + "/submission/"
        + str(submit_id)
    )
    if int(solutionPage.status_code) != 200:
        raise Exception("Returned not OK code " + str(solutionPage))
    tree2 = html.fromstring(solutionPage.text)
    code = tree2.xpath("//pre[@id='program-source-text']/text()")
    if len(code) == 0:
        raise ValueError("Incorrect contest_id or submit_id " + str(code))
    return code[0].replace("\r", "")

or just add one more parameter to get_solution with default value gym=False

Updates in Codeforces API

Describe the bug
Codeforces have updated their API and added new parameter asManager to the context.status endpoint
https://codeforces.com/apiHelp/methods#contest.status

To Reproduce
Steps to reproduce the behavior:

  1. Create codeforces_api.CodeforcesApi object
  2. Call the CodeforcesApi.contest_status method with parameter asManager=True

Expected behavior
Method should work as expected and forward the argument to codeforces webapi

Additional context
image

Add support for additional APIs

To support the use-cases in the project https://github.com/nileshsah/harwest-tool, it'll be great to have additional APIs to support:

(1) Getting a count of the total number of submission-page for a user
Could be achieved by scraping the count from the user submissions page, example

   def get_submissions_page_count(self):
        base_url = "https://codeforces.com/submissions/" + self.user
        sub_soup = self.__get_content_soup(base_url)
        pages = sub_soup.findAll('span', attrs={'class': 'page-index'})
        return int(pages[-1].find('a').text)

(2) Getting the tags for a given problem Id
Even though it's possible to get the tags for a problem via the Codeforces API, it lacks the tag for the rating of the problem which would be quite crucial to have, and hence scraping might be the only viable option. Example,

    def get_contest_tags(self, problem_url):
        con_soup = self.__get_content_soup(problem_url)
        span_tags = con_soup.findAll('span', attrs={'class': 'tag-box'})
        return [x.text.strip() for x in span_tags]

'function' object has no attribute 'urlencode'

Describe the bug
I installed the package into a virtual environment and proceeded to attempt the following code:

import codeforces_api
import json

SECRETS = json.load(open("secret.json"))

cf_api = codeforces_api.CodeforcesApi(api_key = SECRETS["key"], secret = SECRETS["secret"])

print(cf_api.contest_list(gym = True))

which of course raised the error

Traceback (most recent call last):
  File "/root/lumonike/cf_api/main.py", line 8, in <module>
    print(cf_api.contest_list(gym = True))
  File "/root/lumonike/cf_api/env/lib/python3.10/site-packages/codeforces_api/api_requests.py", line 112, in contest_list
    for contest in self._make_request(
  File "/root/lumonike/cf_api/env/lib/python3.10/site-packages/codeforces_api/api_requests.py", line 49, in _make_request
    request_data = self.generate_request(method, **payload)
  File "/root/lumonike/cf_api/env/lib/python3.10/site-packages/codeforces_api/api_request_maker.py", line 71, in generate_request
    api_signature += requests.urllib3.request.urlencode(fields, safe=";")
AttributeError: 'function' object has no attribute 'urlencode'

To Reproduce

  1. follow steps here: https://codeforces.com/blog/entry/74291
  2. run code listed above

Expected behavior
The error mentioned above will be raised

Environment:

  • Python version: 3.10.12
  • Module version: 2a5f8e8
  • OS version: Ubuntu 22.04

Possible Solution
I believe switching line 71 of api_request_maker.py: api_signature += requests.urllib3.request.urlencode(fields, safe=";")
into

api_signature += urlencode(fields, safe=";")

with the addition of this module from urllib.parse import urlencode

completely solve this issue.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.