userbot.py 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125
  1. #! /usr/bin/env python3
  2. # -*- coding: utf-8 -*-
  3. from pyrogram import Client, filters
  4. from pyrogram.errors import FloodWait
  5. from pyrogram.errors import SlowmodeWait
  6. from datetime import datetime
  7. from time import sleep
  8. from progress.bar import IncrementalBar
  9. from os import remove
  10. import json
  11. app = Client("my_account", sleep_threshold=0)
  12. chat_id_list = []
  13. def menu():
  14. chats = input(
  15. """
  16. Введіть публічне посилання на чат або його id
  17. (Наприклад: -1223447675434 або membersofxaknet
  18. або -1223447675434, -17654345438, hz_group)
  19. : """
  20. ).replace(" ", "").split(",")
  21. for chat in chats:
  22. try:
  23. chat_id_list.append(int(chat))
  24. except:
  25. chat_id_list.append(chat)
  26. menu()
  27. def getmember(member):
  28. data = {
  29. "tg_id": "",
  30. "first_name": "",
  31. "last_name": "",
  32. "username": "",
  33. "phone_number": "",
  34. "status": "",
  35. "title": "",
  36. "joined_date": ""
  37. }
  38. if member.user.id:
  39. data["tg_id"] = member.user.id
  40. if member.user.first_name:
  41. data["first_name"] = member.user.first_name
  42. if member.user.last_name:
  43. data["last_name"] = member.user.last_name
  44. if member.user.username:
  45. data["username"] = member.user.username
  46. if member.user.phone_number:
  47. data["phone_number"] = member.user.phone_number
  48. if member.status:
  49. status = member.status
  50. if member.status == "administrator":
  51. status = "admin"
  52. data["status"] = status
  53. if member.title:
  54. data["title"] = member.title
  55. if member.joined_date:
  56. utc_time = datetime.utcfromtimestamp(member.joined_date)
  57. utc_time = utc_time.strftime("%Y-%m-%d %H:%M:%S.%f+00:00 (UTC)")
  58. data["joined_date"] = utc_time
  59. return data
  60. def allwork(chat_id_list):
  61. for chat_id in chat_id_list:
  62. chat_name = app.get_chat(chat_id).title
  63. print(f'Отримання даних учасників чату {chat_name}')
  64. count = app.get_chat_members_count(chat_id)
  65. class MyBar(IncrementalBar):
  66. if count < 70:
  67. width = count
  68. else:
  69. width = 70
  70. suffix = '%(index)d/%(max)d'
  71. bar_prefix = '▐'
  72. bar_suffix = '▌'
  73. empty_fill = '-'
  74. phases = ("░","▒","█")
  75. bar = MyBar("", max = count)
  76. i = 1
  77. members_list = []
  78. admin_list = []
  79. creator = {}
  80. for member in app.iter_chat_members(chat_id):
  81. data = getmember(member)
  82. members_list.append(data)
  83. if data["status"] == 'admin':
  84. admin_list.append(data)
  85. if data["status"] == 'creator':
  86. creator = data
  87. bar.next()
  88. i += 1
  89. if i % 3000 == 0:
  90. sleep(10)
  91. print("\nFloodWait 10 sec")
  92. bar.finish()
  93. if not admin_list:
  94. print("Адмінів не знайдно, або у них увімкнена анонімність")
  95. else:
  96. print(f"Записую адмінів у файл '{chat_id}-admin.json'")
  97. with open(f"{chat_id}_admin.json", 'w', encoding="utf8") as outfile:
  98. json.dump(admin_list, outfile, indent=4, ensure_ascii=False)
  99. if not creator:
  100. print("Власника не знайдно, або у нього увімкнена анонімність")
  101. else:
  102. print(f"Записую власника у файл '{chat_id}-creator.json'")
  103. with open(f"{chat_id}-creator.json", 'w', encoding="utf8") as outfile:
  104. json.dump(creator, outfile, indent=4, ensure_ascii=False)
  105. print(f"Записую всіх у файл '{chat_id}-all.json'")
  106. with open(f"{chat_id}-all.json", 'w', encoding="utf8") as outfile:
  107. json.dump(members_list, outfile, indent=4, ensure_ascii=False)
  108. print("Завершено")
  109. app.stop()
  110. exit(0)
  111. with app:
  112. allwork(chat_id_list)
  113. app.run()