-
Notifications
You must be signed in to change notification settings - Fork 0
/
collect_LikedVideosByUser.py
31 lines (24 loc) · 1.29 KB
/
collect_LikedVideosByUser.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
from TikTokApi import TikTokApi
import pandas as pd
# collect videos liked by a given user (that is the official tiktok account) which defaults to 30 yet 100 is specified
username = "tiktok"
liked_videos = TikTokApi().user_liked_by_username(username, count=100)
def simple_dict(tiktok_dict):
to_return = {}
to_return["user_name"] = tiktok_dict["author"]["uniqueId"]
to_return["user_id"] = tiktok_dict["author"]["id"]
to_return["video_id"] = tiktok_dict["id"]
to_return["video_desc"] = tiktok_dict["desc"]
to_return["video_time"] = tiktok_dict["createTime"]
to_return["video_length"] = tiktok_dict["video"]["duration"]
to_return["video_link"] = "https://www.tiktok.com/@{}/video/{}?lang=en".format(to_return["user_name"], to_return["video_id"])
to_return["n_likes"] = tiktok_dict["stats"]["diggCount"]
to_return["n_shares"] = tiktok_dict["stats"]["shareCount"]
to_return["n_comments"] = tiktok_dict["stats"]["commentCount"]
to_return["n_plays"] = tiktok_dict["stats"]["playCount"]
return to_return
# Then, we can go from the API-outputted "liked_videos" list to a nice, clean table
liked_videos = [simple_dict(v) for v in liked_videos]
liked_videos_df = pd.DataFrame(liked_videos)
liked_videos_df.to_csv('{}_collected_liked_videos.csv'.format(username), index=False)
print("Success!")