pyrogram/examples/get_participants.py

41 lines
1.1 KiB
Python
Raw Normal View History

2018-02-26 14:49:07 +00:00
import time
from pyrogram import Client
from pyrogram.api import functions, types
from pyrogram.api.errors import FloodWait
2018-04-14 16:46:54 +00:00
app = Client("my_account")
app.start()
2018-02-26 14:49:07 +00:00
target = "username" # Target channel/supergroup
users = [] # List that will contain all the users of the target chat
limit = 200 # Amount of users to retrieve for each API call
offset = 0 # Offset starts at 0
while True:
try:
2018-04-14 16:46:54 +00:00
participants = app.send(
2018-02-26 14:49:07 +00:00
functions.channels.GetParticipants(
2018-04-14 16:46:54 +00:00
channel=app.resolve_peer(target),
2018-02-26 14:49:07 +00:00
filter=types.ChannelParticipantsSearch(""), # Filter by empty string (search for all)
offset=offset,
limit=limit,
hash=0
)
)
except FloodWait as e:
# Very large channels will trigger FloodWait.
# When happens, wait X seconds before continuing
time.sleep(e.x)
continue
if not participants.participants:
break # No more participants left
users.extend(participants.users)
offset += limit
2018-04-14 16:46:54 +00:00
app.stop()
2018-02-26 16:01:33 +00:00
2018-02-26 14:49:07 +00:00
# Now the "users" list contains all the members of the target chat