Two main functions, fetch_tiktok_video_data
and fetch_tiktok_comments_data
, which allow you to download video metadata and comments from TikTok using the official TikTok Research API.
- Fetch video metadata for a given TikTok account within a specified timeframe
- Fetch comments for a list of video IDs
- Save the fetched video metadata and comments data as CSV files
- Helper functions to retrieve video IDs from the fetched video data and convert the data to CSV format
Before using this project, make sure you have the following:
- Python 3.x installed
- Required Python packages installed (you can install them using
pip install -r requirements.txt
) - Access to the TikTok Research API (you need to obtain API credentials)
key
andsecret
from TikTok API stored as environment variables
-
Clone the repository or download the project files.
-
Install the required Python packages by running the following command:
pip install -r requirements.txt
-
Update the
accounts
list in the code with the TikTok account usernames you want to fetch data for. -
Modify the
timeframe
list to specify the start and end dates for each month you want to fetch data for. The dates should be in the format "YYYYMMDD". -
Run the script to fetch the video metadata and comments data:
key = os.environ['tiktok_key'] secret = os.environ['tiktok_secret'] access_token = generate_access_token(key, secret)["access_token"] # create start and end dates for each month in 2023 timeframe = [("20230101", "20230131"), ("20230201", "20230228")] accounts = ['nasa_0fficial', 'nytimes'] for account in accounts: for start_date, end_date in timeframe: fetch_tiktok_video_data(account, start_date, end_date, mode="username") video_ids = get_video_ids('TikTok_Data/video_data') for video_id in video_ids: fetch_tiktok_comments_data(video_id) comments_data_to_csv('TikTok_Data/comments_data/') video_data_to_csv('TikTok_Data/video_data/')
-
The fetched video and comments metadata will be saved as JSON files in the
TikTok_Data/video_data/
andTikTok_Data/comments_data/
directories, respectively. -
You can convert the fetched data to CSV format using the helper functions
comments_data_to_csv
andvideo_data_to_csv
.
This function fetches video metadata for a given TikTok account within a specified timeframe.
account
: The TikTok account username or user ID.start_date
: The start date of the timeframe in the format "YYYYMMDD".end_date
: The end date of the timeframe in the format "YYYYMMDD".mode
: The mode of theaccount
parameter. It can be either "username" (default) or "hashtag_name", depending on whether you want to collect data for a specific account or hashtag.filter_hashtags
: A list of hashtags to filter the fetched videos by (only whenmode
is "username"). The default is an empty list.return_data
: A boolean value indicating whether to return the fetched data as a dictionary. The default isFalse
.n_videos
: The number of videos to fetch. The default isNone
, which does not introduce any restrictions on the count.
This function fetches comments data for a given TikTok video ID.
video_id
: The ID of the TikTok video.return_data
: A boolean value indicating whether to return the fetched data as a dictionary. The default isFalse
.
get_video_ids(directory)
: Retrieves the video IDs from the fetched JSON files in the specified directory.comments_data_to_csv(directory)
: Converts the fetched comments data to CSV format and saves it in the specified directory.video_data_to_csv(directory)
: Converts the fetched video metadata to CSV format and saves it in the specified directory.
This project is licensed under the MIT License.