Advertisement
Guest User

Untitled

a guest
Jan 9th, 2022
88
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 2.81 KB | None | 0 0
  1. import sys
  2. from getpass import getpass
  3. from time import sleep
  4.  
  5. from telethon import TelegramClient
  6. from telethon.errors import SessionPasswordNeededError
  7. from telethon.errors.rpcerrorlist import UsernameNotOccupiedError
  8. from telethon.errors.rpcerrorlist import FloodWaitError
  9. from telethon.tl.functions.channels import GetParticipantsRequest
  10. from telethon.tl.functions.contacts import ResolveUsernameRequest
  11. from telethon.tl.types import ChannelParticipantsSearch, InputChannel
  12.  
  13. import asyncio
  14. from aioconsole import ainput
  15.  
  16. from config import app_api_id as api_id, app_api_hash as api_hash, app_phone as phone
  17. #api_id = 149999999
  18. #api_hash = '69cagfbda82fe3d17b3becxa20817d'
  19. #phone = '+79777777777'
  20. limit = 100
  21.  
  22.  
  23. async def get_chat_info(username, client):
  24.     try:
  25.         chat = await client(ResolveUsernameRequest(username))
  26.     except UsernameNotOccupiedError:
  27.         print('Chat/channel not found!')
  28.         sys.exit()
  29.     print (chat)
  30.     result = {
  31.         'chat_id': chat.chats[0].id, #-12013214989,
  32.         'access_hash': chat.chats[0].access_hash
  33.     }
  34.     return result
  35.  
  36.  
  37. async def dump_users(chat, client):
  38.     counter = 0
  39.     offset = 0
  40.     chat_object = InputChannel(chat['chat_id'], chat['access_hash'])
  41.     all_participants = []
  42.     print('Process...')
  43.     while True:
  44.         participants = await client(GetParticipantsRequest(
  45.                     chat_object, ChannelParticipantsSearch(''), offset, limit, hash=chat['access_hash']
  46.                 ))
  47.         if not participants.users:
  48.             break
  49.         all_participants.extend(['{} {}'.format(x.id, x.username)
  50.                            for x in participants.users])
  51.         users_count = len(participants.users)
  52.         offset += users_count
  53.         counter += users_count
  54.         print('{} users collected'.format(counter))
  55.         sleep(2)
  56.     with open('users.txt', 'w') as file:
  57.         file.write('\n'.join(map(str, all_participants)))
  58.  
  59.  
  60. async def main():
  61.     channel_name = await ainput('Input a channel name, without "@": ')
  62.     client = TelegramClient('client-session', api_id, api_hash)
  63.     print('Connecting...')
  64.     await client.connect()
  65.     if not await client.is_user_authorized():
  66.         try:
  67.             await client.send_code_request(phone)
  68.             print('Sending a code...')
  69.             await client.sign_in(phone, code=await ainput('Enter code: '))
  70.             print('Successfully!')
  71.         except FloodWaitError as FloodError:
  72.             print('Flood wait: {}.'.format(FloodError))
  73.             return
  74.         except SessionPasswordNeededError:
  75.             await client.sign_in(password=getpass('Enter password: '))
  76.             print('Successfully!')
  77.     await dump_users(await get_chat_info(channel_name, client), client)
  78.     print('Done!')
  79.  
  80. if __name__ == '__main__':
  81.     asyncio.run(main())
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement