Advertisement
Guest User

Untitled

a guest
Mar 20th, 2018
256
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.18 KB | None | 0 0
  1. from telethon.tl.types import InputChannel, InputPeerChat
  2. from telethon import TelegramClient
  3. from telethon.tl.functions.channels import GetParticipantsRequest, JoinChannelRequest
  4. from telethon.tl.functions.messages import ImportChatInviteRequest
  5. from telethon.tl.types import ChannelParticipantsRecent, ChannelParticipantsSearch, Channel, Chat
  6. from telethon.errors.rpc_error_list import UserAlreadyParticipantError, FloodWaitError, InviteHashInvalidError
  7. from telethon.tl.functions.messages import GetFullChatRequest
  8. from os import path
  9. from time import sleep
  10. import re
  11. import random
  12.  
  13. ###########################################
  14. api_id = 177852
  15. api_hash = '71a9ac62d0f349458d464c09eff832e8'
  16. phone = '+79237927536'
  17. username = 'uname'
  18. links_file = 'links.txt'
  19. result_file = 'result.txt'
  20. SPLITTER = '#'
  21. ###########################################
  22.  
  23. current_path = path.dirname(path.realpath(__file__))
  24.  
  25. join_links = []
  26.  
  27.  
  28. try:
  29. with open(path.join(current_path,'fails.txt'), 'r'):
  30. pass
  31. except Exception:
  32. with open(path.join(current_path,'fails.txt'), 'w'):
  33. pass
  34.  
  35.  
  36. client = TelegramClient(username, api_id, api_hash)
  37.  
  38.  
  39. def auth():
  40. client.connect()
  41. if not client.is_user_authorized():
  42. client.send_code_request(phone)
  43. try:
  44. client.sign_in(phone, input('Enter the code: '))
  45. except:
  46. client.sign_in(password=input('Password: '))
  47.  
  48.  
  49. def get_links(file=links_file):
  50. global join_links
  51. with open(file, 'r') as o:
  52. join_links = o.readlines()
  53.  
  54.  
  55. def get_participants(ch):
  56. all_participants = []
  57.  
  58. if isinstance(ch, Channel):
  59. offset = 0
  60. limit = 100
  61. # print('[getting participants... loop]')
  62. while True:
  63. participants = client(GetParticipantsRequest(
  64. ch, ChannelParticipantsSearch(''), offset, limit, hash=0
  65. ))
  66. if not participants.users:
  67. break
  68. all_participants.extend(participants.users)
  69. offset += len(participants.users)
  70. all_participants = [str(x.id) for x in all_participants]
  71.  
  72.  
  73. elif isinstance(ch, Chat):
  74. a = client(GetFullChatRequest(ch.id))
  75. # for i in a.participants:
  76. # print(i)
  77. print(a)
  78. participants = a.full_chat.participants.participants
  79. # print(participants)
  80. for i in participants:
  81. all_participants.append(i)
  82. all_participants = [str(x.id) for x in all_participants]
  83.  
  84. else:
  85. print(type(ch), 'not supported')
  86. return all_participants
  87.  
  88.  
  89. def work():
  90. if join_links:
  91. a = random.randint(1, 3)
  92. print('[Flood protection; waiting %i seconds]' % a)
  93. sleep(a)
  94. for _, join_link in enumerate(join_links):
  95. join_link = join_link.strip()
  96. join_link, msg, file, caption = join_link.split(SPLITTER)
  97. link_hash = join_link.rsplit('/')[-1]
  98. print('[Processing %s]' % link_hash)
  99. if 'joinchat' not in join_link:
  100. print('[The invite hash is invalid]', link_hash)
  101. continue
  102.  
  103. try:
  104. client(ImportChatInviteRequest(link_hash))
  105. print('[Succefully joined %s]' % link_hash)
  106.  
  107. except FloodWaitError as f:
  108. error = str(f)
  109. try:
  110. t = int(re.findall(r'A wait of (\d+?) seconds is required', error)[0])
  111. except:
  112. pass
  113. else:
  114. print('[Flood protection; waiting %i seconds]' % t)
  115. sleep(t)
  116.  
  117. except InviteHashInvalidError:
  118. print('[The invite hash is invalid]', link_hash)
  119. continue
  120.  
  121. except UserAlreadyParticipantError:
  122. print('[Already joined %s]' % link_hash)
  123.  
  124. except Exception as e:
  125. print(e, link_hash)
  126. continue
  127.  
  128. try:
  129. channel = client.get_entity(join_link)
  130. participants = get_participants(channel)
  131. participants.insert(0, str(channel.id))
  132. res = ','.join(participants)
  133. if msg:
  134. client.send_message(channel, msg)
  135. if file:
  136. client.send_file(channel, file, caption=caption)
  137. except ValueError as v:
  138. with open(path.join(current_path,'fails.txt'), 'a') as fails:
  139. fails.write(join_link.strip()+'\n')
  140. print('[%s failed by flood protection caused often joins; see fails.txt]' % link_hash)
  141. continue
  142. except Exception as e:
  143. print(e, link_hash)
  144. continue
  145.  
  146. with open(path.join(current_path, result_file), 'a') as o:
  147. o.write(res.strip() + '\n')
  148.  
  149. print('%i/%i [Finished %s]\n' % (_ + 1, len(join_links), link_hash))
  150. a = random.randint(1, 3)
  151. print('[Flood protection; waiting %i seconds]' % a)
  152. sleep(a)
  153. else:
  154. exit('[No links provided]')
  155.  
  156.  
  157. if __name__ == '__main__':
  158. auth()
  159. get_links()
  160. work()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement