-
Notifications
You must be signed in to change notification settings - Fork 1
/
table_class.py
973 lines (737 loc) · 33.6 KB
/
table_class.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
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
import json
import argparse
import os
import pandas as pd
import re
class TempDFs(object):
def __init__(self,
json_content,
table_prefix='temp'
):
self.df_list, self.table_name_list, \
self.id_column_names_list, self.column_names_list = \
self.get_temp_dfs(json_content, table_prefix)
self.debug_df_list, self.debug_table_name_list, \
self.debug_id_column_names_list, self.debug_column_names_list = \
None, None, None, None
def get_temp_dfs(self, json_content, table_name_start, no_peek=False):
if no_peek:
row_table_list = self.go_through_dict_list_no_peek(
json_content, table_names=[table_name_start])
else:
row_table_list = self.go_through_dict_list(
json_content, table_names=[table_name_start])
table_dict = self.row_table2table_row(row_table_list)
sorted_table_dict = self.reassign_id(table_dict)
easier_table_dict = self.make_easier_insert_row(sorted_table_dict)
table_column_dict = self.get_total_column_names(easier_table_dict)
for key, dvalue in table_column_dict.items():
for kkey, ddvalue in dvalue.items():
easier_table_dict[key][kkey] = ddvalue
df_list = []
table_name_list = []
id_column_names_list = []
column_names_list = []
temp_df = pd.DataFrame()
for key, dvalue in easier_table_dict.items():
temp_df = temp_df.from_records(dvalue['row_list'])
df_list.append(temp_df)
table_name_list.append(dvalue['table_names'][-1])
id_column_names_list.append(dvalue['id_column_names'])
column_names_list.append(dvalue['column_names'])
df_list, table_name_list, \
id_column_names_list, column_names_list = \
self.sort_by_id_len(df_list, table_name_list,
id_column_names_list, column_names_list)
return df_list, table_name_list, \
id_column_names_list, column_names_list
def go_through_dict_list(self, target, id_column_names=['id_0'], peeling=0,
id_counters=[0], table_names=['unnamed'],
id_count_from=0):
# the method that get the key-value pair,
# and record the structure of getting the pair
results = []
if isinstance(target, dict):
column_names = []
values = []
for key, dvalue in target.items():
if isinstance(dvalue, dict) | isinstance(dvalue, list):
temp = id_column_names + \
['id_' + key + '_' + str(peeling + 1)]
temp2 = table_names + [table_names[-1] + '__' + str(key)]
# if the dvalue is a list containing no dict or list
if isinstance(dvalue, list) & \
(True not in [isinstance(x, dict) for x in dvalue]) & \
(True not in [isinstance(x, list) for x in dvalue]):
for dva in dvalue:
results = results + \
self.go_through_dict_list(
{key: dva},
id_column_names=temp,
peeling=peeling + 1,
table_names=temp2,
id_counters=id_counters + [id_count_from],
id_count_from=id_count_from)
else:
results = results + \
self.go_through_dict_list(
dvalue,
id_column_names=temp,
peeling=peeling + 1,
table_names=temp2,
id_counters=id_counters + [id_count_from],
id_count_from=id_count_from)
else:
column_names.append(key)
values.append(dvalue)
results = results + [[table_names, id_column_names,
column_names, id_counters,
values, peeling]]
return results
elif isinstance(target, list):
pre_id_counter = id_count_from
if False not in [isinstance(x, dict) for x in target]:
dict_keys = []
for targ in target:
for targ_key in targ.keys():
dict_keys.append(targ_key)
appear_times = list(map(dict_keys.count, dict_keys))
if sum(appear_times) == len(appear_times):
new_targ = {}
for targ in target:
for targ_key, targ_value in targ.items():
new_targ[targ_key] = targ_value
target = [new_targ]
for targ in target:
id_counters = id_counters + []
id_counters[-1] = pre_id_counter
if isinstance(targ, dict):
results = results + \
self.go_through_dict_list(
targ,
id_column_names=id_column_names,
peeling=peeling,
id_counters=id_counters,
table_names=table_names,
id_count_from=id_count_from)
pre_id_counter += 1
else:
raise Exception()
return results
def go_through_dict_list_no_peek(self, target,
id_column_names=['id_0'], peeling=0,
id_counters=[0], table_names=['unnamed'],
id_count_from=0):
# the method that get the key-value pair,
# and record the structure of getting the pair
# but no peek the value
results = []
if isinstance(target, dict):
column_names = []
values = []
for key, dvalue in target.items():
if isinstance(dvalue, dict) | isinstance(dvalue, list):
temp = id_column_names + \
['id_' + key + '_' + str(peeling + 1)]
temp2 = table_names + [table_names[-1] + '__' + str(key)]
# if the dvalue is a list containing no dict or list
if isinstance(dvalue, list) & \
(True not in [isinstance(x, dict) for x in dvalue]) & \
(True not in [isinstance(x, list) for x in dvalue]):
for dva in dvalue:
results = results + \
self.go_through_dict_list_no_peek(
{key: "be elegant"},
id_column_names=temp,
peeling=peeling + 1,
table_names=temp2,
id_counters=id_counters + [id_count_from],
id_count_from=id_count_from)
else:
results = results + \
self.go_through_dict_list_no_peek(
dvalue,
id_column_names=temp,
peeling=peeling + 1,
table_names=temp2,
id_counters=id_counters + [id_count_from],
id_count_from=id_count_from)
else:
column_names.append(key)
values.append("be elegant")
results = results + [[table_names, id_column_names,
column_names, id_counters,
values, peeling]]
return results
elif isinstance(target, list):
pre_id_counter = id_count_from
if False not in [isinstance(x, dict) for x in target]:
dict_keys = []
for targ in target:
for targ_key in targ.keys():
dict_keys.append(targ_key)
appear_times = list(map(dict_keys.count, dict_keys))
if sum(appear_times) == len(appear_times):
new_targ = {}
for targ in target:
for targ_key, targ_value in targ.items():
new_targ[targ_key] = targ_value
target = [new_targ]
for targ in target:
id_counters = id_counters + []
id_counters[-1] = pre_id_counter
if isinstance(targ, dict):
results = results + \
self.go_through_dict_list_no_peek(
targ,
id_column_names=id_column_names,
peeling=peeling,
id_counters=id_counters,
table_names=table_names,
id_count_from=id_count_from)
pre_id_counter += 1
else:
print(type(targ))
print("something with no column name")
raise Exception()
return results
def row_table2table_row(self, row_table):
# row_table records every single row accompany its table info
# here we rewrite it into tables with tables' rows
table_dict = {}
for table_names, id_column_names, \
column_names, id_counters, \
values, peeling in row_table:
p_column_names = []
# group is an illegal column_name and causes many problems in SQL
for column_name in column_names:
if column_name == 'group':
column_name = 'grouq'
p_column_names.append(column_name)
if table_names[-1] not in table_dict.keys():
table_dict[table_names[-1]] = {
'id_column_names_list': [id_column_names],
'column_names_list': [p_column_names],
'values_list': [values],
'id_counters_list': [id_counters],
'table_names': table_names,
'peeling': peeling,
'column_name_rows': [p_column_names]}
else:
table_dict[table_names[-1]
]['id_column_names_list'].append(id_column_names)
table_dict[table_names[-1]
]['column_names_list'].append(p_column_names)
table_dict[table_names[-1]]['values_list'].append(values)
table_dict[table_names[-1]
]['id_counters_list'].append(id_counters)
table_dict[table_names[-1]
]['column_name_rows'].append(p_column_names)
return table_dict
def reassign_id(self, table_dict, id_count_from=0):
reassign_id_table_dict = {}
# sort the table by how many dicts or lists be unpacked
table_list = []
for key, dvalue in table_dict.items():
table_list.append([key, dvalue.copy()])
table_list.sort(key=lambda x: x[1]['peeling'])
# reassign id due to that almost tables' foreign keys will change
sorted_table_dict = {}
id_map = {}
for table_name, table in table_list:
id_map[table_name] = {}
present_id = id_count_from
reassigned_id_counters_list = []
for id_counters in table['id_counters_list']:
if len(id_counters) == 1:
id_map[table_name][str(id_counters)] = [present_id]
else:
after_id_counters = id_counters + []
after_id_counters[-1] = present_id
up_id_map = id_map[table['table_names'][-2]]
after_id_counters[:-1] = \
up_id_map[str(after_id_counters[:-1])]
id_map[table_name][str(id_counters)] = after_id_counters
present_id += 1
reassigned_id_counters_list.append(
id_map[table_name][str(id_counters)])
table['id_counters_list'] = reassigned_id_counters_list
reassign_id_table_dict[table_name] = table
return reassign_id_table_dict
def make_easier_insert_row(self, table_dict):
easier_table_dict = {}
# sort the table by how many dicts or lists be unpacked
table_list = []
for key, dvalue in table_dict.items():
table_list.append([key, dvalue.copy()])
table_list.sort(key=lambda x: x[1]['peeling'])
for table_name, table in table_list:
table['row_list'] = []
for column_names, values, \
id_column_names, id_counters in zip(
table['column_names_list'], table['values_list'],
table['id_column_names_list'], table['id_counters_list']):
row = {}
for id_column_name, id_counter in zip(
id_column_names, id_counters):
row[id_column_name] = id_counter
for column_name, value in zip(column_names, values):
row[column_name] = value
table['row_list'].append(row)
easier_table_dict[table_name] = table
return easier_table_dict
def get_total_column_names(self, table_dict):
# here we figure out tables' total id_column_names and column_names
table_column_dict = {}
for key, dvalue in table_dict.items():
id_column_names = []
table_column_dict[key] = {}
for i in dvalue['id_column_names_list']:
for j in i:
if j not in id_column_names:
id_column_names.append(j)
column_names = []
for i in dvalue['column_names_list']:
for j in i:
if j not in column_names:
column_names.append(j)
table_column_dict[key]['id_column_names'] = id_column_names
table_column_dict[key]['column_names'] = column_names
return table_column_dict
def sort_by_id_len(self, df_list, table_name_list,
id_column_names_list, column_names_list):
sorted_df_list = [df for df, id_column_names in
sorted(zip(df_list, id_column_names_list),
key=lambda x: len(x[1]))]
sorted_table_name_list = [table_name for table_name, id_column_names in
sorted(zip(table_name_list,
id_column_names_list),
key=lambda x: len(x[1]))]
sorted_column_names_list = [column_names for column_names,
id_column_names in
sorted(zip(column_names_list,
id_column_names_list),
key=lambda x: len(x[1]))]
sorted_id_column_names_list = sorted(id_column_names_list,
key=lambda x: len(x))
return sorted_df_list, sorted_table_name_list, \
sorted_id_column_names_list, sorted_column_names_list
def get_start_peeling(self, df_list, by='timestamp'):
for i, df in enumerate(df_list):
if str(by) in df.columns:
return i
break
def merge_all_sub_df(self, df_list, table_name_list, id_column_names_list,
start_peeling=0):
top_df = df_list[start_peeling]
sub_df_list = df_list[start_peeling + 1:]
sub_table_name_list = table_name_list[start_peeling + 1:]
sub_id_column_names_list = id_column_names_list[start_peeling + 1:]
temp_df = top_df.copy()
for df, table_name, id_column_names in zip(
sub_df_list, sub_table_name_list, sub_id_column_names_list):
temp_df = temp_df.set_index(id_column_names[-2], drop=False)
append_df = df.drop(
labels=id_column_names[:-2], axis=1
).set_index(id_column_names[-2])
temp_df = temp_df.join(append_df,
rsuffix='_' + table_name.split('__')[-1])
return temp_df.reset_index(drop=True)
def merge_one_to_one_sub_df(
self,
df_list,
table_name_list,
id_column_names_list,
start_peeling=0):
top_df = df_list[start_peeling]
top_id_column_names = id_column_names_list[start_peeling]
sub_df_list = df_list[start_peeling + 1:]
sub_table_name_list = table_name_list[start_peeling + 1:]
sub_id_column_names_list = id_column_names_list[start_peeling + 1:]
temp_df = top_df.copy()
temp_df = temp_df.set_index(top_id_column_names[-1], drop=False)
for df, table_name, id_column_names in zip(
sub_df_list, sub_table_name_list, sub_id_column_names_list):
if top_id_column_names[-1] in id_column_names:
if all(df[top_id_column_names[-1]].value_counts() < 2):
append_df = df.set_index(top_id_column_names[-1])
append_df.columns = append_df.columns.map(
lambda x: str(table_name.split('__')[-1]) + '_' + x)
temp_df = temp_df.join(
append_df, rsuffix='_' + table_name.split('__')[-1])
return temp_df.reset_index(drop=True)
def get_start_peeling_regex(self, df_list, by='timestamp'):
for i, df in enumerate(df_list):
if any(list(map(lambda x: bool(re.match(by, x)), df.columns))):
return i
break
def get_wanted_columns(self, df, wanted_columns=[]):
returned_df = pd.DataFrame(columns=wanted_columns)
for column in wanted_columns:
if column in df.columns:
returned_df[[column]] = df[[column]]
else:
pass
return returned_df
def append_date_weekday_column(self, df,
timestamp_column_name='timestamp',
unit='s'):
df.loc[:, 'date'] = pd.to_datetime(df[timestamp_column_name],
unit=unit)
df.loc[:, 'weekday'] = df.loc[:, 'date'].dt.weekday
df.loc[:, 'date'] = df.loc[:, 'date'].dt.tz_localize('UTC')
df.loc[:, 'date'] = df.loc[:, 'date'].dt.strftime('%Y-%m-%d')
def get_routed_dfs(self, df_list, table_name_list,
id_column_names_list, by_table_name=None):
if by_table_name is None:
return df_list, \
table_name_list, \
id_column_names_list
for i, table_name in enumerate(table_name_list):
if by_table_name in table_name.split('__'):
anchor = i
break
routed_df_list = []
routed_table_name_list = []
routed_id_column_names_list = []
for i in range(0, anchor):
if table_name_list[i] in table_name_list[anchor]:
routed_df_list.append(df_list[i])
routed_table_name_list.append(table_name_list[i])
routed_id_column_names_list.append(id_column_names_list[i])
for i in range(anchor, len(table_name_list)):
if table_name_list[anchor] in table_name_list[i]:
routed_df_list.append(df_list[i])
routed_table_name_list.append(table_name_list[i])
routed_id_column_names_list.append(id_column_names_list[i])
return routed_df_list, \
routed_table_name_list, \
routed_id_column_names_list
def temp_to_wanted_df(self,
wanted_columns=[],
route_by_table_name=None,
start_by='timestamp',
regex=False):
df_list, \
table_name_list, \
id_column_names_list = self.get_routed_dfs(
self.df_list,
self.table_name_list,
self.id_column_names_list,
by_table_name=route_by_table_name)
if isinstance(start_by, int):
start_peeling = start_by
elif regex:
start_peeling = self.get_start_peeling_regex(df_list,
by=start_by)
else:
start_peeling = self.get_start_peeling(df_list,
by=start_by)
top_id = id_column_names_list[start_peeling][-1]
one_to_one_df = self.merge_one_to_one_sub_df(
df_list,
table_name_list,
id_column_names_list,
start_peeling=start_peeling)
if wanted_columns == []:
return one_to_one_df, top_id
else:
wanted_columns = [top_id] + wanted_columns
wanted_df = self.get_wanted_columns(one_to_one_df,
wanted_columns)
return wanted_df, top_id
def get_debug_structure(self, json_content, table_prefix):
self.debug_df_list, self.debug_table_name_list, \
self.debug_id_column_names_list, self.debug_column_names_list = \
self.get_temp_dfs(json_content, table_prefix, no_peek=True)
class PostsDFs(TempDFs):
def __init__(self,
json_content,
table_prefix='your_posts'):
super(PostsDFs, self).__init__(
json_content,
table_prefix=table_prefix)
try:
self.posts_df, self.posts_top_id = self.get_posts_df()
except Exception as e:
print(e, "fail to parse post in your_posts")
self.get_debug_structure(json_content, table_prefix)
self.posts_df = None
try:
self.media_df, self.media_top_id = self.get_posts_media_df()
except Exception as e:
print(e, "fail to parse media in your_posts")
self.get_debug_structure(json_content, table_prefix)
self.media_df = None
try:
self.posts_df_append_media_attached()
except Exception as e:
print(e, "fail to append media_attached in posts")
try:
self.place_df, self.place_top_id = self.get_posts_place_df()
except Exception as e:
print(e, "fail to parse place in your_posts")
self.get_debug_structure(json_content, table_prefix)
self.place_df = None
try:
self.tags_df, self.tags_top_id = self.get_posts_tags_df()
except Exception as e:
print(e, "fail to parse tags in your_posts")
self.get_debug_structure(json_content, table_prefix)
self.tags_df = None
def get_posts_df(self):
wanted_columns = ['timestamp',
'data_update_timestamp',
'title',
'data_post',
'external_context_url',
'external_context_source',
'external_context_name',
'event_name',
'event_start_timestamp',
'event_end_timestamp']
df, top_id = self.temp_to_wanted_df(
wanted_columns=wanted_columns)
df.rename(columns={
top_id: 'post_id',
"data_update_timestamp": "update_timestamp",
"data_post": "post"}, inplace=True)
self.append_date_weekday_column(df)
return df, top_id
def get_posts_media_df(self):
wanted_columns = ['creation_timestamp',
'description',
'uri',
'title',
'thumbnail_uri',
self.posts_top_id]
df, top_id = self.temp_to_wanted_df(
wanted_columns=wanted_columns,
route_by_table_name='media',
start_by='uri')
df.loc[:, 'filename_extension'] = \
df['uri'].map(lambda x: os.path.splitext(x)[-1])
df.rename(columns={
top_id: 'pm_id',
'uri': 'media_uri',
self.posts_top_id: 'post_id'}, inplace=True)
self.append_date_weekday_column(
df, timestamp_column_name='creation_timestamp', unit='s')
return df, top_id
def posts_df_append_media_attached(self):
self.posts_df.loc[:, 'media_attached'] = False
if self.media_df is not None:
posts_id_with_media = self.media_df['post_id']
self.posts_df.loc[posts_id_with_media, 'media_attached'] = True
def get_posts_place_df(self):
wanted_columns = ['address',
'coordinate_latitude',
'coordinate_longitude',
'name',
self.posts_top_id]
df, top_id = self.temp_to_wanted_df(
wanted_columns=wanted_columns,
route_by_table_name='place',
start_by='address')
df.rename(columns={
top_id: 'pp_id',
'coordinate_latitude': 'latitude',
'coordinate_longitude': 'longitude',
self.posts_top_id: 'post_id'}, inplace=True)
return df, top_id
def get_posts_tags_df(self):
wanted_columns = ['tags',
self.posts_top_id]
df, top_id = self.temp_to_wanted_df(
wanted_columns=wanted_columns,
start_by='tags')
df.rename(columns={
top_id: 'tf_id',
self.posts_top_id: 'post_id'}, inplace=True)
return df, top_id
class FriendsDFs(TempDFs):
def __init__(self,
json_content,
table_prefix='friends'):
super(FriendsDFs, self).__init__(
json_content,
table_prefix=table_prefix)
try:
self.friends_df, self.friends_top_id = self.get_friends_df()
except Exception as e:
print(e, "fail to parse friends in friends")
self.get_debug_structure(json_content, table_prefix)
self.friends_df = None
def get_friends_df(self):
wanted_columns = ['name', 'timestamp']
df, top_id = self.temp_to_wanted_df(
wanted_columns=wanted_columns)
df.rename(columns={"name": "friend_name"}, inplace=True)
# self.append_date_weekday_column(friends_df,
# timestamp_column_name='timestamp',
# unit='s')
return df, top_id
class MessagesDFs(TempDFs):
def __init__(self,
json_content,
table_prefix='messages'):
super(MessagesDFs, self).__init__(
json_content,
table_prefix=table_prefix)
try:
self.thread_df, self.thread_top_id = self.get_thread_df()
except Exception as e:
print(e, "fail to parse thread in messages")
self.get_debug_structure(json_content, table_prefix)
self.thread_df = None
try:
self.participants_df, self.participants_top_id = \
self.get_participants_df()
except Exception as e:
print(e, "fail to parse participants in messages")
self.get_debug_structure(json_content, table_prefix)
self.participants_df = None
try:
self.messages_df, self.messages_top_id = self.get_messages_df()
except Exception as e:
print(e, "fail to parse messages in messages")
self.get_debug_structure(json_content, table_prefix)
self.messages_df = None
def get_thread_df(self):
wanted_columns = ['is_still_participant',
'thread_path',
'thread_type',
'title']
df, top_id = self.temp_to_wanted_df(
wanted_columns=wanted_columns,
start_by='thread_path')
df.rename(columns={top_id: 'thread_id'}, inplace=True)
return df, top_id
def get_participants_df(self):
wanted_columns = ['name',
self.thread_top_id]
df, top_id = self.temp_to_wanted_df(
wanted_columns=wanted_columns,
regex=True,
start_by='id_participants')
df.rename(columns={self.thread_top_id: 'thread_id',
top_id: 'participants_id'}, inplace=True)
return df, top_id
def get_messages_df(self):
wanted_columns = ['content',
'sender_name',
'timestamp_ms',
self.thread_top_id]
df, top_id = self.temp_to_wanted_df(
wanted_columns=wanted_columns,
start_by='timestamp_ms')
df.rename(columns={self.thread_top_id: 'thread_id',
top_id: 'message_id'}, inplace=True)
self.append_date_weekday_column(df,
timestamp_column_name='timestamp_ms',
unit='ms')
return df, top_id
class ReactionsDFs(TempDFs):
def __init__(self,
json_content,
table_prefix='reactions'):
super(ReactionsDFs, self).__init__(
json_content,
table_prefix=table_prefix)
try:
self.reactions_df, self.reactions_top_id = self.get_reactions_df()
except Exception as e:
print(e, "fail to parse reactions in reactions")
self.get_debug_structure(json_content, table_prefix)
self.reactions_df = None
def get_reactions_df(self):
wanted_columns = ['timestamp',
'title',
'reaction_actor',
'reaction_reaction']
df, top_id = self.temp_to_wanted_df(
wanted_columns=wanted_columns
)
df.rename(columns={top_id: 'reaction_id',
'reaction_actor': 'actor',
'reaction_reaction': 'reaction'}, inplace=True)
self.append_date_weekday_column(df)
return df, top_id
class CommentsDFs(TempDFs):
def __init__(self,
json_content,
table_prefix='comments'):
super(CommentsDFs, self).__init__(
json_content,
table_prefix=table_prefix)
try:
self.comments_df, self.comments_top_id = self.get_comments_df()
except Exception as e:
print(e, "fail to parse comments in comments")
self.get_debug_structure(json_content, table_prefix)
self.comments_df = None
def get_comments_df(self):
wanted_columns = ['timestamp',
'comment_author',
'comment_comment']
df, top_id = self.temp_to_wanted_df(
wanted_columns=wanted_columns
)
df.rename(columns={top_id: 'comments_id',
'comment_author': 'author',
'comment_comment': 'comment'}, inplace=True)
self.append_date_weekday_column(df)
return df, top_id
class PhotosDFs(TempDFs):
def __init__(self,
json_content,
table_prefix='photos'):
super(PhotosDFs, self).__init__(
json_content,
table_prefix=table_prefix)
try:
self.photos_df, self.photos_top_id = self.get_photos_df()
except Exception as e:
print(e, "fail to parse photos in photos")
self.get_debug_structure(json_content, table_prefix)
self.photos_df = None
def get_photos_df(self):
wanted_columns = ['creation_timestamp',
'uri',
'description']
df, top_id = self.temp_to_wanted_df(
wanted_columns=wanted_columns,
regex=True,
start_by='id_photos*')
df.rename(columns={top_id: 'photo_id',
'uri': 'media_uri'}, inplace=True)
self.append_date_weekday_column(
df, timestamp_column_name='creation_timestamp')
return df, top_id
class VideosDFs(TempDFs):
def __init__(self,
json_content,
table_prefix='video'):
super(VideosDFs, self).__init__(
json_content,
table_prefix=table_prefix)
try:
self.videos_df, self.videos_top_id = self.get_videos_df()
except Exception as e:
print(e, "fail to parse videos in videos")
self.get_debug_structure(json_content, table_prefix)
self.videos_df = None
def get_videos_df(self):
wanted_columns = ['creation_timestamp',
'uri',
'description',
'thumbnail_uri']
df, top_id = self.temp_to_wanted_df(
wanted_columns=wanted_columns,
regex=True,
start_by='id_videos*')
df.rename(columns={top_id: 'video_id',
'uri': 'media_uri'}, inplace=True)
self.append_date_weekday_column(
df, timestamp_column_name='creation_timestamp')
return df, top_id