Repository navigation
Expand file tree
/
Copy pathmain.py
More file actions
100 lines (76 loc) · 4.09 KB
/
Copy pathmain.py
File metadata and controls
100 lines (76 loc) · 4.09 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
import os
import json
import asyncio
import random
import structlog
from datetime import datetime, timezone, timedelta
from workers.fetcher import fetch_profile_status
from notifier.telegram_bot import send_ban_alert, send_admin_alert, send_unban_alert, get_subs, get_users_for_account, dp, get_bot
logger = structlog.get_logger()
DB_STATE_FILE = "db_state.json"
DEFAULT_AVATAR = "https://cdn.pixabay.com/photo/2015/10/05/22/37/blank-profile-picture-973460_1280.png"
def load_state():
if os.path.exists(DB_STATE_FILE):
with open(DB_STATE_FILE, "r") as f: return json.load(f)
return {}
def save_state(state):
with open(DB_STATE_FILE, "w") as f: json.dump(state, f, indent=4)
async def monitor_loop():
logger.info("Iniciando Bucle de Monitoreo Sigiloso...")
while True:
history = load_state()
subs = get_subs()
all_accounts = list(set([acc for accs in subs.values() for acc in accs]))
if not all_accounts:
logger.info("Ninguna cuenta en seguimiento. Esperando...")
await asyncio.sleep(60)
continue
random.shuffle(all_accounts)
for username in all_accounts:
logger.info(f"--- Verificando {username} ---")
try:
result = await fetch_profile_status(username)
current_time = datetime.now(timezone.utc).strftime("%Y-%m-%d %H:%M:%S")
current_ts = datetime.now(timezone.utc).timestamp()
subscribers = get_users_for_account(username)
state_val = history.get(username, {})
if isinstance(state_val, str):
state_val = {"status": state_val, "last_change": current_ts}
prev_status = state_val.get("status")
last_change = state_val.get("last_change", current_ts)
if result["status"] == "ok":
new_status = "active"
follower_count = result["data"].get("follower_count", 0)
avatar = result['data'].get('profile_pic_url_hd', DEFAULT_AVATAR)
if prev_status == "banned":
elapsed = str(timedelta(seconds=current_ts - last_change)).split('.')[0]
for uid in subscribers:
await send_unban_alert(uid, username, follower_count, current_time, elapsed)
state_val["last_change"] = current_ts
state_val["status"] = "active"
history[username] = state_val
save_state(history)
elif result["status"] == "404":
if prev_status != "banned":
elapsed = str(timedelta(seconds=current_ts - last_change)).split('.')[0]
screenshot = result.get("screenshot")
for uid in subscribers:
await send_ban_alert(uid, username, 0, current_time, elapsed, local_image_path=screenshot)
state_val["last_change"] = current_ts
state_val["status"] = "banned"
history[username] = state_val
save_state(history)
elif result["status"] == "blocked":
logger.critical("BLOQUEO DE INSTAGRAM DETECTADO.")
await send_admin_alert(f"🛑 Bloqueo al revisar {username}. Reposo de 12h.")
await asyncio.sleep(43200)
except Exception as e:
logger.error(f"Error procesando {username}: {e}")
await asyncio.sleep(random.uniform(3, 20))
logger.info("Ciclo completo. Iniciando Polling Adaptativo (esperando 10-15 min)...")
await asyncio.sleep(random.uniform(600, 900))
async def main():
asyncio.create_task(monitor_loop())
await dp.start_polling(get_bot())
if __name__ == "__main__":
asyncio.run(main())