Skip to content

Commit

Permalink
feat: add sending data to the server
Browse files Browse the repository at this point in the history
  • Loading branch information
Lekski1 committed Jun 23, 2024
1 parent d045efd commit f3a693b
Show file tree
Hide file tree
Showing 3 changed files with 62 additions and 11 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -19,3 +19,4 @@ public.pem
# Secrets & Output
*.session
parser.json
test_site.py
2 changes: 2 additions & 0 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
70 changes: 59 additions & 11 deletions pyrogram_parser.py
Original file line number Diff line number Diff line change
Expand Up @@ -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):
Expand All @@ -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
Expand All @@ -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__":
Expand Down

0 comments on commit f3a693b

Please sign in to comment.