-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.py
More file actions
71 lines (56 loc) · 1.85 KB
/
main.py
File metadata and controls
71 lines (56 loc) · 1.85 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
"""This script collects data"""
from pyrogram import Client
from pyrogram.handlers import UserStatusHandler
import os
from datetime import datetime
import csv
try:
from api_info import *
except ModuleNotFoundError:
print("Please create file 'api_info.py' and put there your api_id and api_hash.")
exit()
if not os.path.exists("pyrogram_session"):
os.makedirs("pyrogram_session")
names = {} # id:(name,lastname,username)
if not os.path.exists("history.csv"):
history = open("history.csv", "a+", encoding="utf-8", newline="")
writer = csv.writer(history)
writer.writerow(
["Time", "Timestamp", "First name", "Last name", "Username", "Status"]
)
else:
history = open("history.csv", "a+", encoding="utf-8", newline="")
writer = csv.writer(history)
def handle_status_change(client, user):
if user.id in names:
info = f"{datetime.now()}: {names[user.id][0]} {names[user.id][1]} ({names[user.id][2]}) is {user.status}"
print(info)
else:
user_details = client.get_users(user.id)
names[user.id] = (
user_details.first_name,
user_details.last_name,
user_details.username,
)
info = f"{datetime.now()}: {user_details.first_name} {user_details.last_name} ({user_details.username}) is {user.status} - [NEW USER, DATA SAVED]"
print(info)
writer.writerow(
[
current_time := datetime.now(),
datetime.timestamp(current_time),
names[user.id][0],
names[user.id][1],
names[user.id][2],
user.status,
]
)
history.flush()
# ----------- Initializing clients ----------------
app = Client(
"spy_session",
workdir="pyrogram_session/",
api_hash=api_hash,
api_id=api_id,
)
app.add_handler(UserStatusHandler(handle_status_change))
app.run()