Exemplo n.º 1
0
async def _get_channel_users(client: MautrixTelegramClient,
                             entity: InputChannel,
                             limit: int) -> list[TypeUser]:
    if 0 < limit <= 200:
        response = await client(
            GetParticipantsRequest(entity,
                                   ChannelParticipantsRecent(),
                                   offset=0,
                                   limit=limit,
                                   hash=0))
        return list(_filter_participants(response.users,
                                         response.participants))
    elif limit > 200 or limit == -1:
        users: list[TypeUser] = []
        offset = 0
        remaining_quota = limit if limit > 0 else 1000000
        query = ChannelParticipantsSearch(
            "") if limit == -1 else ChannelParticipantsRecent()
        while True:
            if remaining_quota <= 0:
                break
            response = await client(
                GetParticipantsRequest(entity,
                                       query,
                                       offset=offset,
                                       limit=min(remaining_quota, 200),
                                       hash=0))
            if not response.users:
                break
            users += _filter_participants(response.users,
                                          response.participants)
            offset += len(response.participants)
            remaining_quota -= len(response.participants)
        return users
Exemplo n.º 2
0
    async def _get_users(
        self, user: '******', entity: Union[TypeInputPeer, InputUser,
                                                  TypeChat, TypeUser,
                                                  InputChannel]
    ) -> Tuple[List[TypeUser], List[TypeParticipant]]:
        # TODO replace with client.get_participants
        if self.peer_type == "chat":
            chat = await user.client(GetFullChatRequest(chat_id=self.tgid))
            return chat.users, chat.full_chat.participants.participants
        elif self.peer_type == "channel":
            if not self.megagroup and not self.sync_channel_members:
                return [], []

            limit = self.max_initial_member_sync
            if limit == 0:
                return [], []

            try:
                if 0 < limit <= 200:
                    response = await user.client(
                        GetParticipantsRequest(entity,
                                               ChannelParticipantsRecent(),
                                               offset=0,
                                               limit=limit,
                                               hash=0))
                    return response.users, response.participants
                elif limit > 200 or limit == -1:
                    users: List[TypeUser] = []
                    participants: List[TypeParticipant] = []
                    offset = 0
                    remaining_quota = limit if limit > 0 else 1000000
                    query = (ChannelParticipantsSearch("")
                             if limit == -1 else ChannelParticipantsRecent())
                    while True:
                        if remaining_quota <= 0:
                            break
                        response = await user.client(
                            GetParticipantsRequest(entity,
                                                   query,
                                                   offset=offset,
                                                   limit=min(
                                                       remaining_quota, 100),
                                                   hash=0))
                        if not response.users:
                            break
                        participants += response.participants
                        users += response.users
                        offset += len(response.participants)
                        remaining_quota -= len(response.participants)
                    return users, participants
            except ChatAdminRequiredError:
                return [], []
        elif self.peer_type == "user":
            return [entity], []
        return [], []
def dump_users(client, chat_name):
    counter = 0
    offset = 0
    all_participants = []
    current_user = []
    while True:
        participants = client.invoke(
            GetParticipantsRequest(chat_name,
                                   filter=ChannelParticipantsRecent(),
                                   offset=offset,
                                   limit=200,
                                   hash=0))
        if not participants.users:
            break
        #if counter>5000:
        #	break

        for user in participants.users:
            current_user = []
            current_user.append(user.first_name)
            current_user.append(user.last_name)
            current_user.append(user.phone)
            current_user.append(user.username)
            all_participants.append(current_user)

        users_count = len(participants.users)
        offset += users_count
        counter += users_count
        print('{} users collected'.format(counter))
        time.sleep(0.5)

    file_name = "%s.csv" % str(chat_name)
    columns = ["first_name", "last_name", "phone", "username"]
    pd.DataFrame(all_participants, columns=columns).to_csv(file_name,
                                                           index=False)
Exemplo n.º 4
0
    def get_user_list(peer):
        participants = client(
            GetParticipantsRequest(peer,
                                   ChannelParticipantsRecent(),
                                   offset=0,
                                   limit=200,
                                   hash=0))
        users = participants.users

        while participants.count > len(users):
            participants = client(
                GetParticipantsRequest(peer,
                                       ChannelParticipantsRecent(),
                                       offset=len(users),
                                       limit=200,
                                       hash=0))
            users.extend(participants.users)
            sleep(0.5)

        return users
