-
Notifications
You must be signed in to change notification settings - Fork 236
/
get_members.py
84 lines (71 loc) · 2.4 KB
/
get_members.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
#!/usr/bin/env python3
import sys
import os
import json
from datetime import datetime, timedelta
from telethon import sync, TelegramClient, events
from telethon.tl.types import InputPeerEmpty, UserStatusOffline, UserStatusRecently, UserStatusLastMonth, UserStatusLastWeek
from utils import *
with open('config.json', 'r', encoding='utf-8') as f:
config = json.loads(f.read())
root_path = os.path.dirname(os.path.abspath(__file__))
folder_session = root_path + '/session/'
folder_data = root_path + '/data/'
accounts = config['accounts']
group_source = config['group_source']
group_target = config['group_target']
api_id = int(config['api_id'])
api_hash = config['api_hash']
for phone in accounts:
client = TelegramClient(folder_session + phone, api_id, api_hash)
client.connect()
if not client.is_user_authorized():
logging.error('Login fail, check account ('+phone+'), need to run init_session')
else:
path_group = folder_data + str(group_source) + '/'
try:
os.mkdir(path_group)
except OSError as error:
pass
if isinstance(group_source, str):
data = get_member_by_group_username(client, group_source)
else:
data = get_member_by_group_id(client, group_source)
results = [] # list object member to save file
today = datetime.now()
last_week = today + timedelta(days=-7)
last_month = today + timedelta(days=-30)
for user in data:
try:
date_online_str = '19700102'
date_online = None
if not isinstance(user.username, type(None)):
if str(user.username[-3:]).lower() == "bot":
continue
else:
pass
if isinstance(user.status, UserStatusRecently):
date_online_str = 'online'
else:
if isinstance(user.status, UserStatusLastMonth):
date_online = last_month
if isinstance(user.status, UserStatusLastWeek):
date_online = last_week
if isinstance(user.status, UserStatusOffline):
date_online = user.status.was_online
if date_online:
date_online_str = date_online.strftime("%Y%m%d")
tmp = {
'user_id': user.id,
'access_hash': user.access_hash,
'username': user.username,
"date_online": date_online_str
}
results.append(tmp)
except BaseException:
traceback.print_exc()
logging.error("Error get user")
path_file = path_group + str(phone) + '.json'
with open(path_file, 'w', encoding='utf-8') as f:
json.dump(results, f, indent=4, ensure_ascii=False)
client.disconnect()