import csv import time import configparser import os,sys from telethon.tl.functions.users import GetFullUserRequest from telethon.tl.functions.messages import GetDialogsRequest from telethon.tl.types import InputPeerEmpty from telethon.sync import TelegramClient # Color constants for printing re = "\033[1;31m" gr = "\033[1;32m" cy = "\033[1;36m" def banner(): print(f""" {re}╔╦╗{cy}┌─┐┬ ┌─┐{re}╔═╗ ╔═╗{cy}┌─┐┬─┐┌─┐┌─┐┌─┐┬─┐ {re} ║ {cy}├┤ │ ├┤ {re}║ ╦ ╚═╗{cy}│ ├┬┘├─┤├─┘├┤ ├┬┘ {re} ╩ {cy}└─┘┴─┘└─┘{re}╚═╝ ╚═╝{cy}└─┘┴└─┴ ┴┴ └─┘┴└─ version : 3.1 youtube.com/channel/UCnknCgg_3pVXS27ThLpw3xQ """) # Read in credentials from config file try: api_id = "id dlna" api_hash = "hash" phone = "+91no" # Create a TelegramClient object client = TelegramClient(phone, api_id, api_hash) except KeyError: os.system("clear") banner() print(re + "[!] run python3 setup.py first !!") sys.exit(1) # Connect to the Telegram API client.connect() # If the user is not authorized, request a code and sign in if not client.is_user_authorized(): client.send_code_request(phone) os.system("clear") banner() client.sign_in(phone, input(gr + "[+] Enter the code: " + re)) # Clear the terminal and display the banner os.system("clear") banner() # Initialize variables for chat list and chunk size chats = [] last_date = None chunk_size = 200 groups = [] # Request a list of the user's chats result = client( GetDialogsRequest( offset_date=last_date, offset_id=0, offset_peer=InputPeerEmpty(), limit=chunk_size, hash=0, ) ) # Add the chats to the list chats.extend(result.chats) # Filter the list of # chats to only include group chats for chat in chats: try: if chat.megagroup: groups.append(chat) except: continue # Prompt the user to select a group print(gr + "[+] Choose a group to scrape members: " + re) i = 0 for g in groups: print(gr + "[" + cy + str(i) + gr + "]" + cy + " - " + g.title) i += 1 print("") g_index = input(gr + "[+] Enter a number: " + re) target_group = groups[int(g_index)] # Retrieve the list of group members print(gr + "[+] Fetching members...") time.sleep(1) all_participants = client.get_participants(target_group, aggressive=True) # Save the list of group members to a CSV file print(gr + "[+] Saving in file...") time.sleep(1) try: with open("members.csv", "w", encoding="UTF-8") as f: writer = csv.writer(f, delimiter=",", lineterminator="\n") writer.writerow( [ "username", "user id", "access hash", "name", "group", "group id", "data center", ] ) for user in all_participants: dmsi = client(GetFullUserRequest(user.id)) try: data_center = user.photo.dc_id except: data_center = "Rand" if data_center == 5: continue else: if user.username: username = user.username else: username = "" if user.first_name: first_name = user.first_name else: first_name = "" if user.last_name: last_name = user.last_name else: last_name = "" name = (first_name + " " + last_name).strip() writer.writerow( [ username, user.id, user.access_hash, name, target_group.title, target_group.id, data_center, ] ) except Exception as e: print(gr + "[-] Error :" + str(e)) # Display a message indicating that the process is complete print(gr + "[+] Done!")