P
P
Python Newbie2022-01-08 12:58:48
Python
Python Newbie, 2022-01-08 12:58:48

How to speed up the time of collecting data about group members in pyrogram?

Hello, there is a code that gets the group members and, with the help of another request in telegram, pulls their bio. The code is written synchronously.

How can this process be accelerated?

The code

import os


from pyrogram import Client, types
from pyrogram.raw.types import InputUser
from pyrogram.raw.functions.users.get_full_user import GetFullUser

from dotenv import load_dotenv

import pandas as pd


load_dotenv('file.env')

e = input('Введите ссылку по типу https://t.me/git_cool: ').replace(
    'https://t.me/', '').replace('t.me/', '')

chat = e
app = Client("my_account", os.getenv('api_id'), os.getenv('api_hash'))


def get_about(m):
    u = app.send(GetFullUser(id=InputUser(
        user_id=m.user_id, access_hash=m.access_hash)), sleep_threshold=50.00)
    about = u.full_user.about if u.full_user.about is not None else 'У пользователя нет био :('
    return about


with app:
    print('Начинаем парсинг...')
    app.get_chat(chat)
    mems = []

    for mem in app.iter_chat_members(chat):
        member: types.User = mem.user
        username = member.username if member.username is not None else 'У пользователя нет никнейма'
        phone = member.phone_number if member.phone_number is not None else 'Пользователь скрыл свой номер телефона'
        name = member.first_name if member.first_name is not None else 'Пользователь не указал своё имя'
        last_name = member.last_name if member.last_name is not None else 'Пользователь не указал свою фамилию'
        m = app.resolve_peer(member.id)

        about = get_about(m)

        print(f'{username} [{name} {last_name}] {phone} - {about}')
        mems.append({'user_id': member.id, 'username': username, 'phone': phone,
                    'name': name, 'last_name': last_name, 'about': about})

print('Парсинг закончен')
print('Идёт загрузка в excel....Подождите немного')

print(mems)

df = pd.DataFrame(mems)
df.to_excel('members.xlsx', index=False)
print('Excel документ готов!')

Answer the question

In order to leave comments, you need to log in

1 answer(s)
P
Pavel Shvedov, 2022-01-08
@mmmaaak

Rewrite to asynchrony if the library allows

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question