forked from nacbotics5/Telethon_bot
-
Notifications
You must be signed in to change notification settings - Fork 0
/
get_members.py
55 lines (40 loc) · 1.67 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
import csv
from config import*
from telethon import TelegramClient, sync
client = TelegramClient(None, api_id, api_hash)
client.start()
#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(f'{channel_name}.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)
return(f"Users from ({channel_name}) have been copied successfully")
except Exception as e:
print(f"An error occurred because:: {e}")
get_channels()
try:
get_channels()
except Exception as e:
print(e)