Guest User

Untitled

a guest
Mar 2nd, 2018
180
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.90 KB | None | 0 0
  1. from telethon import TelegramClient
  2. from telethon.errors.rpc_errors_401 import SessionPasswordNeededError
  3.  
  4. # (1) Use your own values here
  5. api_id = 17349
  6. api_hash = '344583e45741c457fe1862106095a5eb'
  7.  
  8. phone = 'YOUR_NUMBER_HERE'
  9. username = 'username'
  10.  
  11. # (2) Create the client and connect
  12. client = TelegramClient(username, api_id, api_hash)
  13. client.connect()
  14.  
  15. # Ensure you're authorized
  16. if not client.is_user_authorized():
  17. client.send_code_request(phone)
  18. try:
  19. client.sign_in(phone, input('Enter the code: '))
  20. except SessionPasswordNeededError:
  21. client.sign_in(password=input('Password: '))
  22.  
  23. me = client.get_me()
  24. print(me)
  25.  
  26. from telethon.tl.functions.messages import GetDialogsRequest
  27. from telethon.tl.types import InputPeerEmpty
  28.  
  29. get_dialogs = GetDialogsRequest(
  30. offset_date=None,
  31. offset_id=0,
  32. offset_peer=InputPeerEmpty(),
  33. limit=30,
  34. )
  35.  
  36. dialogs = client(get_dialogs)
  37. print(dialogs)
  38.  
  39. from telethon.tl.functions.messages import GetHistoryRequest
  40. from telethon.tl.types import PeerUser, PeerChat, PeerChannel, InputPeerUser, InputPeerChat, InputPeerChannel
  41. from telethon.tl.types.messages import Messages
  42.  
  43. counts = {}
  44.  
  45. # create dictionary of ids to users and chats
  46. users = {}
  47. chats = {}
  48.  
  49. for u in dialogs.users:
  50. users[u.id] = u
  51.  
  52. for c in dialogs.chats:
  53. chats[c.id] = c
  54.  
  55. for d in dialogs.dialogs:
  56. peer = d.peer
  57. if isinstance(peer, PeerChannel):
  58. id = peer.channel_id
  59. channel = chats[id]
  60. access_hash = channel.access_hash
  61. name = channel.title
  62.  
  63. input_peer = InputPeerChannel(id, access_hash)
  64. elif isinstance(peer, PeerChat):
  65. id = peer.chat_id
  66. group = chats[id]
  67. name = group.title
  68.  
  69. input_peer = InputPeerChat(id)
  70. elif isinstance(peer, PeerUser):
  71. id = peer.user_id
  72. user = users[id]
  73. access_hash = user.access_hash
  74. name = user.first_name
  75.  
  76. input_peer = InputPeerUser(id, access_hash)
  77. else:
  78. continue
  79.  
  80. get_history = GetHistoryRequest(
  81. peer=input_peer,
  82. offset_id=0,
  83. offset_date=None,
  84. add_offset=0,
  85. limit=1,
  86. max_id=0,
  87. min_id=0,
  88. )
  89.  
  90. history = client(get_history)
  91. if isinstance(history, Messages):
  92. count = len(history.messages)
  93. else:
  94. count = history.count
  95.  
  96. counts[name] = count
  97.  
  98. print(counts)
  99. sorted_counts = sorted(counts.items(), key=lambda x: x[1], reverse=True)
  100. for name, count in sorted_counts:
  101. print('{}: {}'.format(name, count))
  102.  
  103. # Using helper methods
  104.  
  105. # from telethon.tl.types import User
  106. #
  107. # _, entities = client.get_dialogs(limit=30)
  108. #
  109. # counts = []
  110. # for e in entities:
  111. # if isinstance(e, User):
  112. # name = e.first_name
  113. # else:
  114. # name = e.title
  115. #
  116. # count, _, _ = client.get_message_history(e, limit=1)
  117. # counts.append((name, count))
  118. #
  119. # message_counts.sort(key=lambda x: x[1], reverse=True)
  120. # for name, count in counts:
  121. # print('{}: {}'.format(name, count))
Add Comment
Please, Sign In to add comment