-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathscript.py
708 lines (574 loc) · 21.9 KB
/
script.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
import itertools
import port.api.props as props
from port.api.commands import CommandSystemDonate, CommandUIRender
import pandas as pd
import zipfile
import json
import datetime
from collections import defaultdict, namedtuple
from contextlib import suppress
##########################
# TikTok file processing #
##########################
filter_start = datetime.datetime(2021, 1, 1)
filter_end = datetime.datetime(2025, 1, 1)
datetime_format = "%Y-%m-%d %H:%M:%S"
def parse_datetime(value):
return datetime.datetime.strptime(value, datetime_format)
def get_in(data_dict, *key_path):
for k in key_path:
data_dict = data_dict.get(k, None)
if data_dict is None:
return None
return data_dict
def get_list(data_dict, *key_path):
result = get_in(data_dict, *key_path)
if result is None:
return []
return result
def get_dict(data_dict, *key_path):
result = get_in(data_dict, *key_path)
if result is None:
return {}
return result
def get_string(data_dict, *key_path):
result = get_in(data_dict, *key_path)
if result is None:
return ""
return result
def cast_number(data_dict, *key_path):
value = get_in(data_dict, *key_path)
if value is None or value == "None":
return 0
return value
def get_activity_video_browsing_list_data(data):
return get_list(data, "Activity", "Video Browsing History", "VideoList")
def get_comment_list_data(data):
return get_in(data, "Comment", "Comments", "CommentsList")
def get_date_filtered_items(items):
for item in items:
timestamp = parse_datetime(item["Date"])
if timestamp < filter_start or timestamp > filter_end:
continue
yield (timestamp, item)
def get_count_by_date_key(timestamps, key_func):
"""Returns a dict of the form (key, count)
The key is determined by the key_func, which takes a datetime object and
returns an object suitable for sorting and usage as a dictionary key.
The returned list is sorted by key.
"""
item_count = defaultdict(int)
for timestamp in timestamps:
item_count[key_func(timestamp)] += 1
return sorted(item_count.items())
def get_all_first(items):
return (i[0] for i in items)
def hourly_key(date):
return date.replace(minute=0, second=0, microsecond=0)
def daily_key(date):
return date.date()
def get_sessions(timestamps):
"""Returns a list of tuples of the form (start, end, duration)
The start and end are datetime objects, and the duration is a timedelta
object.
"""
timestamps = list(sorted(timestamps))
if len(timestamps) == 0:
return []
if len(timestamps) == 1:
return [(timestamps[0], timestamps[0], datetime.timedelta(0))]
sessions = []
start = timestamps[0]
end = timestamps[0]
for prev, cur in zip(timestamps, timestamps[1:]):
if cur - prev > datetime.timedelta(minutes=5):
sessions.append((start, end, end - start))
start = cur
end = cur
sessions.append((start, end, end - start))
return sessions
def load_tiktok_data(json_file):
data = json.load(json_file)
if not get_user_name(data):
raise IOError("Unsupported file type")
return data
def get_json_data_from_zip(zip_file):
with zipfile.ZipFile(zip_file, "r") as zip:
for name in zip.namelist():
if not name.endswith(".json"):
continue
with zip.open(name) as json_file:
with suppress(IOError, json.JSONDecodeError):
return [load_tiktok_data(json_file)]
return []
def get_json_data_from_file(file_):
# TikTok exports can be a single JSON file or a zipped JSON file
try:
with open(file_) as f:
return [load_tiktok_data(f)]
except (json.decoder.JSONDecodeError, UnicodeDecodeError):
return get_json_data_from_zip(file_)
def filtered_count(data, *key_path):
items = get_list(data, *key_path)
filtered_items = get_date_filtered_items(items)
return len(list(filtered_items))
def get_user_name(data):
return get_in(data, "Profile", "Profile Information", "ProfileMap", "userName")
def get_chat_history(data):
return get_dict(data, "Direct Messages", "Chat History", "ChatHistory")
def flatten_chat_history(history):
return itertools.chain(*history.values())
def filter_by_key(items, key, value):
return filter(lambda item: item[key] == value, items)
def exclude_by_key(items, key, value):
"""
Return a filtered list where items that match key & value are excluded.
"""
return filter(lambda item: item[key] != value, items)
def map_to_timeslot(series):
return series.map(lambda hour: f"{hour}-{hour+1}")
def extract_summary_data(data):
user_name = get_user_name(data)
chat_history = get_chat_history(data)
flattened = flatten_chat_history(chat_history)
direct_messages_in_period = list(get_date_filtered_items(flattened))
sent_count = len(
list(
filter(lambda item: item[1]["From"] == user_name, direct_messages_in_period)
)
)
received_count = len(
list(
filter(
lambda item: item[1]["From"] != user_name,
direct_messages_in_period,
)
)
)
summary_data = {
"Description": [
"Followers",
"Following",
"Likes received",
"Videos posted",
"Likes given",
"Comments posted",
"Messages sent",
"Messages received",
"Videos watched",
],
"Number": [
filtered_count(data, "Activity", "Follower List", "FansList"),
filtered_count(data, "Activity", "Following List", "Following"),
cast_number(
data,
"Profile",
"Profile Information",
"ProfileMap",
"likesReceived",
),
filtered_count(data, "Video", "Videos", "VideoList"),
filtered_count(data, "Activity", "Like List", "ItemFavoriteList"),
filtered_count(data, "Comment", "Comments", "CommentsList"),
sent_count,
received_count,
filtered_count(data, "Activity", "Video Browsing History", "VideoList"),
],
}
return ExtractionResult(
"tiktok_summary",
props.Translatable(
{"en": "Summary information", "nl": "Samenvatting gegevens"}
),
pd.DataFrame(summary_data),
props.Translatable(
{
"en": "Here we can now add a description per table to better help the user understand what the data is about. The description should be short. Say about one to three sentences",
"nl": "Hier kunnen we nu een beschrijving per tabel toevoegen om de gebruiker beter te helpen begrijpen waar de gegevens over gaan. De beschrijving moet kort zijn. Zeg ongeveer één tot drie zinnen",
}
),
None,
)
def extract_videos_viewed(data):
videos = get_all_first(
get_date_filtered_items(get_activity_video_browsing_list_data(data))
)
video_counts = get_count_by_date_key(videos, hourly_key)
if not video_counts:
return
df = pd.DataFrame(video_counts, columns=["Date", "Videos"])
df["Timeslot"] = map_to_timeslot(df["Date"].dt.hour)
df["Date"] = df["Date"].dt.strftime("%Y-%m-%d %H:00:00")
df = df.reindex(columns=["Date", "Timeslot", "Videos"])
visualizations = [
props.PropsUIChartVisualization(
title=props.Translatable(
{
"en": "Average number of videos watched per hour of the day",
"nl": "Gemiddeld aantal videos bekeken per uur van de dag",
}
),
type="bar",
group=props.PropsUIChartGroup(
column="Date", label="Hour of the day", dateFormat="hour_cycle"
),
values=[
props.PropsUIChartValue(
column="Videos",
label="Average nr. of videos",
aggregate="mean",
addZeroes=True,
)
],
)
]
return ExtractionResult(
"tiktok_videos_viewed",
props.Translatable({"en": "Video views", "nl": "Videos gezien"}),
df,
None,
visualizations,
)
def extract_video_posts(data):
video_list = get_in(data, "Video", "Videos", "VideoList")
if video_list is None:
return
videos = get_date_filtered_items(video_list)
post_stats = defaultdict(lambda: defaultdict(int))
for date, video in videos:
hourly_stats = post_stats[hourly_key(date)]
hourly_stats["Videos"] += 1
hourly_stats["Likes received"] += int(video["Likes"])
df = pd.DataFrame(post_stats).transpose()
df["Date"] = df.index.strftime("%Y-%m-%d")
df["Timeslot"] = map_to_timeslot(df.index.hour)
df = df.reset_index(drop=True)
df = df.reindex(columns=["Date", "Timeslot", "Videos", "Likes received"])
return ExtractionResult(
"tiktok_posts",
props.Translatable({"en": "Video posts", "nl": "Video posts"}),
df,
None,
None,
)
def extract_comments_and_likes(data):
comments = get_all_first(
get_date_filtered_items(get_list(data, "Comment", "Comments", "CommentsList"))
)
comment_counts = get_count_by_date_key(comments, hourly_key)
likes_given = get_all_first(
get_date_filtered_items(
get_list(data, "Activity", "Like List", "ItemFavoriteList")
)
)
likes_given_counts = get_count_by_date_key(likes_given, hourly_key)
if not likes_given_counts:
return
df1 = pd.DataFrame(comment_counts, columns=["Date", "Comment posts"]).set_index(
"Date"
)
df2 = pd.DataFrame(likes_given_counts, columns=["Date", "Likes given"]).set_index(
"Date"
)
df = pd.merge(df1, df2, left_on="Date", right_on="Date", how="outer").sort_index()
df["Timeslot"] = map_to_timeslot(df.index.hour)
df["Date"] = df.index.strftime("%Y-%m-%d %H:00:00")
df = (
df.reindex(columns=["Date", "Timeslot", "Comment posts", "Likes given"])
.reset_index(drop=True)
.fillna(0)
)
df["Comment posts"] = df["Comment posts"].astype(int)
df["Likes given"] = df["Likes given"].astype(int)
visualizations = [
props.PropsUIChartVisualization(
title=props.Translatable(
{
"en": "Average number of comments and likes for every hour of the day",
"nl": "Gemiddeld aantal comments en likes per uur van de dag",
}
),
type="bar",
group=props.PropsUIChartGroup(
column="Date", label="Hour of the day", dateFormat="hour_cycle"
),
values=[
props.PropsUIChartValue(
label="Average nr. of comments",
column="Comment posts",
aggregate="mean",
addZeroes=True,
),
props.PropsUIChartValue(
label="Average nr. of posts",
column="Likes given",
aggregate="mean",
addZeroes=True,
),
],
)
]
return ExtractionResult(
"tiktok_comments_and_likes",
props.Translatable({"en": "Comments and likes", "nl": "Comments en likes"}),
df,
None,
visualizations,
)
def extract_session_info(data):
session_paths = [
("Video", "Videos", "VideoList"),
("Activity", "Video Browsing History", "VideoList"),
("Comment", "Comments", "CommentsList"),
]
item_lists = [get_list(data, *path) for path in session_paths]
dates = get_all_first(get_date_filtered_items(itertools.chain(*item_lists)))
sessions = get_sessions(dates)
df = pd.DataFrame(sessions, columns=["Start", "End", "Duration"])
df["Start"] = df["Start"].dt.strftime("%Y-%m-%d %H:%M")
df["Duration (in minutes)"] = (df["Duration"].dt.total_seconds() / 60).round(2)
df = df.drop("End", axis=1)
df = df.drop("Duration", axis=1)
visualizations = [
props.PropsUIChartVisualization(
title=props.Translatable(
{
"en": "Number of minutes spent on TikTok",
"nl": "Aantal minuten besteed aan TikTok",
}
),
type="line",
group=props.PropsUIChartGroup(
column="Start", label="Date", dateFormat="auto"
),
values=[
props.PropsUIChartValue(
label="Nr. of minutes",
column="Duration (in minutes)",
aggregate="sum",
addZeroes=True,
)
],
)
]
return ExtractionResult(
"tiktok_session_info",
props.Translatable({"en": "Session information", "nl": "Sessie informatie"}),
df,
None,
visualizations,
)
def extract_direct_messages(data):
history = get_in(data, "Direct Messages", "Chat History", "ChatHistory")
counter = itertools.count(start=1)
anon_ids = defaultdict(lambda: next(counter))
# Ensure 1 is the ID of the donating user
anon_ids[get_user_name(data)]
table = {"Anonymous ID": [], "Sent": []}
for item in flatten_chat_history(history):
table["Anonymous ID"].append(anon_ids[item["From"]])
table["Sent"].append(parse_datetime(item["Date"]).strftime("%Y-%m-%d %H:%M"))
return ExtractionResult(
"tiktok_direct_messages",
props.Translatable(
{"en": "Direct Message Activity", "nl": "Berichten activiteit"}
),
pd.DataFrame(table),
None,
None,
)
def extract_comment_activity(data):
comments = get_in(data, "Comment", "Comments", "CommentsList")
if comments is None:
return
timestamps = [
parse_datetime(item["Date"]).strftime("%Y-%m-%d %H:%M") for item in comments
]
return ExtractionResult(
"tiktok_comment_activity",
props.Translatable({"en": "Comment Activity", "nl": "Commentaar activiteit"}),
pd.DataFrame({"Posted on": timestamps}),
None,
None,
)
def extract_videos_liked(data):
favorite_videos = get_in(data, "Activity", "Favorite Videos", "FavoriteVideoList")
if favorite_videos is None:
return
table = {"Liked": [], "Link": []}
for item in favorite_videos:
table["Liked"].append(parse_datetime(item["Date"]).strftime("%Y-%m-%d %H:%M"))
table["Link"].append(item["Link"])
return ExtractionResult(
"tiktok_videos_liked",
props.Translatable({"en": "Videos liked", "nl": "Gelikete videos"}),
pd.DataFrame(table),
None,
None
)
def extract_tiktok_data(zip_file):
extractors = [
extract_summary_data,
extract_video_posts,
extract_comments_and_likes,
extract_videos_viewed,
extract_session_info,
extract_direct_messages,
extract_comment_activity,
extract_videos_liked,
]
for data in get_json_data_from_file(zip_file):
return [
table
for table in (extractor(data) for extractor in extractors)
if table is not None
]
######################
# Data donation flow #
######################
ExtractionResult = namedtuple(
"ExtractionResult", ["id", "title", "data_frame", "description", "visualizations"]
)
class InvalidFileError(Exception):
"""Indicates that the file does not match expectations."""
class SkipToNextStep(Exception):
pass
class DataDonationProcessor:
def __init__(self, platform, mime_types, extractor, session_id):
self.platform = platform
self.mime_types = mime_types
self.extractor = extractor
self.session_id = session_id
self.progress = 0
self.meta_data = []
def process(self):
with suppress(SkipToNextStep):
while True:
file_result = yield from self.prompt_file()
self.log(f"extracting file")
try:
extraction_result = self.extract_data(file_result.value)
except IOError as e:
self.log(f"prompt confirmation to retry file selection")
yield from self.prompt_retry()
return
except InvalidFileError:
self.log(f"invalid file detected, prompting for retry")
if (yield from self.prompt_retry()):
continue
else:
return
else:
if extraction_result is None:
try_again = yield from self.prompt_retry()
if try_again:
continue
else:
return
self.log(f"extraction successful, go to consent form")
yield from self.prompt_consent(extraction_result)
def prompt_retry(self):
retry_result = yield render_donation_page(
self.platform, retry_confirmation(self.platform), self.progress
)
return retry_result.__type__ == "PayloadTrue"
def prompt_file(self):
description = props.Translatable(
{
"en": f"Please follow the download instructions and choose the file that you stored on your device. Click “Skip” at the right bottom, if you do not have a {self.platform} file. ",
"nl": f"Volg de download instructies en kies het bestand dat u opgeslagen heeft op uw apparaat. Als u geen {self.platform} bestand heeft klik dan op “Overslaan” rechts onder.",
}
)
prompt_file = props.PropsUIPromptFileInput(description, self.mime_types)
file_result = yield render_donation_page(
self.platform, prompt_file, self.progress
)
if file_result.__type__ != "PayloadString":
self.log(f"skip to next step")
raise SkipToNextStep()
return file_result
def log(self, message):
self.meta_data.append(("debug", f"{self.platform}: {message}"))
def extract_data(self, file):
return self.extractor(file)
def prompt_consent(self, data):
log_title = props.Translatable({"en": "Log messages", "nl": "Log berichten"})
tables = [
props.PropsUIPromptConsentFormTable(
table.id,
table.title,
table.data_frame,
table.description,
table.visualizations,
)
for table in data
]
meta_frame = pd.DataFrame(self.meta_data, columns=["type", "message"])
meta_table = props.PropsUIPromptConsentFormTable(
"log_messages", log_title, meta_frame
)
self.log(f"prompt consent")
consent_result = yield render_donation_page(
self.platform,
props.PropsUIPromptConsentForm(tables, [meta_table]),
self.progress,
)
if consent_result.__type__ == "PayloadJSON":
self.log(f"donate consent data")
yield donate(f"{self.sessionId}-{self.platform}", consent_result.value)
class DataDonation:
def __init__(self, platform, mime_types, extractor):
self.platform = platform
self.mime_types = mime_types
self.extractor = extractor
def __call__(self, session_id):
processor = DataDonationProcessor(
self.platform, self.mime_types, self.extractor, session_id
)
yield from processor.process()
tik_tok_data_donation = DataDonation(
"TikTok", "application/zip, text/plain, application/json", extract_tiktok_data
)
def process(session_id):
progress = 0
yield donate(f"{session_id}-tracking", '[{ "message": "user entered script" }]')
yield from tik_tok_data_donation(session_id)
yield render_end_page()
def render_end_page():
page = props.PropsUIPageEnd()
return CommandUIRender(page)
def render_donation_page(platform, body, progress):
header = props.PropsUIHeader(props.Translatable({"en": platform, "nl": platform}))
footer = props.PropsUIFooter(progress)
page = props.PropsUIPageDonation(platform, header, body, footer)
return CommandUIRender(page)
def retry_confirmation(platform):
text = props.Translatable(
{
"en": "Unfortunately, we cannot process your data. Please make sure that you selected JSON as a file format when downloading your data from TikTok.",
"nl": "Helaas kunnen we uw gegevens niet verwerken. Zorg ervoor dat u JSON heeft geselecteerd als bestandsformaat bij het downloaden van uw gegevens van TikTok.",
}
)
ok = props.Translatable({"en": "Try again", "nl": "Probeer opnieuw"})
cancel = props.Translatable({"en": "Continue", "nl": "Verder"})
return props.PropsUIPromptConfirm(text, ok, cancel)
def prompt_consent(id, data, meta_data):
table_title = props.Translatable(
{"en": "Zip file contents", "nl": "Inhoud zip bestand"}
)
log_title = props.Translatable({"en": "Log messages", "nl": "Log berichten"})
data_frame = pd.DataFrame(data, columns=["filename", "compressed size", "size"])
table = props.PropsUIPromptConsentFormTable("zip_content", table_title, data_frame)
meta_frame = pd.DataFrame(meta_data, columns=["type", "message"])
meta_table = props.PropsUIPromptConsentFormTable(
"log_messages", log_title, meta_frame
)
return props.PropsUIPromptConsentForm([table], [meta_table])
def donate(key, json_string):
return CommandSystemDonate(key, json_string)
if __name__ == "__main__":
import sys
if len(sys.argv) > 1:
print(extract_tiktok_data(sys.argv[1]))
else:
print("please provide a zip file as argument")