diff --git a/README.md b/README.md index e0c30c0..a3f5bfd 100644 --- a/README.md +++ b/README.md @@ -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. @@ -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 ``` @@ -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: diff --git a/leetcode/cli.py b/leetcode/cli.py index 78864bf..8a9178a 100644 --- a/leetcode/cli.py +++ b/leetcode/cli.py @@ -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: diff --git a/leetcode/client/leetcode.py b/leetcode/client/leetcode.py index 4279434..6f00a21 100644 --- a/leetcode/client/leetcode.py +++ b/leetcode/client/leetcode.py @@ -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'] diff --git a/leetcode/client/process.py b/leetcode/client/process.py index 35ca2a8..5cafb6d 100644 --- a/leetcode/client/process.py +++ b/leetcode/client/process.py @@ -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) diff --git a/leetcode/client/quiz.py b/leetcode/client/quiz.py index 49e5362..8b911a5 100644 --- a/leetcode/client/quiz.py +++ b/leetcode/client/quiz.py @@ -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 @@ -24,10 +25,11 @@ 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!) { @@ -35,6 +37,7 @@ def load(self): title titleSlug questionId + questionFrontendId content difficulty stats @@ -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) @@ -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', diff --git a/leetcode/helper/common.py b/leetcode/helper/common.py index 7f86fa9..0c8ec5e 100644 --- a/leetcode/helper/common.py +++ b/leetcode/helper/common.py @@ -7,6 +7,7 @@ LANG_MAPPING = { 'C++': 'cpp', 'Python': 'python', + 'Python3': 'python3', 'Java': 'java', 'C': 'c', 'C#': 'csharp', diff --git a/setup.py b/setup.py index bed7ae9..0b98e64 100644 --- a/setup.py +++ b/setup.py @@ -10,7 +10,7 @@ setup( name="terminal-leetcode", - version="0.0.18", + version="0.0.20", author="Liyun Xiu", author_email="chishui2@gmail.com", description="A terminal based leetcode website viewer",