From f3a693b32bc675481ce565d98d8aab336d40b99e Mon Sep 17 00:00:00 2001 From: Eldar Mametov Date: Sun, 23 Jun 2024 17:00:39 +0300 Subject: [PATCH] feat: add sending data to the server --- .gitignore | 1 + pyproject.toml | 2 ++ pyrogram_parser.py | 70 ++++++++++++++++++++++++++++++++++++++-------- 3 files changed, 62 insertions(+), 11 deletions(-) diff --git a/.gitignore b/.gitignore index 526be24..b091641 100644 --- a/.gitignore +++ b/.gitignore @@ -19,3 +19,4 @@ public.pem # Secrets & Output *.session parser.json +test_site.py diff --git a/pyproject.toml b/pyproject.toml index 3132f73..d62dacc 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -10,6 +10,8 @@ readme = "README.md" python = "^3.12" Pyrogram = "^2.0.106" pydantic = "^2.7.4" +httpx = "^0.27.0" +asyncio = "^3.4.3" [tool.poetry.group.dev] optional = true diff --git a/pyrogram_parser.py b/pyrogram_parser.py index f19f87c..12268fb 100644 --- a/pyrogram_parser.py +++ b/pyrogram_parser.py @@ -2,11 +2,16 @@ import json from pydantic import BaseModel from datetime import datetime +import httpx +import os +import asyncio # File for saving data filename = "parser.json" # specify the name of the session file for userbot app = Client("my_account") +# The url of the server +url = "http://127.0.0.1:5000/api/messages" class Chat(BaseModel): @@ -31,19 +36,60 @@ def serializableDict(self): return new_dict -def saveJson(message): +async def saveJson(message_save): """Saves the message to a JSON file""" - message_save = StructureMessage.model_validate_json(message) + async with asyncio.Lock(): + if not os.path.exists(filename): + with open(filename, "w", encoding="utf-8") as file: + file.write("[]") + # Opening a file and writing data + with open(filename, "r+", encoding="utf-8") as file: + try: + data = json.load(file) + except json.JSONDecodeError: + data = [] + data.append(message_save.serializableDict()) + file.seek(0) + json.dump(data, file, ensure_ascii=False, indent=4) - # Opening a file and writing data - with open(filename, "r+", encoding="utf-8") as file: + +async def resend_all_message(): + """Resending messages to the server""" + async with asyncio.Lock(): + if not os.path.exists(filename): + return + with open(filename, "r+", encoding="utf-8") as file: + try: + data = json.load(file) + except json.JSONDecodeError: + data = [] + if not data: + return + async with httpx.AsyncClient() as client: + for history_message in data: + response = await client.post(url, json=history_message) + print( + f"Status code: {response.status_code}, message id: {history_message['id']}" + ) + if response.status_code != 200: + return + file.seek(0) + json.dump([], file, ensure_ascii=False, indent=4) + file.truncate() + + +async def send_to_server(message_save): + """Send message to the server""" + async with httpx.AsyncClient() as client: try: - data = json.load(file) - except json.JSONDecodeError: - data = [] - data.append(message_save.serializableDict()) - file.seek(0) - json.dump(data, file, ensure_ascii=False, indent=4) + response = await client.post(url, json=message_save.serializableDict()) + print(f"Status code: {response.status_code}, message id: {message_save.id}") + if response.status_code == 200: + await resend_all_message() + else: + await saveJson(message_save) + except httpx.RequestError: + await saveJson(message_save) # Handler for new messages in the specified chats @@ -54,7 +100,9 @@ async def new_message_handler(client, message): """Processes new messages and saves them if there is a text or signature""" if message.text or message.caption: message = str(message) - saveJson(message) + message_save = StructureMessage.model_validate_json(message) + await send_to_server(message_save) + # saveJson(message) if __name__ == "__main__":