Created
April 4, 2023 15:17
-
-
Save ashavijit/a1ebca898afce8645aecf1963ba44a69 to your computer and use it in GitHub Desktop.
B0Bot handlers
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| class News: | |
| def __init__(self, id, text, created_at): | |
| self.id = id | |
| self.text = text | |
| self.created_at = created_at | |
| def save_to_db(self): | |
| news_collection = db["news_collection"] | |
| news_collection.insert_one({ | |
| "id": self.id, | |
| "text": self.text, | |
| "created_at": self.created_at | |
| }) | |
| @classmethod | |
| def get_latest_news(cls, keyword): | |
| news_collection = db["news_collection"] | |
| latest_news = news_collection.find_one({'text': {'$regex': f'.*{keyword}.*', '$options': 'i'}}, | |
| sort=[('created_at', pymongo.DESCENDING)]) | |
| if latest_news: | |
| return cls(latest_news['id'], latest_news['text'], latest_news['created_at']) | |
| else: | |
| return None |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment