-
Notifications
You must be signed in to change notification settings - Fork 1
/
runfirestore.py
334 lines (257 loc) · 6.89 KB
/
runfirestore.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
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
# -*- coding: utf-8 -*-
# import modules
import datetime
import pandas as pd
import argparse
import asyncio
import json
import time as t
import sys
import os
# import Telegram API submodules
from api import *
from datetime import timedelta
from utils import (
get_config_attrs, JSONEncoder, create_dirs, cmd_request_type,
write_collected_chats
)
# import firebase submodules
import firebase_admin
from firebase_admin import credentials
from firebase_admin import firestore
'''
Arguments
'''
parser = argparse.ArgumentParser(description='Arguments.')
parser.add_argument(
'--telegram-channel',
type=str,
required='--batch-file' not in sys.argv,
help='Specifies a Telegram Channel.'
)
parser.add_argument(
'--batch-file',
type=str,
required='--telegram-channel' not in sys.argv,
help='File containing Telegram Channels, one channel per line.'
)
parser.add_argument(
'--limit-download-to-channel-metadata',
action='store_true',
help='Will collect channels metadata only, not posts data.'
)
'''
Output
'''
parser.add_argument(
'--output',
'-o',
type=str,
required=False,
help='Folder to save collected data. Default: `./output/data`'
)
'''
Updating data
'''
parser.add_argument(
'--min-id',
type=int,
help='Specifies the offset id. This will update Telegram data with new posts.'
)
# parse arguments
args = vars(parser.parse_args())
config_attrs = get_config_attrs()
args = {**args, **config_attrs}
if all(i is not None for i in args.values()):
parser.error('Select either --telegram-channel or --batch-file options only.')
# log results
text = f'''
Init program at {t.ctime()}
'''
print (text)
'''
Variables
'''
# Telegram API credentials
'''
FILL API KEYS
'''
sfile = 'session_file'
api_id = args['api_id']
api_hash = args['api_hash']
phone = args['phone']
# event loop
loop = asyncio.get_event_loop()
# data collection
counter = {}
'''
> Get Client <API connection>
'''
# get `client` connection
client = TelegramClient(sfile, api_id, api_hash)
async def run():
await client.connect()
"""await client.send_code_request(phone)
code = input('Enter the code you received: ')
await client.sign_in(phone, code)
print('Client Created') """
client.loop.run_until_complete(run())
# request type
req_type, req_input = cmd_request_type(args)
if req_type == 'batch':
req_input = [
i.rstrip() for i in open(
req_input, encoding='utf-8', mode='r'
)
]
else:
req_input = [req_input]
'''
Methods
- GetHistoryRequest
- SearchGlobalRequest
'''
# Initialize Firebase Admin SDK
cred = credentials.Certificate('./unified-acf08-firebase-adminsdk-xytc6-0f7c8f724e.json')
firebase_admin.initialize_app(cred)
db = firestore.client()
# iterate channels
for channel in req_input:
'''
Process arguments
-> channels' data
-> Get Entity <Channel's attrs>
-> Get Full Channel request.
-> Get Posts <Request channels' posts>
'''
# new line
print ('')
print (f'> Collecting data from Telegram Channel -> {channel}')
print ('> ...')
print ('')
# Channel's attributes
entity_attrs = loop.run_until_complete(
get_entity_attrs(client, channel)
)
if entity_attrs:
# Get Channel ID | convert output to dict
channel_id = entity_attrs.id
channel_user = entity_attrs.username
entity_attrs_dict = entity_attrs.to_dict()
# Collect Source -> GetFullChannelRequest
channel_request = loop.run_until_complete(
full_channel_req(client, channel_id)
)
# save full channel request
full_channel_data = channel_request.to_dict()
# JsonEncoder
full_channel_data = JSONEncoder().encode(full_channel_data)
full_channel_data = json.loads(full_channel_data)
if not args['limit_download_to_channel_metadata']:
channel_id = full_channel_data['full_chat']['id']
channel_name = full_channel_data['chats'][0]['title']
channel_username = full_channel_data['chats'][0]['username']
# Collect posts
if not args['min_id']:
posts = loop.run_until_complete(
get_posts(client, channel_id)
)
# save data
print ('> Writing channel data...')
doc_ref = db.collection('channels').document()
doc_ref.set({
'channel_id': channel_id,
'channel_name': channel_name,
'channel_username': channel_username
})
print ('> done.')
print ('')
else:
min_id = args['min_id']
posts = loop.run_until_complete(
get_posts(client, channel_id, min_id=min_id)
)
data = posts.to_dict()
# Get offset ID | Get messages
offset_id = min([i['id'] for i in data['messages']])
while len(posts.messages) > 0:
if args['min_id']:
posts = loop.run_until_complete(
get_posts(
client,
channel_id,
min_id=min_id,
offset_id=offset_id
)
)
else:
posts = loop.run_until_complete(
get_posts(
client,
channel_id,
offset_id=offset_id
)
)
# Update data dict
if posts.messages:
tmp = posts.to_dict()
data['messages'].extend(tmp['messages'])
# Adding unique chats objects
all_chats = [i['id'] for i in data['chats']]
chats = [
i for i in tmp['chats']
if i['id'] not in all_chats
]
# Adding unique users objects
all_users = [i['id'] for i in data['users']]
users = [
i for i in tmp['users']
if i['id'] not in all_users
]
# extend UNIQUE chats & users
data['chats'].extend(chats)
data['users'].extend(users)
# Get offset ID
offset_id = min([i['id'] for i in tmp['messages']])
# JsonEncoder
data = JSONEncoder().encode(data)
data = json.loads(data)
print('> Writing posts data...')
# extract and store data in Firestore
messages = data.get('messages', [])
for message in messages:
message_id = message.get('id')
channel_id = message.get('peer_id', {}).get('channel_id')
message_text = message.get('message')
timestamp = message.get('date')
current_date = datetime.datetime.now().date()
two_weeks_prior = current_date - timedelta(days=14)
message_date = datetime.datetime.strptime(timestamp, "%Y-%m-%d %H:%M:%S%z").date()
doc_ref = db.collection('news').document()
if message_text is not None and message_text != '' and two_weeks_prior <= message_date <= current_date:
doc_ref.set({
'channel_id': channel_id,
'channel_name': channel_name,
'channel_username': channel_username,
'message_id': message_id,
'timestamp': datetime.datetime.strptime(timestamp, "%Y-%m-%d %H:%M:%S%z"),
'message_text': message_text
})
print('> done.')
print('')
# sleep program for a few seconds
if len(req_input) > 1:
time.sleep(2)
else:
'''
Channels not found
'''
""" exceptions_path = f'{output_folder}/_exceptions-channels-not-found.txt'
w = open(exceptions_path, encoding='utf-8', mode='a')
w.write(f'{channel}\n')
w.close() """
# log results
text = f'''
End program at {t.ctime()}
'''
print (text)