-
Notifications
You must be signed in to change notification settings - Fork 60
/
Copy pathbuild-datasets.py
346 lines (279 loc) · 7.65 KB
/
build-datasets.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
335
336
337
338
339
340
341
342
343
344
345
346
# -*- coding: utf-8 -*-
# import modules
import pandas as pd
import argparse
import json
import glob
import time
import os
# import submodules
from tqdm import tqdm
# import local submodules
from utils import (
chats_dataset_columns, clean_msg, msg_attrs, get_forward_attrs, get_reply_attrs,
get_url_attrs, get_document_attrs, get_poll_attrs, get_contact_attrs,
get_geo_attrs, msgs_dataset_columns
)
'''
Arguments
'''
parser = argparse.ArgumentParser(description='Arguments.')
parser.add_argument(
'--data-path',
'-d',
type=str,
required=False,
help='Path where data is located. Will use `./output/data` if not given.'
)
# Parse arguments
args = vars(parser.parse_args())
# get main path
if args['data_path']:
main_path = args['data_path']
if main_path.endswith('/'):
main_path = main_path[:-1]
else:
main_path = './output/data'
# log results
text = f'''
Init program at {time.ctime()}
'''
print (text)
# Collect JSON files
json_files_path = f'{main_path}/**/*_messages.json'
json_files = glob.glob(
os.path.join(json_files_path),
recursive=True
)
# Collected channels
chats_file_path = f'{main_path}/collected_chats.csv'
data = pd.read_csv(chats_file_path, encoding='utf-8')
# Init values
data['collected_actions'] = 0
data['collected_posts'] = 0
data['replies'] = 0
data['other_actions'] = 0
data['number_views'] = 0
data['forwards'] = 0
data['replies_received'] = 0
# Save dataset
msgs_file_path = f'{main_path}/msgs_dataset.csv'
msgs_data_columns = msgs_dataset_columns()
# JSON files
for f in json_files:
'''
Iterate JSON files
'''
# Get channel name
username = f.split('.json')[0].replace('\\', '/').split('/')[-1].replace(
'_messages', ''
)
# Echo
print (f'Reading data from channel -> {username}')
# read JSON file
with open(f, encoding='utf-8', mode='r') as fl:
obj = json.load(fl)
fl.close()
'''
Get actions
'''
actions = obj['count']
posts = len(
[
i for i in obj['messages'] if 'message' in i.keys()
and i['reply_to'] == None
]
)
replies = len(
[
i for i in obj['messages'] if 'message' in i.keys()
and i['reply_to'] != None
]
)
other = len(
[
i for i in obj['messages'] if 'action' in i.keys()
]
)
'''
Attrs: views, forwards, replies
'''
views = sum(
[
i['views'] for i in obj['messages']
if 'views' in i.keys() and i['views'] != None
]
)
forwards = sum(
[
i['forwards'] for i in obj['messages']
if 'forwards' in i.keys() and i['forwards'] != None
]
)
replies_received = sum(
[
i['replies']['replies'] for i in obj['messages']
if 'replies' in i.keys() and i['replies'] != None
]
)
# Add values to dataset
data.loc[data['username'] == username, 'collected_actions'] = actions
data.loc[data['username'] == username, 'collected_posts'] = posts
data.loc[data['username'] == username, 'replies'] = replies
data.loc[data['username'] == username, 'other_actions'] = other
data.loc[data['username'] == username, 'number_views'] = views
data.loc[data['username'] == username, 'forwards'] = forwards
data.loc[data['username'] == username, 'replies_received'] = replies_received
'''
Reading posts
'''
messages = obj['messages']
pbar = tqdm(total=len(messages))
pbar.set_description(f'Reading posts')
# main object
response = {
'channel_name': username
}
for idx, item in enumerate(messages):
'''
Iterate posts
'''
if item['_'] == 'Message':
# channel id
response['channel_id'] = item['peer_id']['channel_id']
# message id
msg_id = item['id']
response['msg_id'] = msg_id
# add attrs
response['message'] = item['message']
# clean message
msg = clean_msg(item['message'])
response['cleaned_message'] = msg
# timestamp
date = item['date']
response['date'] = date
# signature and message link
response['signature'] = \
f'msg_iteration.{idx}.user.{username}.post.{msg_id}'
response['msg_link'] = f'https://t.me/{username}/{msg_id}'
# check peer
response['msg_from_peer'] = None
response['msg_from_id'] = None
response = msg_attrs(item, response)
# reactions
response['views'] = 0 if item['views'] == None else item['views']
response['number_replies'] = \
item['replies']['replies'] if item['replies'] != None else 0
response['number_forwards'] = 0 if item['forwards'] == None \
else item['forwards']
# Forward attrs
forward_attrs = item['fwd_from']
response['is_forward'] = 1 if forward_attrs != None else 0
response['forward_msg_from_peer_type'] = None
response['forward_msg_from_peer_id'] = None
response['forward_msg_from_peer_name'] = None
response['forward_msg_date'] = None
response['forward_msg_date_string'] = None
response['forward_msg_link'] = None
if forward_attrs:
response = get_forward_attrs(
forward_attrs,
response,
data
)
# Reply attrs
response['is_reply'] = 0
response['reply_to_msg_id'] = None
response['reply_msg_link'] = None
response = get_reply_attrs(
item,
response,
username
)
# Media
response['contains_media'] = 1 if item['media'] != None else 0
if 'media' in item.keys():
response['media_type'] = None if item['media'] == None \
else item['media']['_']
# URLs -> Constructor MessageMediaWebPage
'''
Type WebPage
Source: https://core.telegram.org/constructor/messageMediaWebPage
Telethon: https://tl.telethon.dev/constructors/web_page.html
'''
response = get_url_attrs(item['media'], response)
# Media Document -> Constructor MessageMediaDocument
'''
Type Document
Source: https://core.telegram.org/constructor/messageMediaDocument
Telethon: https://tl.telethon.dev/constructors/document.html
'''
response['document_type'] = None
response['document_id'] = None
response['document_video_duration'] = None
response['document_filename'] = None
response = get_document_attrs(item['media'], response)
# Polls attrs
'''
Type Poll
Source: https://core.telegram.org/constructor/messageMediaPoll
Telethon: https://tl.telethon.dev/constructors/poll.html
'''
response['poll_id'] = None
response['poll_question'] = None
response['poll_total_voters'] = None
response['poll_results'] = None
response = get_poll_attrs(item['media'], response)
# Contact attrs
'''
Type Contact
Source: https://core.telegram.org/constructor/messageMediaContact
Telethon: https://tl.telethon.dev/constructors/message_media_contact.html
'''
response['contact_phone_number'] = None
response['contact_name'] = None
response['contact_userid'] = None
response = get_contact_attrs(item['media'], response)
# Geo attrs
'''
Type GeoPoint
Source: https://core.telegram.org/constructor/messageMediaGeo
Telethon:
> https://tl.telethon.dev/constructors/geo_point.html
> https://tl.telethon.dev/constructors/message_media_venue.html
'''
response['geo_type'] = None
response['lat'] = None
response['lng'] = None
response['venue_id'] = None
response['venue_type'] = None
response['venue_title'] = None
response['venue_address'] = None
response['venue_provider'] = None
response = get_geo_attrs(item['media'], response)
# create dataframe
res = [response]
df = pd.DataFrame.from_dict(res)
# order df msgs data columns
df = df[msgs_data_columns].copy()
# update CSV file
df.to_csv(
msgs_file_path,
encoding='utf-8',
header=not os.path.isfile(msgs_file_path),
index=False,
mode='a'
)
# Update pbar
pbar.update(1)
# Close pbar connection
pbar.close()
print ('-- END --')
print ('')
# Save data
chats_columns = chats_dataset_columns()
data = data[chats_columns].copy()
data.to_excel(
chats_file_path.replace('.csv', '.xlsx'),
index=False
)