Spaces:
Paused
Paused
CBV01 commited on
Commit ·
d26ae7a
1
Parent(s): 5ad28da
Fix Pyright errors and typing issues in backend/main.py
Browse files- backend/main.py +35 -30
backend/main.py
CHANGED
|
@@ -53,13 +53,14 @@ else:
|
|
| 53 |
async def async_timeout(delay: float):
|
| 54 |
loop = asyncio.get_running_loop()
|
| 55 |
task = asyncio.current_task()
|
| 56 |
-
handle = loop.call_later(delay, task.cancel)
|
| 57 |
try:
|
| 58 |
yield
|
| 59 |
except asyncio.CancelledError:
|
| 60 |
raise asyncio.TimeoutError()
|
| 61 |
finally:
|
| 62 |
-
handle
|
|
|
|
| 63 |
|
| 64 |
# Simple in-memory log buffer
|
| 65 |
LOG_BUFFER = collections.deque(maxlen=100)
|
|
@@ -170,11 +171,12 @@ async def lifespan(app: FastAPI):
|
|
| 170 |
|
| 171 |
# Shutdown logic
|
| 172 |
print("CORE: Shutting down...")
|
| 173 |
-
poller_task
|
| 174 |
-
|
| 175 |
-
|
| 176 |
-
|
| 177 |
-
|
|
|
|
| 178 |
|
| 179 |
# Close pool
|
| 180 |
await POOL.disconnect_all()
|
|
@@ -227,9 +229,6 @@ async def verify_telegram_info(username: str) -> Dict[str, Any]:
|
|
| 227 |
except:
|
| 228 |
# Fallback to assuming it exists if the check itself fails (e.g. network error)
|
| 229 |
return {"exists": True, "participants_count": 0}
|
| 230 |
-
|
| 231 |
-
# Final default return for safety
|
| 232 |
-
return {"exists": True, "participants_count": 0}
|
| 233 |
|
| 234 |
|
| 235 |
|
|
@@ -249,7 +248,6 @@ async def get_current_admin(user_id: str = Depends(get_current_user_id)):
|
|
| 249 |
return user_id
|
| 250 |
finally:
|
| 251 |
if hasattr(conn, "close"): conn.close()
|
| 252 |
-
return user_id
|
| 253 |
|
| 254 |
app = FastAPI(title="ArkiTel Automation API", lifespan=lifespan)
|
| 255 |
|
|
@@ -314,7 +312,7 @@ async def fetch_tg_profile_data(client: TelegramClient, phone_number: str):
|
|
| 314 |
async def debug_db():
|
| 315 |
url = os.getenv("TURSO_DATABASE_URL", "NOT_SET")
|
| 316 |
token = os.getenv("TURSO_AUTH_TOKEN", "NOT_SET")
|
| 317 |
-
u_str =
|
| 318 |
display_url = "NOT_SET"
|
| 319 |
if u_str != "NOT_SET":
|
| 320 |
d_url = ""
|
|
@@ -379,10 +377,10 @@ async def resolve_tg_entity(client, target):
|
|
| 379 |
# Fallback: Search globally to "prime" the session
|
| 380 |
try:
|
| 381 |
from telethon.tl.functions.messages import SearchGlobalRequest # type: ignore
|
| 382 |
-
from telethon.tl.types import InputMessagesFilterEmpty # type: ignore
|
| 383 |
|
| 384 |
# Search globally for this username
|
| 385 |
-
await client(SearchGlobalRequest(q=username, filter=InputMessagesFilterEmpty(), limit=1))
|
| 386 |
# Try again after search
|
| 387 |
entity = await client.get_entity(username)
|
| 388 |
return entity
|
|
@@ -824,7 +822,7 @@ async def send_code(req: PhoneLoginRequest, user_id: str = Depends(get_current_u
|
|
| 824 |
auth_sessions[auth_key] = {
|
| 825 |
"client": client,
|
| 826 |
"phone_code_hash": send_code_res.phone_code_hash,
|
| 827 |
-
"api_id":
|
| 828 |
"api_hash": api_hash
|
| 829 |
}
|
| 830 |
return {"phone_code_hash": send_code_res.phone_code_hash}
|
|
@@ -948,7 +946,7 @@ async def get_dialogs(req: FetchDialogsRequest, user_id: str = Depends(get_curre
|
|
| 948 |
try:
|
| 949 |
# Fetch all dialogs across main list and all folders (archived=True was restricting to folder 1 only)
|
| 950 |
async with async_timeout(120):
|
| 951 |
-
async for dialog in client.iter_dialogs(
|
| 952 |
# Robust check for any container that isn't a single user
|
| 953 |
if dialog.is_group or dialog.is_channel:
|
| 954 |
did = str(dialog.id)
|
|
@@ -1359,12 +1357,17 @@ async def _scrape_telegram_search(base_query: str, rows: list, queue: Optional[a
|
|
| 1359 |
# RUN ON ALL VARIATIONS: restores 800+ volume
|
| 1360 |
if True:
|
| 1361 |
from telethon.tl.functions.messages import SearchGlobalRequest # type: ignore
|
| 1362 |
-
from telethon.tl.types import InputMessagesFilterEmpty # type: ignore
|
| 1363 |
try:
|
| 1364 |
# Bump limit to 100 for maximum discovery volume
|
| 1365 |
msg_results = await client(SearchGlobalRequest(
|
| 1366 |
q=q,
|
| 1367 |
filter=InputMessagesFilterEmpty(),
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1368 |
limit=100
|
| 1369 |
))
|
| 1370 |
# ZERO-COST REFINEMENT: Cross-reference chats ALREADY in the response.
|
|
@@ -1823,9 +1826,9 @@ async def bulk_join(req: BulkJoinRequest, user_id: str = Depends(get_current_use
|
|
| 1823 |
for g_id in req.group_ids:
|
| 1824 |
try:
|
| 1825 |
await asyncio.sleep(random.randint(10, 15))
|
| 1826 |
-
resolved_id = int(g_id) if
|
| 1827 |
entity = await client.get_entity(resolved_id)
|
| 1828 |
-
await client(JoinChannelRequest(entity))
|
| 1829 |
results['joined'] += 1
|
| 1830 |
except Exception as e:
|
| 1831 |
results['failed'] += 1
|
|
@@ -1918,7 +1921,7 @@ async def bulk_join_stream(
|
|
| 1918 |
|
| 1919 |
yield f"data: {_json.dumps({'type': 'progress', 'idx': idx, 'total': total, 'msg': f'Joining {group_label}...'})}\n\n"
|
| 1920 |
# Use pool-based client
|
| 1921 |
-
await smart_join(client, g_id,
|
| 1922 |
|
| 1923 |
joined = joined + 1
|
| 1924 |
yield f"data: {_json.dumps({'type': 'joined', 'name': group_label, 'idx': idx, 'total': total, 'joined': joined, 'failed': failed})}\n\n"
|
|
@@ -1984,7 +1987,7 @@ async def extract_group_members(req: JoinRequest, user_id: str = Depends(get_cur
|
|
| 1984 |
|
| 1985 |
members = []
|
| 1986 |
try:
|
| 1987 |
-
async for user in client.iter_participants(entity, limit=500):
|
| 1988 |
if user.bot: continue
|
| 1989 |
members.append({
|
| 1990 |
"id": str(user.id),
|
|
@@ -2632,11 +2635,11 @@ async def extract_members(req: ExtractRequest, user_id: str = Depends(get_curren
|
|
| 2632 |
|
| 2633 |
# Resolve entity
|
| 2634 |
target = req.group_id
|
| 2635 |
-
if
|
| 2636 |
entity = await client.get_entity(target)
|
| 2637 |
|
| 2638 |
# Get participants
|
| 2639 |
-
users = await client.get_participants(entity, limit=1000)
|
| 2640 |
|
| 2641 |
participants = []
|
| 2642 |
for u in users:
|
|
@@ -2650,10 +2653,11 @@ async def extract_members(req: ExtractRequest, user_id: str = Depends(get_curren
|
|
| 2650 |
|
| 2651 |
# Save to Leads table
|
| 2652 |
conn = get_db_connection()
|
|
|
|
| 2653 |
for p in participants:
|
| 2654 |
conn.execute(
|
| 2655 |
"INSERT OR IGNORE INTO leads (user_id, group_id, username, first_name, last_name) VALUES (?, ?, ?, ?, ?)",
|
| 2656 |
-
(user_id, str(
|
| 2657 |
)
|
| 2658 |
if hasattr(conn, "commit"): conn.commit()
|
| 2659 |
if hasattr(conn, "close"): conn.close()
|
|
@@ -2960,7 +2964,7 @@ async def get_members(group_id: str, phone_number: Optional[str] = None, user_id
|
|
| 2960 |
members = []
|
| 2961 |
try:
|
| 2962 |
entity = await client.get_entity(group_id)
|
| 2963 |
-
async for user in client.iter_participants(entity, limit=100):
|
| 2964 |
members.append({
|
| 2965 |
"id": user.id,
|
| 2966 |
"first_name": user.first_name,
|
|
@@ -2973,7 +2977,7 @@ async def get_members(group_id: str, phone_number: Optional[str] = None, user_id
|
|
| 2973 |
conn = get_db_connection()
|
| 2974 |
conn.execute(
|
| 2975 |
"INSERT INTO leads (user_id, group_id, username, first_name, last_name) VALUES (?, ?, ?, ?, ?)",
|
| 2976 |
-
(user_id,
|
| 2977 |
)
|
| 2978 |
if hasattr(conn, "commit"): conn.commit()
|
| 2979 |
if hasattr(conn, "close"): conn.close()
|
|
@@ -3419,11 +3423,11 @@ async def task_poller():
|
|
| 3419 |
exclude_list = list(set([str(e).strip() for e in (exclude_list if isinstance(exclude_list, list) else [])]))
|
| 3420 |
|
| 3421 |
def recursive_spin(text: str) -> str:
|
| 3422 |
-
text_obj =
|
| 3423 |
while '{' in text_obj and '}' in text_obj:
|
| 3424 |
# Simple regex for leaf-level spintax: {a|b|c}
|
| 3425 |
text_obj = re.sub(r'\{([^{}]*)\}', lambda m: random.choice(m.group(1).split('|')), text_obj)
|
| 3426 |
-
return
|
| 3427 |
|
| 3428 |
try:
|
| 3429 |
processed_list = ast.literal_eval(processed_groups_str) if processed_groups_str else []
|
|
@@ -3494,7 +3498,7 @@ async def task_poller():
|
|
| 3494 |
failed_list.append({"id": group, "name": group, "reason": "Not a Member"})
|
| 3495 |
continue
|
| 3496 |
|
| 3497 |
-
res_title = getattr(entity, 'title',
|
| 3498 |
log_debug(f"BKG_TASK[{task_id}]: Target resolved -> {res_title}")
|
| 3499 |
except Exception as e:
|
| 3500 |
log_debug(f"BKG_TASK[{task_id}]: Mapping Error for {group}: {str(e)}")
|
|
@@ -3532,7 +3536,8 @@ async def task_poller():
|
|
| 3532 |
# SPAM PREVENTION: Check last message
|
| 3533 |
try:
|
| 3534 |
last_msgs = await client.get_messages(entity, limit=1)
|
| 3535 |
-
|
|
|
|
| 3536 |
log_debug(f"BKG_TASK[{task_id}]: Skipping {target_id} - Last message was from this account.")
|
| 3537 |
continue
|
| 3538 |
except Exception as e:
|
|
|
|
| 53 |
async def async_timeout(delay: float):
|
| 54 |
loop = asyncio.get_running_loop()
|
| 55 |
task = asyncio.current_task()
|
| 56 |
+
handle = loop.call_later(delay, task.cancel) if task else None
|
| 57 |
try:
|
| 58 |
yield
|
| 59 |
except asyncio.CancelledError:
|
| 60 |
raise asyncio.TimeoutError()
|
| 61 |
finally:
|
| 62 |
+
if handle:
|
| 63 |
+
handle.cancel()
|
| 64 |
|
| 65 |
# Simple in-memory log buffer
|
| 66 |
LOG_BUFFER = collections.deque(maxlen=100)
|
|
|
|
| 171 |
|
| 172 |
# Shutdown logic
|
| 173 |
print("CORE: Shutting down...")
|
| 174 |
+
if poller_task is not None:
|
| 175 |
+
poller_task.cancel()
|
| 176 |
+
try:
|
| 177 |
+
await poller_task
|
| 178 |
+
except asyncio.CancelledError:
|
| 179 |
+
print("CORE: Poller task cancelled.")
|
| 180 |
|
| 181 |
# Close pool
|
| 182 |
await POOL.disconnect_all()
|
|
|
|
| 229 |
except:
|
| 230 |
# Fallback to assuming it exists if the check itself fails (e.g. network error)
|
| 231 |
return {"exists": True, "participants_count": 0}
|
|
|
|
|
|
|
|
|
|
| 232 |
|
| 233 |
|
| 234 |
|
|
|
|
| 248 |
return user_id
|
| 249 |
finally:
|
| 250 |
if hasattr(conn, "close"): conn.close()
|
|
|
|
| 251 |
|
| 252 |
app = FastAPI(title="ArkiTel Automation API", lifespan=lifespan)
|
| 253 |
|
|
|
|
| 312 |
async def debug_db():
|
| 313 |
url = os.getenv("TURSO_DATABASE_URL", "NOT_SET")
|
| 314 |
token = os.getenv("TURSO_AUTH_TOKEN", "NOT_SET")
|
| 315 |
+
u_str = url if url else "NOT_SET"
|
| 316 |
display_url = "NOT_SET"
|
| 317 |
if u_str != "NOT_SET":
|
| 318 |
d_url = ""
|
|
|
|
| 377 |
# Fallback: Search globally to "prime" the session
|
| 378 |
try:
|
| 379 |
from telethon.tl.functions.messages import SearchGlobalRequest # type: ignore
|
| 380 |
+
from telethon.tl.types import InputMessagesFilterEmpty, InputPeerEmpty # type: ignore
|
| 381 |
|
| 382 |
# Search globally for this username
|
| 383 |
+
await client(SearchGlobalRequest(q=username, filter=InputMessagesFilterEmpty(), min_date=None, max_date=None, offset_rate=0, offset_peer=InputPeerEmpty(), offset_id=0, limit=1))
|
| 384 |
# Try again after search
|
| 385 |
entity = await client.get_entity(username)
|
| 386 |
return entity
|
|
|
|
| 822 |
auth_sessions[auth_key] = {
|
| 823 |
"client": client,
|
| 824 |
"phone_code_hash": send_code_res.phone_code_hash,
|
| 825 |
+
"api_id": api_id,
|
| 826 |
"api_hash": api_hash
|
| 827 |
}
|
| 828 |
return {"phone_code_hash": send_code_res.phone_code_hash}
|
|
|
|
| 946 |
try:
|
| 947 |
# Fetch all dialogs across main list and all folders (archived=True was restricting to folder 1 only)
|
| 948 |
async with async_timeout(120):
|
| 949 |
+
async for dialog in client.iter_dialogs(ignore_migrated=False):
|
| 950 |
# Robust check for any container that isn't a single user
|
| 951 |
if dialog.is_group or dialog.is_channel:
|
| 952 |
did = str(dialog.id)
|
|
|
|
| 1357 |
# RUN ON ALL VARIATIONS: restores 800+ volume
|
| 1358 |
if True:
|
| 1359 |
from telethon.tl.functions.messages import SearchGlobalRequest # type: ignore
|
| 1360 |
+
from telethon.tl.types import InputMessagesFilterEmpty, InputPeerEmpty # type: ignore
|
| 1361 |
try:
|
| 1362 |
# Bump limit to 100 for maximum discovery volume
|
| 1363 |
msg_results = await client(SearchGlobalRequest(
|
| 1364 |
q=q,
|
| 1365 |
filter=InputMessagesFilterEmpty(),
|
| 1366 |
+
min_date=None,
|
| 1367 |
+
max_date=None,
|
| 1368 |
+
offset_rate=0,
|
| 1369 |
+
offset_peer=InputPeerEmpty(),
|
| 1370 |
+
offset_id=0,
|
| 1371 |
limit=100
|
| 1372 |
))
|
| 1373 |
# ZERO-COST REFINEMENT: Cross-reference chats ALREADY in the response.
|
|
|
|
| 1826 |
for g_id in req.group_ids:
|
| 1827 |
try:
|
| 1828 |
await asyncio.sleep(random.randint(10, 15))
|
| 1829 |
+
resolved_id = int(g_id) if g_id.lstrip('-').isdigit() else g_id
|
| 1830 |
entity = await client.get_entity(resolved_id)
|
| 1831 |
+
await client(JoinChannelRequest(cast(Any, entity)))
|
| 1832 |
results['joined'] += 1
|
| 1833 |
except Exception as e:
|
| 1834 |
results['failed'] += 1
|
|
|
|
| 1921 |
|
| 1922 |
yield f"data: {_json.dumps({'type': 'progress', 'idx': idx, 'total': total, 'msg': f'Joining {group_label}...'})}\n\n"
|
| 1923 |
# Use pool-based client
|
| 1924 |
+
await smart_join(client, g_id, user_id, phone_number)
|
| 1925 |
|
| 1926 |
joined = joined + 1
|
| 1927 |
yield f"data: {_json.dumps({'type': 'joined', 'name': group_label, 'idx': idx, 'total': total, 'joined': joined, 'failed': failed})}\n\n"
|
|
|
|
| 1987 |
|
| 1988 |
members = []
|
| 1989 |
try:
|
| 1990 |
+
async for user in client.iter_participants(cast(Any, entity), limit=500):
|
| 1991 |
if user.bot: continue
|
| 1992 |
members.append({
|
| 1993 |
"id": str(user.id),
|
|
|
|
| 2635 |
|
| 2636 |
# Resolve entity
|
| 2637 |
target = req.group_id
|
| 2638 |
+
if target.lstrip('-').isdigit(): target = int(target)
|
| 2639 |
entity = await client.get_entity(target)
|
| 2640 |
|
| 2641 |
# Get participants
|
| 2642 |
+
users = await client.get_participants(cast(Any, entity), limit=1000)
|
| 2643 |
|
| 2644 |
participants = []
|
| 2645 |
for u in users:
|
|
|
|
| 2653 |
|
| 2654 |
# Save to Leads table
|
| 2655 |
conn = get_db_connection()
|
| 2656 |
+
entity_id = getattr(entity, 'id', target)
|
| 2657 |
for p in participants:
|
| 2658 |
conn.execute(
|
| 2659 |
"INSERT OR IGNORE INTO leads (user_id, group_id, username, first_name, last_name) VALUES (?, ?, ?, ?, ?)",
|
| 2660 |
+
(user_id, str(entity_id), p['username'], p['first_name'], p['last_name'])
|
| 2661 |
)
|
| 2662 |
if hasattr(conn, "commit"): conn.commit()
|
| 2663 |
if hasattr(conn, "close"): conn.close()
|
|
|
|
| 2964 |
members = []
|
| 2965 |
try:
|
| 2966 |
entity = await client.get_entity(group_id)
|
| 2967 |
+
async for user in client.iter_participants(cast(Any, entity), limit=100):
|
| 2968 |
members.append({
|
| 2969 |
"id": user.id,
|
| 2970 |
"first_name": user.first_name,
|
|
|
|
| 2977 |
conn = get_db_connection()
|
| 2978 |
conn.execute(
|
| 2979 |
"INSERT INTO leads (user_id, group_id, username, first_name, last_name) VALUES (?, ?, ?, ?, ?)",
|
| 2980 |
+
(user_id, group_id, user.username, user.first_name, user.last_name)
|
| 2981 |
)
|
| 2982 |
if hasattr(conn, "commit"): conn.commit()
|
| 2983 |
if hasattr(conn, "close"): conn.close()
|
|
|
|
| 3423 |
exclude_list = list(set([str(e).strip() for e in (exclude_list if isinstance(exclude_list, list) else [])]))
|
| 3424 |
|
| 3425 |
def recursive_spin(text: str) -> str:
|
| 3426 |
+
text_obj = text
|
| 3427 |
while '{' in text_obj and '}' in text_obj:
|
| 3428 |
# Simple regex for leaf-level spintax: {a|b|c}
|
| 3429 |
text_obj = re.sub(r'\{([^{}]*)\}', lambda m: random.choice(m.group(1).split('|')), text_obj)
|
| 3430 |
+
return text_obj
|
| 3431 |
|
| 3432 |
try:
|
| 3433 |
processed_list = ast.literal_eval(processed_groups_str) if processed_groups_str else []
|
|
|
|
| 3498 |
failed_list.append({"id": group, "name": group, "reason": "Not a Member"})
|
| 3499 |
continue
|
| 3500 |
|
| 3501 |
+
res_title = getattr(entity, 'title', group)
|
| 3502 |
log_debug(f"BKG_TASK[{task_id}]: Target resolved -> {res_title}")
|
| 3503 |
except Exception as e:
|
| 3504 |
log_debug(f"BKG_TASK[{task_id}]: Mapping Error for {group}: {str(e)}")
|
|
|
|
| 3536 |
# SPAM PREVENTION: Check last message
|
| 3537 |
try:
|
| 3538 |
last_msgs = await client.get_messages(entity, limit=1)
|
| 3539 |
+
first_msg = last_msgs[0] if isinstance(last_msgs, list) else last_msgs
|
| 3540 |
+
if first_msg and getattr(first_msg, 'sender_id', None) == me_id:
|
| 3541 |
log_debug(f"BKG_TASK[{task_id}]: Skipping {target_id} - Last message was from this account.")
|
| 3542 |
continue
|
| 3543 |
except Exception as e:
|