Leetcode API implementation.
test This implements methods that are available publicly for leetcode. There is already an implementation of them in form of CLI [1], but it has a list of disadvantages.
-
It is written in JS
-
Even for JS in order to reuse it you have to invoke it via CLI
-
It is not supported very well, so authentication doesn't work anymore
So I have decided to create my own implementation and here is it.
Using the swagger file you'll be able to generate the code for any language you like and start using leetcode API directly from your code.
Just keep in mind that swagger doesn't really support cookie auth,
which is needed in order to use LC API. If you use python you can
just use the generated code from this repo. Otherwise you'll have to
implement something like fix_cookies.patch
for your target language.
You can find examples of usage in example.py
[1] https://github.com/skygragon/leetcode-cli
This Python package is automatically generated by the Swagger Codegen project:
- API version: 1.0.1-1
- Package version: 1.0.0
- Build package: io.swagger.codegen.v3.generators.python.PythonClientCodegenкак думаешь, сможешь с мамой договориться?
Python 2.7 and 3.4+
If the python package is hosted on Github, you can install directly from Github
pip install git+https://github.com/prius/python-leetcode.git
(you may need to run pip
with root permission: sudo pip install git+https://github.com/prius/python-leetcode.git
)
Then import the package:
import leetcode
Install via Setuptools.
python setup.py install --user
(or sudo python setup.py install
to install the package for all users)
Then import the package:
import leetcode
Please follow the installation procedure and then run the following:
from __future__ import print_function
import time
import leetcode
from leetcode.rest import ApiException
from pprint import pprint
# Configure API key authorization: cookieCSRF
configuration = leetcode.Configuration()
configuration.api_key['csrftoken'] = 'YOUR_API_KEY'
# Uncomment below to setup prefix (e.g. Bearer) for API key, if needed
# configuration.api_key_prefix['csrftoken'] = 'Bearer'
# Configure API key authorization: cookieSession
configuration = leetcode.Configuration()
configuration.api_key['LEETCODE_SESSION'] = 'YOUR_API_KEY'
# Uncomment below to setup prefix (e.g. Bearer) for API key, if needed
# configuration.api_key_prefix['LEETCODE_SESSION'] = 'Bearer'
# Configure API key authorization: headerCSRF
configuration = leetcode.Configuration()
configuration.api_key['x-csrftoken'] = 'YOUR_API_KEY'
# Uncomment below to setup prefix (e.g. Bearer) for API key, if needed
# configuration.api_key_prefix['x-csrftoken'] = 'Bearer'
# Configure API key authorization: referer
configuration = leetcode.Configuration()
configuration.api_key['Referer'] = 'YOUR_API_KEY'
# Uncomment below to setup prefix (e.g. Bearer) for API key, if needed
# configuration.api_key_prefix['Referer'] = 'Bearer'
# create an instance of the API class
api_instance = leetcode.DefaultApi(leetcode.ApiClient(configuration))
topic = 'topic_example' # str |
try:
api_response = api_instance.api_problems_topic_get(topic)
pprint(api_response)
except ApiException as e:
print("Exception when calling DefaultApi->api_problems_topic_get: %s\n" % e)
# Configure API key authorization: cookieCSRF
configuration = leetcode.Configuration()
configuration.api_key['csrftoken'] = 'YOUR_API_KEY'
# Uncomment below to setup prefix (e.g. Bearer) for API key, if needed
# configuration.api_key_prefix['csrftoken'] = 'Bearer'
# Configure API key authorization: cookieSession
configuration = leetcode.Configuration()
configuration.api_key['LEETCODE_SESSION'] = 'YOUR_API_KEY'
# Uncomment below to setup prefix (e.g. Bearer) for API key, if needed
# configuration.api_key_prefix['LEETCODE_SESSION'] = 'Bearer'
# Configure API key authorization: headerCSRF
configuration = leetcode.Configuration()
configuration.api_key['x-csrftoken'] = 'YOUR_API_KEY'
# Uncomment below to setup prefix (e.g. Bearer) for API key, if needed
# configuration.api_key_prefix['x-csrftoken'] = 'Bearer'
# Configure API key authorization: referer
configuration = leetcode.Configuration()
configuration.api_key['Referer'] = 'YOUR_API_KEY'
# Uncomment below to setup prefix (e.g. Bearer) for API key, if needed
# configuration.api_key_prefix['Referer'] = 'Bearer'
# create an instance of the API class
api_instance = leetcode.DefaultApi(leetcode.ApiClient(configuration))
body = leetcode.GraphqlQuery() # GraphqlQuery | GraphQL query (optional)
try:
api_response = api_instance.graphql_post(body=body)
pprint(api_response)
except ApiException as e:
print("Exception when calling DefaultApi->graphql_post: %s\n" % e)
# Configure API key authorization: cookieCSRF
configuration = leetcode.Configuration()
configuration.api_key['csrftoken'] = 'YOUR_API_KEY'
# Uncomment below to setup prefix (e.g. Bearer) for API key, if needed
# configuration.api_key_prefix['csrftoken'] = 'Bearer'
# Configure API key authorization: cookieSession
configuration = leetcode.Configuration()
configuration.api_key['LEETCODE_SESSION'] = 'YOUR_API_KEY'
# Uncomment below to setup prefix (e.g. Bearer) for API key, if needed
# configuration.api_key_prefix['LEETCODE_SESSION'] = 'Bearer'
# Configure API key authorization: headerCSRF
configuration = leetcode.Configuration()
configuration.api_key['x-csrftoken'] = 'YOUR_API_KEY'
# Uncomment below to setup prefix (e.g. Bearer) for API key, if needed
# configuration.api_key_prefix['x-csrftoken'] = 'Bearer'
# Configure API key authorization: referer
configuration = leetcode.Configuration()
configuration.api_key['Referer'] = 'YOUR_API_KEY'
# Uncomment below to setup prefix (e.g. Bearer) for API key, if needed
# configuration.api_key_prefix['Referer'] = 'Bearer'
# create an instance of the API class
api_instance = leetcode.DefaultApi(leetcode.ApiClient(configuration))
problem = 'problem_example' # str |
body = leetcode.TestSubmission() # TestSubmission | Solution to test (optional)
try:
api_response = api_instance.problems_problem_interpret_solution_post(problem, body=body)
pprint(api_response)
except ApiException as e:
print("Exception when calling DefaultApi->problems_problem_interpret_solution_post: %s\n" % e)
# Configure API key authorization: cookieCSRF
configuration = leetcode.Configuration()
configuration.api_key['csrftoken'] = 'YOUR_API_KEY'
# Uncomment below to setup prefix (e.g. Bearer) for API key, if needed
# configuration.api_key_prefix['csrftoken'] = 'Bearer'
# Configure API key authorization: cookieSession
configuration = leetcode.Configuration()
configuration.api_key['LEETCODE_SESSION'] = 'YOUR_API_KEY'
# Uncomment below to setup prefix (e.g. Bearer) for API key, if needed
# configuration.api_key_prefix['LEETCODE_SESSION'] = 'Bearer'
# Configure API key authorization: headerCSRF
configuration = leetcode.Configuration()
configuration.api_key['x-csrftoken'] = 'YOUR_API_KEY'
# Uncomment below to setup prefix (e.g. Bearer) for API key, if needed
# configuration.api_key_prefix['x-csrftoken'] = 'Bearer'
# Configure API key authorization: referer
configuration = leetcode.Configuration()
configuration.api_key['Referer'] = 'YOUR_API_KEY'
# Uncomment below to setup prefix (e.g. Bearer) for API key, if needed
# configuration.api_key_prefix['Referer'] = 'Bearer'
# create an instance of the API class
api_instance = leetcode.DefaultApi(leetcode.ApiClient(configuration))
problem = 'problem_example' # str |
body = leetcode.Submission() # Submission | Solution to test (optional)
try:
api_response = api_instance.problems_problem_submit_post(problem, body=body)
pprint(api_response)
except ApiException as e:
print("Exception when calling DefaultApi->problems_problem_submit_post: %s\n" % e)
# Configure API key authorization: cookieCSRF
configuration = leetcode.Configuration()
configuration.api_key['csrftoken'] = 'YOUR_API_KEY'
# Uncomment below to setup prefix (e.g. Bearer) for API key, if needed
# configuration.api_key_prefix['csrftoken'] = 'Bearer'
# Configure API key authorization: cookieSession
configuration = leetcode.Configuration()
configuration.api_key['LEETCODE_SESSION'] = 'YOUR_API_KEY'
# Uncomment below to setup prefix (e.g. Bearer) for API key, if needed
# configuration.api_key_prefix['LEETCODE_SESSION'] = 'Bearer'
# Configure API key authorization: headerCSRF
configuration = leetcode.Configuration()
configuration.api_key['x-csrftoken'] = 'YOUR_API_KEY'
# Uncomment below to setup prefix (e.g. Bearer) for API key, if needed
# configuration.api_key_prefix['x-csrftoken'] = 'Bearer'
# Configure API key authorization: referer
configuration = leetcode.Configuration()
configuration.api_key['Referer'] = 'YOUR_API_KEY'
# Uncomment below to setup prefix (e.g. Bearer) for API key, if needed
# configuration.api_key_prefix['Referer'] = 'Bearer'
# create an instance of the API class
api_instance = leetcode.DefaultApi(leetcode.ApiClient(configuration))
id = leetcode.Id() # Id | Either submission id (int) or interpretation id (string)
try:
api_response = api_instance.submissions_detail_id_check_get(id)
pprint(api_response)
except ApiException as e:
print("Exception when calling DefaultApi->submissions_detail_id_check_get: %s\n" % e)
All URIs are relative to https://leetcode.com
Class | Method | HTTP request | Description |
---|---|---|---|
DefaultApi | api_problems_topic_get | GET /api/problems/{topic}/ | |
DefaultApi | graphql_post | POST /graphql | |
DefaultApi | problems_problem_interpret_solution_post | POST /problems/{problem}/interpret_solution/ | |
DefaultApi | problems_problem_submit_post | POST /problems/{problem}/submit/ | |
DefaultApi | submissions_detail_id_check_get | GET /submissions/detail/{id}/check/ |
- BaseSubmissionResult
- Difficulty
- GraphqlData
- GraphqlQuery
- GraphqlQueryVariables
- GraphqlQuestionCodeSnippet
- GraphqlQuestionContributor
- GraphqlQuestionDetail
- GraphqlQuestionSolution
- GraphqlQuestionTopicTag
- GraphqlResponse
- GraphqlUser
- Id
- InlineResponse200
- Interpretation
- OneOfid
- OneOfinlineResponse200
- Problems
- Stat
- StatStatusPair
- Submission
- SubmissionId
- SubmissionResult
- TestSubmission
- TestSubmissionResult
- Type: API key
- API key parameter name: csrftoken
- Location: URL query string
- Type: API key
- API key parameter name: LEETCODE_SESSION
- Location: URL query string
- Type: API key
- API key parameter name: x-csrftoken
- Location: HTTP header
- Type: API key
- API key parameter name: Referer
- Location: HTTP header