-
-
Save dangsonbk/78757bc14baa32f5cfa224c087133a28 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import sys | |
from getpass import getpass | |
from time import sleep | |
try: | |
from telethon import TelegramClient | |
except ImportError as e: | |
print("telethon is not available in your system. Installing ...") | |
import pip | |
pip.main(['install', "telethon", "--user"]) | |
input("Please restart the script to apply new package") | |
exit() | |
from telethon import TelegramClient | |
from telethon.errors import SessionPasswordNeededError | |
# from telethon.errors.rpc_error_list import UsernameNotOccupiedError | |
from telethon.errors.rpcbaseerrors import FloodError | |
# from telethon.errors.rpcbaseerrors import ChatAdminRequiredError | |
from telethon.tl.functions.channels import GetParticipantsRequest | |
from telethon.tl.functions.contacts import ResolveUsernameRequest | |
from telethon.tl.types import ChannelParticipantsSearch, InputChannel | |
# First you need create app on https://my.telegram.org | |
api_id = 518161 | |
api_hash = 'b5ef3577c856cb8c22d3f5d4eab75537' | |
phone = '+84389908656' | |
limit = 100 | |
def dump_users(chat_info, client): | |
counter = 0 | |
offset = 0 | |
chat_object = InputChannel(chat_info['chat_id'], chat_info['access_hash']) | |
all_participants = [] | |
print('Process...') | |
while True: | |
try: | |
participants = client.invoke( | |
GetParticipantsRequest( chat_object, | |
ChannelParticipantsSearch(''), | |
offset, | |
limit, | |
hash=0 | |
) | |
) | |
if not participants.users: | |
break | |
all_participants.extend(['{}, {}'.format(x.id, x.username) | |
for x in participants.users]) | |
users_count = len(participants.users) | |
offset += users_count | |
counter += users_count | |
print('{} users collected'.format(counter)) | |
except e: | |
print(e) | |
break | |
sleep(2) | |
with open('users.txt', 'w') as file: | |
file.write('\n'.join(map(str, all_participants))) | |
def main(): | |
channel_name = input('Input a channel name, without "@": ') | |
client = TelegramClient('current-session', api_id, api_hash) | |
print('Connecting...') | |
client.connect() | |
if not client.is_user_authorized(): | |
try: | |
client.send_code_request(phone) | |
print('Sending a code...') | |
client.sign_in(phone, code=input('Enter code: ')) | |
print('Authorized Successfully!') | |
except FloodError as FloodError: | |
print('Flood wait: {}.'.format(FloodError)) | |
sys.exit() | |
except SessionPasswordNeededError: | |
client.sign_in(password=getpass('Enter password: ')) | |
print('Authorized Successfully!') | |
try: | |
chat = client(ResolveUsernameRequest(channel_name)) | |
except e: | |
print('Chat/channel not found!') | |
sys.exit() | |
chat_info = { | |
'chat_id': chat.peer.channel_id, | |
'access_hash': chat.chats[0].access_hash | |
} | |
dump_users(chat_info, client) | |
print('Done!') | |
if __name__ == '__main__': | |
main() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment