/TikTok-API

An API that allows you to scrape information about TikTok posts/users

Primary LanguagePython

TikTok API

Status: (Working) 12-20-2021 3:00PM EST

Introduction:

The API is for scraping only and gives the ability to access the following data from tiktok:

Version:

V0.1.1 Beta for Python 3.x.x

Requirements:

Example:

Check example.py for an overall understanding of how to use this api

Usage:

Creating an api instance:

Example:

from TikTokAPI import TTAPI
api = TTAPI()

Receiving data from tiktok api:

Note: If the api response is {} then try a different input, if that does not work try updating the device parameters, if that also fails then the tiktok api might not be working or the script expired

.getPost(post_id)

Gets a post.

Example:

api.getPost(post_id)

#Returns: { author: {...}, aweme_id: '999', desc: 'description', music: {...}, statistics: {...}, video: {...}, ... }

.listComments(post_id, amount)

Lists comments for a post.

Example:

api.listComments(post_id, amount)

#Returns: [{ text: 'first!', user: {...} }, { text: 'second!', user: {...} }, ...]

.getUser(user_id)

Gets a user's profile.

Example:

api.getUser(user_id)

#Returns: { aweme_count: 1000, nickname: 'example', unique_id: 'musername', ... }

.listPosts(user_id)

Lists a user's posts.

Example:

api.listPosts(user_id)

#Returns: [{ author: {...}, aweme_id: '999', desc: 'description', music: {...}, statistics: {...}, video: {...} }, ...]

.listFollowers(user_id)

Lists the users that follow the specified user.

Example:

api.listFollowers(user_id)

#Returns: [{ unique_id: 'follower1' }, { unique_id: 'follower2' }, ...]

.listFollowing(user_id)

Lists the users that the specified user follows.

Example:

api.listFollowing(user_id)

#Returns: [{ unique_id: 'following1' }, { unique_id: 'following2' }, ...]

Extracting id's from url's:

.getPostId(url)

Extracts and returns the id of a post by using a tiktok url.

Example:

#Any post url should work

api.getPostId("https://www.tiktok.com/@tiktok/video/6839775130407750917")

#Returns: 6839775130407750917

.getUserId(url)

Extracts and returns the id of a user by using a tiktok url to their post or account.

Example:

#Any post/user url should work

api.getUserId("https://www.tiktok.com/@tiktok/video/6839775130407750917")

#Returns: 107955

Finding and setting device parameters:

Note: This step will be required in the future because the default parameters will eventually expire

.getDeviceParams(url)

Device parameters are required to be able to use the tiktok's api service, they are specific to your device and account, using your own parameters is a much more stable solution because you are the one who owns them.

This function returns a dictionary of needed parameters from a sniffed tiktok url, if the returned dictionary is blank then not all required parameters are present. You can use a mitm proxy to scan the url's the tiktok app gets/posts and look for keywords like tiktokv, api, passport, or aweme.

Example:

#Any tiktok api url's similar to this one should work

api.getDeviceParams("https://api-h2.tiktokv.com/aweme/v1/aweme/detail/?aweme_id=7026288424218807553&origin_type=web&request_source=0&os_api=25&device_type=ONEPLUS+A3010&ssmix=a&manifest_version_code=170804&dpi=240&uoo=0&carrier_region=US&region=US&carrier_region_v2=310&app_name=trill&version_name=17.8.4&timezone_offset=-18000&ts=1639622884&ab_version=17.8.4&cpu_support64=false&ac2=unknown&ac=wifi&app_type=normal&host_abi=armeabi-v7a&channel=apkpure&update_version_code=170804&_rticket=1639622884&device_platform=android&iid=7032045377013942018&build_number=17.8.4&locale=en&op_region=US&version_code=170804&timezone_name=America%2FNew_York&cdid=ddbd4b01-a3d7-4e69-8a31-7ff76e4469fc&openudid=9b842ece932f071b&device_id=7031670777339250182&sys_region=US&app_language=en&resolution=900*1563&language=en&device_brand=OnePlus&os_version=7.1.1&aid=1180")

#Returns: {'os_api': '25', 'device_type': 'ONEPLUS+A3010', 'app_name': 'trill', 'version_name': '17.8.4', 'channel': 'apkpure', 'device_platform': 'android', 'iid': '7032045377013942018', 'version_code': '170804', 'device_id': '7031670777339250182', 'os_version': '7.1.1', 'aid': '1180'}

.setDeviceParams(param_dict)

This function sets the device parameters of the tiktok api class to the dictionary you input. You can create a working device parameter dictionary using .getDeviceParams(url).

Example:

api.setDeviceParams({'os_api': '25', 'device_type': 'ONEPLUS+A3010', 'app_name': 'trill', 'version_name': '17.8.4', 'channel': 'apkpure', 'device_platform': 'android', 'iid': '7032045377013942018', 'version_code': '170804', 'device_id': '7031670777339250182', 'os_version': '7.1.1', 'aid': '1180'})

#Device parameters are now updated

Credits:

Parameter generation: https://www.codenong.com/cs109390694/

TikTok API methods: https://github.com/szdc/tiktok-api

Logs: