|
| 1 | +# Pyrogram - Telegram MTProto API Client Library for Python |
| 2 | +# Copyright (C) 2017-2018 Dan Tès <https://github.com/delivrance> |
| 3 | +# |
| 4 | +# This file is part of Pyrogram. |
| 5 | +# |
| 6 | +# Pyrogram is free software: you can redistribute it and/or modify |
| 7 | +# it under the terms of the GNU Lesser General Public License as published |
| 8 | +# by the Free Software Foundation, either version 3 of the License, or |
| 9 | +# (at your option) any later version. |
| 10 | +# |
| 11 | +# Pyrogram is distributed in the hope that it will be useful, |
| 12 | +# but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 13 | +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 14 | +# GNU Lesser General Public License for more details. |
| 15 | +# |
| 16 | +# You should have received a copy of the GNU Lesser General Public License |
| 17 | +# along with Pyrogram. If not, see <http://www.gnu.org/licenses/>. |
| 18 | + |
| 19 | +import time |
| 20 | +from string import ascii_lowercase |
| 21 | + |
| 22 | +from pyrogram import Client |
| 23 | +from pyrogram.api.errors import FloodWait |
| 24 | + |
| 25 | +"""This is an improved version of get_chat_members.py |
| 26 | +
|
| 27 | +Since Telegram will return at most 10.000 members for a single query, this script |
| 28 | +repeats the search using numbers ("0" to "9") and all the available ascii letters ("a" to "z"). |
| 29 | +
|
| 30 | +This can be further improved by also searching for non-ascii characters (e.g.: Japanese script), |
| 31 | +as some user names may not contain ascii letters at all. |
| 32 | +""" |
| 33 | + |
| 34 | +app = Client("my_account") |
| 35 | + |
| 36 | +target = "pyrogramchat" # Target channel/supergroup |
| 37 | +members = {} # List that will contain all the members of the target chat |
| 38 | +limit = 200 # Amount of users to retrieve for each API call (max 200) |
| 39 | + |
| 40 | +# "" + "0123456789" + "abcdefghijklmnopqrstuvwxyz" (as list) |
| 41 | +queries = [""] + [str(i) for i in range(10)] + list(ascii_lowercase) |
| 42 | + |
| 43 | +with app: |
| 44 | + for q in queries: |
| 45 | + print('Searching for "{}"'.format(q)) |
| 46 | + offset = 0 # For each query, offset restarts from 0 |
| 47 | + |
| 48 | + while True: |
| 49 | + try: |
| 50 | + chunk = app.get_chat_members(target, offset, query=q) |
| 51 | + except FloodWait as e: # Very large chats could trigger FloodWait |
| 52 | + print("Flood wait: {} seconds".format(e.x)) |
| 53 | + time.sleep(e.x) # When it happens, wait X seconds and try again |
| 54 | + continue |
| 55 | + |
| 56 | + if not chunk.chat_members: |
| 57 | + print('Done searching for "{}"'.format(q)) |
| 58 | + print() |
| 59 | + break # No more members left |
| 60 | + |
| 61 | + members.update({i.user.id: i for i in chunk.chat_members}) |
| 62 | + offset += len(chunk.chat_members) |
| 63 | + |
| 64 | + print("Total members: {}".format(len(members))) |
| 65 | + |
| 66 | + print("Grand total: {}".format(len(members))) |
| 67 | + |
| 68 | +# Now the "members" list contains all the members of the target chat |
0 commit comments