Exemplo n.º 5
0
def get_users(group_username):
    users_list = []
    channel = client(ResolveUsernameRequest(group_username))
    offset_counter = 0
    while True:
        users = client(GetParticipantsRequest(InputChannel(channel.peer.channel_id, channel.chats[0].access_hash), limit=200, offset=offset_counter, filter=ChannelParticipantsRecent()))
        if len(users.participants) == 0: break
        offset_counter += 200
        users_list.extend(users.users)
        time.sleep(5)
    return users_list
Exemplo n.º 6
0
def scrape_task(user_id, group_id, tg_account_id=None):
    user = User.objects.get(pk=user_id)
    group = TelegramGroup.objects.get(pk=group_id)
    if tg_account_id:
        account = TelegramAccount.objects.get(pk=tg_account_id)
    else:
        try:
            account = random.choice(
                TelegramAccount.objects.filter(active=True, confirmed=True))
        except IndexError:
            return {
                'success': False,
                'error': _('No available active accounts '
                           'for scrapping.')
            }
    proxy_settings = (socks.HTTP, settings.PROXY_HOST, settings.PROXY_PORT,
                      True, settings.PROXY_USERNAME +
                      '-session-{}'.format(random.randint(9999, 9999999)),
                      settings.PROXY_PASSWORD)
    account.set_is_used(True)
    client = TelegramClient(StringSession(account.session),
                            account.api_id,
                            account.api_hash,
                            proxy=proxy_settings)
    client.connect()

    try:
        try:
            group_entity = client.get_input_entity(
                group.username if group.username else group.join_link)
        except Exception as e:
            logger.exception(e)
            account.set_is_used(False)
            client.disconnect()
            return {'success': False, 'error': _('Error finding group.')}

        joined = join_group(client, account, group_entity)
        if not joined:
            account.set_is_used(False)
            client.disconnect()
            return {'success': False, 'error': _('Error joining group.')}
        elif isinstance(joined, dict):  # means there was an error
            account.set_is_used(False)
            client.disconnect()
            return joined

        try:
            members = client.get_participants(
                group_entity,
                aggressive=True,
                filter=ChannelParticipantsRecent())
            admins = client.get_participants(
                group_entity,
                aggressive=True,
                filter=ChannelParticipantsAdmins())
            members = [
                m for m in members if not m.bot and  # skip bots
                m not in admins and  # skip admins
                m.username and  # skip without usernames
                not m.is_self
            ]  # skip if members is current client user
        except:
            account.set_is_used(False)
            client.disconnect()
            return {
                'success': False,
                'error': _('Error getting list of group '
                           'members.')
            }
        client.disconnect()
        account.set_is_used(False)
        with transaction.atomic():
            for i, m in enumerate(members):
                TelegramContact.objects.get_or_create(user=user,
                                                      group=group,
                                                      username=m.username,
                                                      priority=i + 1)
        message = _('Scrapped {} users.'.format(len(members)))
        return {'success': True, 'error': None, 'message': message}
    except Exception as e:
        logger.exception(e)
        return {
            'success': True,
            'error': _('Error getting list of group '
                       'members.')
        }
Exemplo n.º 7
0
from telethon.tl.functions.users import GetUsersRequest
from telethon.tl.functions.channels import GetFullChannelRequest
from telethon.utils import get_input_peer
from telethon.tl.types.channels import ChannelParticipants
from telethon.tl.functions.messages import SendMessageRequest
import sys
import urllib 
import time
api_id = ******  # 
api_hash = '*******'
phone_number = '****8'

client = TelegramClient('%sweessionname%', api_id, api_hash)  
client.connect() # logining and connecting to Telegram servers

if not client.is_user_authorized():  # authorization (if there is no .session file created before)
    client.send_code_request(phone_number)
    client.sign_in(phone_number, input('Enter the code: '))

result = client(ResolveUsernameRequest('___NODEVIEWCOPY___33___NODEVIEWCOPY___'))
found_chats = result.chats
found_users = result.users

r = client(GetParticipantsRequest(result.chats[0],
filter=ChannelParticipantsRecent(),
                           # List of filters https://lonamiwebs.github.io/Telethon/types/channel_participants_filter.html
                           offset=___NODEVIEWCOPY___34___NODEVIEWCOPY___,  # getting info from 0th user
                           limit=5000)  # limiting number of users in a request
						   )

fusers=r.users