diff --git a/searchgram/engine.py b/searchgram/engine.py index 53baa1a..8ba48bf 100644 --- a/searchgram/engine.py +++ b/searchgram/engine.py @@ -38,7 +38,7 @@ def check_ignore(message): return True @staticmethod - def __clean_user(user: "str"): + def clean_user(user: "str"): if user is None: return None with contextlib.suppress(Exception): @@ -52,8 +52,89 @@ def __clean_user(user: "str"): def upsert(self, message): pass - def search(self, keyword, _type=None, user=None, page=1, mode=None): - pass + def search(self, keyword, _type=None, user=None, page=1, mode=None) -> dict: + """ + + :param keyword: + :param _type: ChatType.["BOT", "CHANNEL", "GROUP", "PRIVATE", "SUPERGROUP"] + :param user: username or id + :param page: + :param mode: exact match + :return: + """ + + return { + "hits": [ + { + "ID": "123456-1114100", + "_": "Message", + "id": 1114100, + "from_user": { + "_": "User", + "id": 260260121, + "is_self": True, + "is_contact": False, + "is_mutual_contact": False, + "is_deleted": False, + "is_bot": False, + "is_verified": False, + "is_restricted": False, + "is_scam": False, + "is_fake": False, + "is_support": False, + "is_premium": True, + "first_name": "Benny小可爱", + "status": "UserStatus.ONLINE", + "next_offline_date": "2023-11-18 16:35:09", + "username": "BennyThink", + "emoji_status": {"_": "EmojiStatus", "custom_emoji_id": 5377556559356238807}, + "dc_id": 5, + "phone_number": "*********", + "photo": { + "_": "ChatPhoto", + "small_file_id": "AQADBQADtacxGxlBgw8AEAIAAxlBgw8ABNG47cEZeO_bAAQeBA", + "small_photo_unique_id": "AgADtacxGxlBgw8", + "big_file_id": "AQADBQADtacxGxlBgw8AEAMAAxlBgw8ABNG47cEZeO_bAAQeBA", + "big_photo_unique_id": "AgADtacxGxlBgw8", + }, + }, + "date": "2023-11-18 16:30:21", + "chat": { + "_": "Chat", + "id": 123456, + "type": "ChatType.PRIVATE", + "is_verified": False, + "is_restricted": False, + "is_scam": False, + "is_fake": False, + "is_support": False, + "username": "name_unknow", + "first_name": "ok", + "photo": { + "_": "ChatPhoto", + "small_file_id": "edfgh567", + "small_photo_unique_id": "ghjgh", + "big_file_id": "AQAeBA", + "big_photo_unique_id": "AgyebeVQ", + }, + "dc_id": 5, + }, + "mentioned": False, + "scheduled": False, + "from_scheduled": False, + "has_protected_content": False, + "text": "测试仪你", + "outgoing": True, + "timestamp": 1700321421, + } + ], + "query": "测试", + "processingTimeMs": 1, + "hitsPerPage": 10, + "page": 1, + "totalPages": 1, + "totalHits": 1, + } def ping(self) -> str: pass diff --git a/searchgram/meili.py b/searchgram/meili.py index 2e382c7..68325d2 100644 --- a/searchgram/meili.py +++ b/searchgram/meili.py @@ -36,10 +36,10 @@ def upsert(self, message): data = self.set_uid(message) self.client.index("telegram").add_documents([data], primary_key="ID") - def search(self, keyword, _type=None, user=None, page=1, mode=None): + def search(self, keyword, _type=None, user=None, page=1, mode=None) -> dict: if mode: keyword = f'"{keyword}"' - user = self.__clean_user(user) + user = self.clean_user(user) params = { "hitsPerPage": 10, "page": page, diff --git a/searchgram/mongo.py b/searchgram/mongo.py index 1322734..3fe434b 100644 --- a/searchgram/mongo.py +++ b/searchgram/mongo.py @@ -67,12 +67,12 @@ def search2(self, keyword, _type=None, user=None, page=1, mode=None): return results def ping(self) -> str: - count = self.col.count_documents({}) + count = self.chat.count_documents({}) size = self.db.command("dbstats")["storageSize"] return f"{count} messages, {sizeof_fmt(size)}" def clear_db(self): - pass + self.client.drop_database("telegram") if __name__ == "__main__":