Skip to content

Commit

Permalink
Support Python3 & fix sumbit with wrong id
Browse files Browse the repository at this point in the history
  • Loading branch information
chishui committed Sep 25, 2021
1 parent 11e29a6 commit 3fee04a
Show file tree
Hide file tree
Showing 7 changed files with 18 additions and 7 deletions.
5 changes: 3 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@ This project is inspired by [RTV](https://github.com/michael-lazar/rtv).
[![PyPI](https://img.shields.io/pypi/v/nine.svg?maxAge=2592000)](https://pypi.python.org/pypi/terminal-leetcode)
[![PyPI](https://img.shields.io/badge/python-3.7-blue.svg?maxAge=2592000)](https://pypi.python.org/pypi/terminal-leetcode)

[Discuss in Slack](https://terminal-leetcode.slack.com)
--------------
# March 22th Update
#### Add code submit function.
Expand All @@ -29,7 +28,7 @@ edit and share. You can find tag file of Facebook from tags directory.
# Installation
Install with pip
```
$ pip install terminal-leetcode
$ pip3 install terminal-leetcode
```
Clone the repository
```
Expand All @@ -53,6 +52,8 @@ This option will get your cookies from your browser and use those for any reques
you need to sign in your account from your browser first. There may be some limitations, please refer to pycookiecheat
for its [documentation](https://github.com/n8henrie/pycookiecheat)

On Mac for the first time use, it will pop up a window and ask to input password of your computer.

#### Option 2 (No longer available)
To login you need to create a config.cfg file in folder ~/.config/leetcode.
Input your username and password in config.cfg as:
Expand Down
2 changes: 1 addition & 1 deletion leetcode/cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ def submit(self):
try:
parser = argparse.ArgumentParser(
description='submit your code for online judge',
usage='leetcode submit --id [problem id]')
usage='leetcode submit --id [problem frontend id]')
parser.add_argument('--id', type=int, required=True, help='set problem id')
args = parser.parse_args(sys.argv[2:])
if args.id:
Expand Down
1 change: 1 addition & 0 deletions leetcode/client/leetcode.py
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@ def _parse_home_API(self, text):
data.title = quiz['stat']['question__title']
data.slug = quiz['stat']['question__title_slug']
data.id = quiz['stat']['frontend_question_id']
data.real_quiz_id = data.id # default real_quiz_id to frontend id
data.locked = not self.is_paid and quiz['paid_only']
data.difficulty = difficulty[quiz['difficulty']['level']]
data.favorite = quiz['is_favor']
Expand Down
2 changes: 2 additions & 0 deletions leetcode/client/process.py
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,8 @@ def submit(self, id):
break
if not quiz:
return (False, None)
else:
quiz.load()

code = self.get_code_from_quiz_id(id)
success, text_or_id = quiz.submit(code)
Expand Down
12 changes: 9 additions & 3 deletions leetcode/client/quiz.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,8 @@

class Quiz(object):
def __init__(self, auth):
self.id = None
self.id = None # question frontend id
self.real_quiz_id = None # question id of backend
self.title = None
self.content = None
self.sample_code = None
Expand All @@ -24,17 +25,19 @@ def __init__(self, auth):
self.html_content = None
self.auth = auth
self.slug = None
self.already_load = False
self.logger = logging.getLogger(__name__)

def load(self):
if not self.auth.is_login:
if not self.auth.is_login or self.already_load:
return False

query = """query questionData($titleSlug: String!) {
question(titleSlug: $titleSlug) {
title
titleSlug
questionId
questionFrontendId
content
difficulty
stats
Expand Down Expand Up @@ -85,10 +88,13 @@ def load(self):
self.html_content = obj["data"]["question"]["content"]
content = obj["data"]["question"]["content"]
bs = BeautifulSoup(content, "lxml")
self.id = obj["data"]["question"]["questionFrontendId"]
self.real_quiz_id = obj["data"]["question"]["questionId"]
self.content = bs.get_text()
self.content = self.content.replace(chr(13), '')
self.sample_code = self._get_code_snippet(obj["data"]["question"]["codeSnippets"])
self.tags = map(lambda x: x["name"], obj["data"]["question"]["topicTags"])
self.already_load = True
return True
except Exception:
self.logger.error("Fatal error in main loop", exc_info=True)
Expand All @@ -104,7 +110,7 @@ def _get_code_snippet(self, snippets):
def submit(self, code):
if not self.auth.is_login:
return (False, "")
body = {'question_id': self.id,
body = {'question_id': self.real_quiz_id,
'test_mode': False,
'lang': LANG_MAPPING.get(config.language, 'cpp'),
'judge_type': 'large',
Expand Down
1 change: 1 addition & 0 deletions leetcode/helper/common.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
LANG_MAPPING = {
'C++': 'cpp',
'Python': 'python',
'Python3': 'python3',
'Java': 'java',
'C': 'c',
'C#': 'csharp',
Expand Down
2 changes: 1 addition & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@

setup(
name="terminal-leetcode",
version="0.0.18",
version="0.0.20",
author="Liyun Xiu",
author_email="[email protected]",
description="A terminal based leetcode website viewer",
Expand Down

0 comments on commit 3fee04a

Please sign in to comment.