Skip to content

Commit

Permalink
Fix mess with ItemId/QuestionId
Browse files Browse the repository at this point in the history
  • Loading branch information
dartkron committed Sep 25, 2021
1 parent b690264 commit 0c60398
Show file tree
Hide file tree
Showing 5 changed files with 22 additions and 17 deletions.
9 changes: 6 additions & 3 deletions lib/bot_ydb.py
Original file line number Diff line number Diff line change
Expand Up @@ -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;
'''
Expand All @@ -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')),
Expand All @@ -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'),
Expand Down
9 changes: 5 additions & 4 deletions lib/common.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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,
Expand Down
17 changes: 9 additions & 8 deletions lib/leetcode_api.py
Original file line number Diff line number Diff line change
Expand Up @@ -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 = {
Expand Down Expand Up @@ -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()
Expand All @@ -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']

Expand All @@ -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'])
2 changes: 1 addition & 1 deletion lib/tasks_actions.py
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down
2 changes: 1 addition & 1 deletion tests/common_tests.py
Original file line number Diff line number Diff line change
Expand Up @@ -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')
Expand Down

0 comments on commit 0c60398

Please sign in to comment.