-
Notifications
You must be signed in to change notification settings - Fork 3
/
get_members.py
98 lines (67 loc) · 2.79 KB
/
get_members.py
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
import csv
from config import*
from telethon import TelegramClient, sync
client = TelegramClient(phone, api_id, api_hash)
# get all the channels that I can access
channels = {d.entity.username: d.entity
for d in client.get_dialogs()
if d.is_channel}
def save_members(username,id, access_hash,name):
with open('tbot.csv', mode='a+') as csv_file:
fieldnames = ['username','id','access_hash','name']
writer = csv.DictWriter(csv_file, fieldnames=fieldnames)
writer.writeheader()
writer.writerow({'username': username,'id':id,'access_hash': access_hash,'name': name})
for i,channel_name in enumerate(channels.keys()):
try:print(f"{i} ::: {channel_name}")
except:pass
channel_id = int(input("Please select a number :: "))
channel_name = list(channels.keys())[channel_id]
channel = channels[channel_name]
print(f"\n\nYou have selected:: {channel_name}")
print("\n\nGetting the members of this channel\n")
try:
# get all the users and print them
for user in client.get_participants(channel):
name = f"{user.first_name} {user.last_name}"
save_members(user.username,user.id,user.access_hash,name)import csv
from config import*
from telethon import TelegramClient, sync
client = TelegramClient(phone, api_id, api_hash)
client.connect()
if not client.is_user_authorized():
client.send_code_request(phone)
client.sign_in(phone, input('Enter the code: '))
def get_channels():
# get all the channels that I can access
channels = {d.entity.username: d.entity
for d in client.get_dialogs()
if d.is_channel}
for i,channel_name in enumerate(channels.keys()):
try:print(f"{i} ::: {channel_name}")
except:pass
channel_id = int(input("Please select a number :: "))
channel_name = list(channels.keys())[channel_id]
channel = channels[channel_name]
print(f"\n\nYou have selected:: {channel_name}")
print("\n\nGetting the members of this channel\n")
try:
with open('tbot.csv', mode='w+') as csv_file:
fieldnames = ['username','id','access_hash','name']
writer = csv.DictWriter(csv_file, fieldnames=fieldnames)
writer.writeheader()
# get all the users and print them
for user in client.get_participants(channel):
name = f"{user.first_name} {user.last_name}"
writer.writerow({'username': user.username,'id':user.id,'access_hash':user.access_hash,'name':name})
print(user.username,user.id,user.access_hash,name)
except Exception as e:
print(e)
get_channels()
try:
get_channels()
except Exception as e:
print(e)
print(user.username,user.id,user.access_hash,name)
except Exception as e:
print(e)