From 0c60398018b3cfe3419a82226f114f1a8bc35a21 Mon Sep 17 00:00:00 2001 From: Sergey Mishin Date: Sat, 25 Sep 2021 02:38:46 +0000 Subject: [PATCH] Fix mess with ItemId/QuestionId --- lib/bot_ydb.py | 9 ++++++--- lib/common.py | 9 +++++---- lib/leetcode_api.py | 17 +++++++++-------- lib/tasks_actions.py | 2 +- tests/common_tests.py | 2 +- 5 files changed, 22 insertions(+), 17 deletions(-) diff --git a/lib/bot_ydb.py b/lib/bot_ydb.py index d7eea8c..9c5e9c7 100644 --- a/lib/bot_ydb.py +++ b/lib/bot_ydb.py @@ -60,7 +60,7 @@ def execute_select_query(session): query = ''' DECLARE $dateId AS Uint64; - SELECT title, text, questionId, hints + SELECT title, text, questionId, itemId, hints FROM dailyQuestion WHERE id = $dateId; ''' @@ -79,6 +79,7 @@ def execute_select_query(session): BotLeetCodeTask( dateId, result[0].questionId, + result[0].itemId, result[0].title.decode('UTF-8'), result[0].text.decode('UTF-8'), json.loads(result[0].hints.decode('UTF-8')), @@ -94,18 +95,20 @@ def do_insert(session): query = ''' DECLARE $dateId AS Uint64; DECLARE $questionId AS Uint64; + DECLARE $itemId AS Uint64; DECLARE $title AS String; DECLARE $content AS String; DECLARE $hints AS String; - REPLACE INTO dailyQuestion (id, questionId, title, text, hints) - VALUES ($dateId, $questionId, $title, $content, $hints); + REPLACE INTO dailyQuestion (id, questionId, itemId, title, text, hints) + VALUES ($dateId, $questionId, $itemId, $title, $content, $hints); ''' prepared_query = session.prepare(query) return session.transaction(ydb.SerializableReadWrite()).execute( prepared_query, { '$dateId': task.DateId, '$questionId': task.QuestionId, + '$itemId': task.ItemId, '$title': task.Title.encode('UTF-8'), '$content': task.Content.encode('UTF-8'), '$hints': json.dumps(task.Hints).encode('UTF-8'), diff --git a/lib/common.py b/lib/common.py index cca0295..f7d4768 100644 --- a/lib/common.py +++ b/lib/common.py @@ -19,12 +19,12 @@ class SendMessageError(Error): class BotLeetCodeTask(LeetCodeTask): - def __init__(self, dateId: int = 0, questionId: int = 0, title: str = '', content: str = '', hints: List[str] = []) -> None: + def __init__(self, dateId: int = 0, questionId: int = 0, itemId: int = 0, title: str = '', content: str = '', hints: List[str] = []) -> None: self.DateId = dateId - LeetCodeTask.__init__(self, questionId, title, content, hints) + LeetCodeTask.__init__(self, questionId, itemId, title, content, hints) def fromLeetCodeTask(self, task: LeetCodeTask): - LeetCodeTask.__init__(self, task.QuestionId, task.Title, task.Content, task.Hints) + LeetCodeTask.__init__(self, task.QuestionId, task.ItemId, task.Title, task.Content, task.Hints) def __repr__(self) -> str: return f'DateId: {self.DateId}\n' + LeetCodeTask.__repr__(self) @@ -43,13 +43,14 @@ def generateHintsInlineKeyboard(self) -> str: if level: listOfHints.append(level.copy()) level = [] - listOfHints = [[{'text': 'See task on LeetCode website', 'url': f'https://leetcode.com/explore/item/{self.QuestionId}'}]] + listOfHints + listOfHints = [[{'text': 'See task on LeetCode website', 'url': f'https://leetcode.com/explore/item/{self.ItemId}'}]] + listOfHints return json.dumps({'inline_keyboard': listOfHints}) def toJsonStr(self) -> str: return json.dumps({ 'dateId': self.DateId, 'questionId': self.QuestionId, + 'itemId': self.QuestionId, 'title': self.Title, 'content': self.Content, 'hints': self.Hints, diff --git a/lib/leetcode_api.py b/lib/leetcode_api.py index bf2a31a..6c7d9fa 100644 --- a/lib/leetcode_api.py +++ b/lib/leetcode_api.py @@ -5,14 +5,15 @@ class LeetCodeTask: # pylint: disable=too-few-public-methods - def __init__(self, questionId: int = 0, title: str = '', content: str = '', hints: List[str] = []) -> None: + def __init__(self, questionId: int = 0, itemId: int = 0, title: str = '', content: str = '', hints: List[str] = []) -> None: self.QuestionId = questionId + self.ItemId = itemId self.Title = title self.Content = content self.Hints = hints def __repr__(self) -> str: - return f'QuesetionId: {self.QuestionId}\nTitle: {self.Title}\nContent: {self.Content}\nHints: {self.Hints}' + return f'QuesetionId: {self.QuestionId}\nItemId: {self.ItemId}\nTitle: {self.Title}\nContent: {self.Content}\nHints: {self.Hints}' MONTH_NAMES = { @@ -72,7 +73,7 @@ class NonPredictableError(Error): pass -def getDailyTaskId(targetDate: datetime) -> int: +def getDailyTaskId(targetDate: datetime) -> str: GET_CHAPTERS['variables']['cardSlug'] = generateCardSlug(targetDate) r = requestGraphQl(GET_CHAPTERS) js = r.json() @@ -90,8 +91,8 @@ def getDailyTaskId(targetDate: datetime) -> int: return js['data']['chapters'][chapter]['items'][item]['id'] -def getQestionSlug(questionId: int) -> str: - GET_SLUG['variables']['itemId'] = questionId +def getQestionSlug(itemId: str) -> str: + GET_SLUG['variables']['itemId'] = itemId r = requestGraphQl(GET_SLUG) return r.json()['data']['item']['question']['titleSlug'] @@ -107,7 +108,7 @@ def generateCardSlug(date: datetime) -> str: def getTaskOfTheDay(targetDate: datetime) -> LeetCodeTask: - questionId = getDailyTaskId(targetDate) - s = getQestionSlug(questionId) + itemId = getDailyTaskId(targetDate) + s = getQestionSlug(itemId) question = getQuestionDetails(s) - return LeetCodeTask(int(questionId), question['questionTitle'], question['content'], question['hints']) + return LeetCodeTask(int(question['questionId']), int(itemId), question['questionTitle'], question['content'], question['hints']) diff --git a/lib/tasks_actions.py b/lib/tasks_actions.py index d158d58..65ddf22 100644 --- a/lib/tasks_actions.py +++ b/lib/tasks_actions.py @@ -31,7 +31,7 @@ def saveToCache(task: BotLeetCodeTask) -> None: def loadTaskFromJsonStr(jsonStr: str) -> BotLeetCodeTask: jsonTask = json.loads(jsonStr) - return BotLeetCodeTask(jsonTask['dateId'], jsonTask['questionId'], jsonTask['title'], jsonTask['content'], jsonTask['hints']) + return BotLeetCodeTask(jsonTask['dateId'], jsonTask['questionId'], jsonTask['itemId'], jsonTask['title'], jsonTask['content'], jsonTask['hints']) def getFixedTaskForDate(targetDate: datetime) -> BotLeetCodeTask: diff --git a/tests/common_tests.py b/tests/common_tests.py index 97b1291..5d61d4a 100644 --- a/tests/common_tests.py +++ b/tests/common_tests.py @@ -60,7 +60,7 @@ def test_fixTagsAndImages() -> None: with patch('lib.common.replaceImgWithA', return_value = 'replaced A') as patched_replaceA, \ patch('lib.common.removeUnsupportedTags', return_value = 'removed tags') as patched_removeTags: - res = fixTagsAndImages(BotLeetCodeTask(1, 2, 'test title', 'test content')) + res = fixTagsAndImages(BotLeetCodeTask(1, 2, 3, 'test title', 'test content')) assert(res.Content == 'replaced A') assert(res.Title == 'removed tags') patched_replaceA.assert_called_once_with('removed tags')