Загрузка данных


import asyncio
import aiohttp

INPUT_FILE = "usernames.txt"
EXISTING_FILE = "existing_groups.txt"
NON_EXISTING_FILE = "non_existing.txt"

HEADERS = {
    "User-Agent": (
        "Mozilla/5.0 (Windows NT 10.0; Win64; x64) "
        "AppleWebKit/537.36 (KHTML, like Gecko) "
        "Chrome/124.0.0.0 Safari/537.36"
    )
}

async def write_file(filename, text):
    with open(filename, "a", encoding="utf-8") as f:
        f.write(text + "\n")

async def check_group(session, username, idx, total):
    url = f"https://t.me/{username}"
    try:
        async with session.get(url, headers=HEADERS, timeout=10) as response:
            html = await response.text()

            # Признаки мертвой/несуществующей/забаненной страницы
            is_dead = (
                "If you have <strong>Telegram</strong>, you can contact" in html
                or "tgme_page_extra" not in html
                or "View in Telegram" not in html
            )

            # Проверка, что это группа/чат, а не просто пустая заглушка
            is_group = (
                "members" in html
                or "subscribers" in html
                or "online" in html
                or "View in Telegram" in html
            )

            if is_group and not is_dead:
                await write_file(EXISTING_FILE, f"@{username}")
                print(f"[{idx}/{total}] Существует: @{username}")
            else:
                await write_file(NON_EXISTING_FILE, f"@{username}")
                print(f"[{idx}/{total}] Не существует / бан: @{username}")

    except Exception as e:
        await write_file(NON_EXISTING_FILE, f"@{username}")
        print(f"[{idx}/{total}] Ошибка запроса: @{username}")

async def main():
    open(EXISTING_FILE, "w", encoding="utf-8").close()
    open(NON_EXISTING_FILE, "w", encoding="utf-8").close()

    try:
        with open(INPUT_FILE, "r", encoding="utf-8") as f:
            usernames = [line.strip().lstrip("@") for line in f if line.strip()]
    except FileNotFoundError:
        print(f"Ошибка: Не найден {INPUT_FILE}")
        return

    total = len(usernames)
    print(f"Загружено {total} ссылок. Запуск парсинга без авторизации...\n")

    # Проверка через HTTP-сессию
    connector = aiohttp.TCPConnector(limit=5)
    async with aiohttp.ClientSession(connector=connector) as session:
        for idx, username in enumerate(usernames, 1):
            await check_group(session, username, idx, total)
            # Небольшая пауза, чтобы веб-сервер не выдал Too Many Requests
            await asyncio.sleep(0.4)

    print("\nПроверка завершена без использования сессий Telegram!")

if __name__ == "__main__":
    asyncio.run(main())