Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import sys
- from getpass import getpass
- from time import sleep
- from telethon import TelegramClient
- from telethon.errors import SessionPasswordNeededError
- from telethon.errors.rpcerrorlist import UsernameNotOccupiedError
- from telethon.errors.rpcerrorlist import FloodWaitError
- from telethon.tl.functions.channels import GetParticipantsRequest
- from telethon.tl.functions.contacts import ResolveUsernameRequest
- from telethon.tl.types import ChannelParticipantsSearch, InputChannel
- import asyncio
- from aioconsole import ainput
- from config import app_api_id as api_id, app_api_hash as api_hash, app_phone as phone
- #api_id = 149999999
- #api_hash = '69cagfbda82fe3d17b3becxa20817d'
- #phone = '+79777777777'
- limit = 100
- async def get_chat_info(username, client):
- try:
- chat = await client(ResolveUsernameRequest(username))
- except UsernameNotOccupiedError:
- print('Chat/channel not found!')
- sys.exit()
- print (chat)
- result = {
- 'chat_id': chat.chats[0].id, #-12013214989,
- 'access_hash': chat.chats[0].access_hash
- }
- return result
- async def dump_users(chat, client):
- counter = 0
- offset = 0
- chat_object = InputChannel(chat['chat_id'], chat['access_hash'])
- all_participants = []
- print('Process...')
- while True:
- participants = await client(GetParticipantsRequest(
- chat_object, ChannelParticipantsSearch(''), offset, limit, hash=chat['access_hash']
- ))
- 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))
- sleep(2)
- with open('users.txt', 'w') as file:
- file.write('\n'.join(map(str, all_participants)))
- async def main():
- channel_name = await ainput('Input a channel name, without "@": ')
- client = TelegramClient('client-session', api_id, api_hash)
- print('Connecting...')
- await client.connect()
- if not await client.is_user_authorized():
- try:
- await client.send_code_request(phone)
- print('Sending a code...')
- await client.sign_in(phone, code=await ainput('Enter code: '))
- print('Successfully!')
- except FloodWaitError as FloodError:
- print('Flood wait: {}.'.format(FloodError))
- return
- except SessionPasswordNeededError:
- await client.sign_in(password=getpass('Enter password: '))
- print('Successfully!')
- await dump_users(await get_chat_info(channel_name, client), client)
- print('Done!')
- if __name__ == '__main__':
- asyncio.run(main())
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement