|
import csv |
|
import asyncio |
|
from telethon import TelegramClient |
|
from tqdm import tqdm |
|
|
|
|
|
API_ID = 25216912 |
|
API_HASH = "f65f6050fe9b342a4996c59e4283ab5e" |
|
PHONE_NUMBER = "+9677" |
|
SESSION = "session/a" |
|
|
|
CSV_FILENAME = "telegram_groups.csv" |
|
|
|
async def get_group_links(): |
|
async with TelegramClient(SESSION, API_ID, API_HASH) as client: |
|
await client.start(PHONE_NUMBER) |
|
|
|
dialogs = await client.get_dialogs() |
|
groups = [d for d in dialogs if d.is_group or d.is_channel] |
|
|
|
|
|
with open(CSV_FILENAME, mode="a", newline="", encoding="utf-8") as file: |
|
writer = csv.writer(file) |
|
writer.writerow(["phone_number", "group_name", "username", "group_id", "member_count"]) |
|
|
|
|
|
for group in tqdm(groups, desc="Fetching groups", unit="group"): |
|
group_name = group.title |
|
group_id = group.entity.id |
|
|
|
|
|
username = f"https://t.me/{group.entity.username}" if hasattr(group.entity, "username") and group.entity.username else "private_group" |
|
|
|
try: |
|
|
|
entity = await client.get_entity(group_id) |
|
member_count = entity.participants_count if hasattr(entity, "participants_count") else "unknown" |
|
except: |
|
member_count = "unknown" |
|
|
|
|
|
writer.writerow([PHONE_NUMBER, group_name, username, group_id, member_count]) |
|
|
|
print(f"\n✅ Data saved to {CSV_FILENAME}") |
|
|
|
|
|
asyncio.run(get_group_links()) |