Skip to main content

Unofficial TikTok API Wrapped in Python

Project description

Tikfly API

Unofficial TikTok API Wrapped in Python

GitHub Repo stars GitHub forks Vistors License

Available on PyPi

Installation

pip install tikfly

Usage

Get TikTok User Info

import asyncio
from tikfly import TikflyApi

API_KEY = 'YOUR_API_KEY' # How to get your api key -> https://docs.tikfly.io/getting-started/quickstart
USERNAME = 'taylorswift' # Change to the user you want to get data from

async def get_tiktok_user_info():
  tikfly = TikflyApi(x_rapidapi_key=API_KEY)

  res_user_info = await tikfly.get_user_info(unique_id=USERNAME)

  print(res_user_info.userInfo)

asyncio.run(get_tiktok_user_info())

Get Tiktok User Videos

import asyncio
from tikfly import TikflyApi
from tikfly.schemas.UserPostSchema import VideoItem

API_KEY = 'YOUR_API_KEY' # How to get your api key -> https://docs.tikfly.io/getting-started/quickstart
USERNAME = 'taylorswift' # Change to the user you want to get data from
NUM = 50 # Max number of videos to fetch

async def get_tiktok_user_videos():
  tikfly = TikflyApi(x_rapidapi_key=API_KEY)

  print(f'Fetching {NUM} videos of user: {USERNAME}')
  res_user_info = await tikfly.get_user_info(USERNAME)

  user_sec_uid = res_user_info.userInfo.user.secUid
  print('User secUid:', user_sec_uid)

  results: list[VideoItem] = []
  cursor = 0
  count = 15
  has_more = True

  while has_more and len(results) < NUM:
    print(f'Current cursor: {cursor}')
    res_user_posts = await tikfly.get_user_posts(
      user_sec_uid,
      count=count,
      cursor=cursor
    )
    
    itemList = getattr(res_user_posts, 'itemList', [])

    if not itemList:
      break

    results.extend(itemList)

    remaining = NUM - len(results)
    results.extend(itemList[:remaining])

    has_more = res_user_posts.hasMore
    cursor = res_user_posts.cursor

  for video in results:
    print(f'- {video.id}: {video.desc}')

asyncio.run(get_tiktok_user_videos())

Download Tiktok Videos (Without Watermark)

import ssl
import asyncio
import certifi
import aiohttp
from tikfly import TikflyApi

API_KEY = 'YOUR_API_KEY' # How to get your api key -> https://docs.tikfly.io/getting-started/quickstart
VIDEO_URLS = [
  'https://www.tiktok.com/@taylorswift/video/7558098574555254046'
]

async def download_video(url: str, save_path: str):
  CHUNK_SIZE = 1024 * 1024
  ssl_context = ssl.create_default_context(cafile=certifi.where())
  async with aiohttp.ClientSession() as session:
    async with session.get(url, ssl=ssl_context) as resp:
      resp.raise_for_status()

      with open(save_path, 'wb') as f:
        async for chunk in resp.content.iter_chunked(CHUNK_SIZE):
            f.write(chunk)

  print(f'Saved video to: {save_path}')

async def download_tiktok_videos():
  tikfly = TikflyApi(x_rapidapi_key=API_KEY)

  for url in VIDEO_URLS:
    res = await tikfly.download_video(url)
    download_url = res.play
    print(f'Download URL: {download_url}')

    if download_url:
      video_id = url.rstrip('/').split('/')[-1]
      await download_video(download_url, f'{video_id}.mp4')

asyncio.run(download_tiktok_videos())

Examples

Go to the /tests and /examples folder to view more snippet code and example data.

Tutorials

Contributing

Contributions are welcome! If you find this project helpful, please consider starring the repository on GitHub ⭐️

License

This project is licensed under the MIT License. See the LICENSE file for more details.

Disclaimer

This project is an independent, open-source tool and is not affiliated, endorsed, or sponsored by TikTok.

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

tikfly-1.1.1.tar.gz (15.5 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

tikfly-1.1.1-py3-none-any.whl (23.0 kB view details)

Uploaded Python 3

File details

Details for the file tikfly-1.1.1.tar.gz.

File metadata

  • Download URL: tikfly-1.1.1.tar.gz
  • Upload date:
  • Size: 15.5 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.13.7

File hashes

Hashes for tikfly-1.1.1.tar.gz
Algorithm Hash digest
SHA256 b4b79b4e8d8d04de62a3ea0c3bc9a294b0eea54f0071700401534842a52b28e2
MD5 1030840566ea6a0ddee75eba12ba03ec
BLAKE2b-256 00e87621ed07e01fb2b549797ebf4a6e5d4142f9f635410825e07ae919b49693

See more details on using hashes here.

Provenance

The following attestation bundles were made for tikfly-1.1.1.tar.gz:

Publisher: publish.yml on tikfly/tikfly-api-py

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file tikfly-1.1.1-py3-none-any.whl.

File metadata

  • Download URL: tikfly-1.1.1-py3-none-any.whl
  • Upload date:
  • Size: 23.0 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.13.7

File hashes

Hashes for tikfly-1.1.1-py3-none-any.whl
Algorithm Hash digest
SHA256 83b9d0ed1ecdca8a70bd73b2a38113877686f6e8aa78f9ce1d449352affa7227
MD5 9705f450904f901866de3fef15de76c8
BLAKE2b-256 ea9b6ebc99249188453a8cbc6f00178a72d6551c5425c4660b232dc3e06e6d22

See more details on using hashes here.

Provenance

The following attestation bundles were made for tikfly-1.1.1-py3-none-any.whl:

Publisher: publish.yml on tikfly/tikfly-api-py

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page