From 88ad2a0e854015a4f1ab67a2b129fb79fcd2eddb Mon Sep 17 00:00:00 2001 From: South Date: Sat, 18 Jul 2020 09:49:09 +0700 Subject: [PATCH] new version: save user json. Need to update addmember --- config.example.json | 2 + get_data.py | 89 +++++++++++++++++++++++++++++++++++++++++++++ getonlygroup.py | 64 -------------------------------- 3 files changed, 91 insertions(+), 64 deletions(-) create mode 100644 get_data.py delete mode 100644 getonlygroup.py diff --git a/config.example.json b/config.example.json index bbde2275..8ffaef4e 100644 --- a/config.example.json +++ b/config.example.json @@ -1,4 +1,6 @@ { + "group_target": 1398120166, + "group_source": 1490302444, "accounts": [ { "phone": "+84XXXX", diff --git a/get_data.py b/get_data.py new file mode 100644 index 00000000..dbe0134d --- /dev/null +++ b/get_data.py @@ -0,0 +1,89 @@ +from telethon import TelegramClient, connection +import logging +from telethon import sync, TelegramClient, events +from telethon.tl.functions.messages import GetDialogsRequest +from telethon.tl.types import InputPeerEmpty +import json + +logging.basicConfig(level=logging.WARNING) + +def get_group(phone, api_id, api_hash): + folder_session = 'session/' + client = TelegramClient(folder_session + phone, api_id, api_hash) + client.connect() + if not client.is_user_authorized(): + print('Login fail, need to run init_session') + else: + get_data_group(client, phone) + +def get_data_group(client, phone): + print('getting data ' + phone) + chats = [] + last_date = None + chunk_size = 200 + groups=[] + + query = client(GetDialogsRequest( + offset_date=last_date, + offset_id=0, + offset_peer=InputPeerEmpty(), + limit=chunk_size, + hash = 0 + )) + chats.extend(query.chats) + for chat in chats: + try: + if chat.megagroup is not None and chat.access_hash is not None: + groups.append(chat) + except: + continue + + results = [] + for group in groups: + try: + tmp = { + 'group_id': str(group.id), + 'access_hash': str(group.access_hash), + 'title': str(group.title), + } + results.append(tmp) + + if group.megagroup == True: + get_data_user(client, group) + except Exception as e: + print(e) + print('error save group') + with open('data/group/' + phone + '.json', 'w') as f: + json.dump(results, f, indent=4, ensure_ascii=False) + +def get_data_user(client, group): + group_id = str(group.id) + print(group_id) + + all_participants = [] + all_participants = client.get_participants(group, aggressive=True) + results = [] + for user in all_participants: + tmp = { + 'user_id': str(user.id), + 'access_hash': str(user.access_hash), + 'username': str(user.username) + } + results.append(tmp) + with open('data/user/' + phone + "_" + group_id +'.json', 'w') as f: + json.dump(results, f, indent=4, ensure_ascii=False) + + +with open('config.json', 'r') as f: + config = json.loads(f.read()) + +accounts = config['accounts'] + +folder_session = 'session/' + +for account in accounts: + api_id = account['api_id'] + api_hash = account['api_hash'] + phone = account['phone'] + print(phone) + get_group(phone, api_id, api_hash) \ No newline at end of file diff --git a/getonlygroup.py b/getonlygroup.py deleted file mode 100644 index 005cbe74..00000000 --- a/getonlygroup.py +++ /dev/null @@ -1,64 +0,0 @@ -from telethon import TelegramClient, connection -import logging -from telethon import sync, TelegramClient, events -from telethon.tl.functions.messages import GetDialogsRequest -from telethon.tl.types import InputPeerEmpty - -logging.basicConfig(level=logging.WARNING) - -with open('phone.txt') as f: - data_client = f.read().split("\n") - -for row_client in data_client: - split_row_client = row_client.split(";") - if(split_row_client.__len__() > 2): - phone = split_row_client[0] - api_id = int(split_row_client[1]) - api_hash = split_row_client[2] - - client = TelegramClient(phone, api_id, api_hash, connection=connection.ConnectionTcpMTProxyRandomizedIntermediate, proxy=('116.203.2.245', 1337, 'dd81b667f85e7e5d358de3b8e4ade6302f')) - - client.connect() - if not client.is_user_authorized(): - print('dang nhap khong thanh cong') - else: - print('getting data ' + phone) - chats = [] - last_date = None - chunk_size = 200 - groups=[] - - result = client(GetDialogsRequest( - offset_date=last_date, - offset_id=0, - offset_peer=InputPeerEmpty(), - limit=chunk_size, - hash = 0 - )) - chats.extend(result.chats) - for chat in chats: - try: - if chat.megagroup is not None and chat.access_hash is not None: - groups.append(chat) - except: - continue - - str_group = '' - for group in groups: - - try: - group_id = str(group.id) - access_hash = str(group.access_hash) - title = str(group.title) - - row_group = group_id + ";" + access_hash + ";" + title + "\n" - - str_group += row_group - - except Exception as e: - print('loi lay user') - - - with open('data/group/' + phone + '.csv', 'w') as g: - g.write(str_group) - g.close() \ No newline at end of file