Skip to content

Commit

Permalink
new version: save user json. Need to update addmember
Browse files Browse the repository at this point in the history
  • Loading branch information
South committed Jul 18, 2020
1 parent acd7332 commit 88ad2a0
Show file tree
Hide file tree
Showing 3 changed files with 91 additions and 64 deletions.
2 changes: 2 additions & 0 deletions config.example.json
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
{
"group_target": 1398120166,
"group_source": 1490302444,
"accounts": [
{
"phone": "+84XXXX",
Expand Down
89 changes: 89 additions & 0 deletions get_data.py
Original file line number Diff line number Diff line change
@@ -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)
64 changes: 0 additions & 64 deletions getonlygroup.py

This file was deleted.

0 comments on commit 88ad2a0

Please sign in to comment.