import asyncio from pydurian.db import CountryConfig, User from pydurian.helpers import get_locker from pydurian.plugins.phone_number import send_phone_number from pydurian.sms import ApiError, EmptyPhoneError, SmsService from pyrogram.client import Client from pyrogram.errors import ChannelPrivate async def get_numbers(client: Client) -> None: users = await User.find_all().to_list() for user in users: if not user.config: continue sms = SmsService(user.config.sms.user, "0257", user.config.sms.api_key) for country in user.config.countries: async def try_fetch_phone_number( user: User, sms: SmsService, country: CountryConfig ) -> None: chat_id = user.user_id if user.config.channel is None else user.config.channel try: phone_number = await sms.get_number(country.code) except EmptyPhoneError: return None except ApiError: return await try_fetch_phone_number(user, sms, country) try: await send_phone_number(client, chat_id, phone_number) except ChannelPrivate: await send_phone_number(client, user.user_id, phone_number) async with get_locker(user.user_id): await user.sync() country.numbers_count -= 1 if country.numbers_count <= 0: user.config.countries.pop(0) await user.save() return None await try_fetch_phone_number(user, sms, country) await asyncio.sleep(0.1)