-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpyrogram_parser.py
More file actions
123 lines (104 loc) · 3.89 KB
/
pyrogram_parser.py
File metadata and controls
123 lines (104 loc) · 3.89 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
from pyrogram import Client, filters
import json
from pydantic import BaseModel
from datetime import datetime
import httpx
import os
import asyncio
from dotenv import load_dotenv
# Loading parameters from .env
load_dotenv()
# File for saving data
filename = "parser.json"
# specify the name of the session file for userbot
app = Client(os.getenv("PYROGRAM_SESSION_STRING"))
# The url of the server
url = os.getenv("URL")
class Chat(BaseModel):
id: int
type: str
title: str
username: str
class StructureMessage(BaseModel):
id: int
sender_chat: Chat
date: datetime
chat: Chat
text: str | None
caption: str | None
def serializableDict(self):
"""Converts an object into a dictionary with date conversion to an ISO format string"""
new_dict = self.dict()
new_dict["date"] = new_dict["date"].isoformat()
return new_dict
async def saveJson(message_save):
"""Saves the message to a JSON file"""
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 = []
print(f"save message: id = {message_save.id}")
data.append(message_save.serializableDict())
file.seek(0)
json.dump(data, file, ensure_ascii=False, indent=4)
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 not in [200, 201]:
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:
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 in [200, 201]:
await resend_all_message()
else:
await saveJson(message_save)
except httpx.RequestError:
await saveJson(message_save)
# Handler for new messages in the specified chats
@app.on_message(
filters.chat(["@sportinIU", "@opportunitiesforyou", "@test_kanal_capstone"])
)
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_save = StructureMessage.model_validate(message, from_attributes=True)
await send_to_server(message_save)
# Handler for edited messages in the specified chats
@app.on_edited_message(
filters.chat(["@sportinIU", "@opportunitiesforyou", "@test_kanal_capstone"])
)
async def edited_message_handler(client, message):
"""the process for the message that was changed"""
if message.text or message.caption:
message_save = StructureMessage.model_validate(message, from_attributes=True)
await send_to_server(message_save)
if __name__ == "__main__":
app.run()