Closed SaadatAliKlasra closed 3 years ago
This is available only on mobile api
This is available only on mobile api
are there any mobile API wrapper ?
This is available only on mobile api
are there any mobile API wrapper ?
there is one here on github but it's not working anymore, it's so old.
I managed to make a class that can download trending tiktok videos without the watermark
import asyncio
import aiohttp
from TikTokApi import TikTokApi
from yt_dlp import YoutubeDL
import os
from datetime import datetime
class TikTokDownloader:
def __init__(self, ms_token):
self.ms_token = ms_token
# Ensure the download directory with today's date exists
self.download_directory = datetime.now().strftime("%Y-%m-%d")
os.makedirs(self.download_directory, exist_ok=True)
self.ydl_opts = {
"format": "best",
"outtmpl": os.path.join(self.download_directory, "%(title)s.%(ext)s")
}
async def get_video_url_list_and_desc(self, video_id):
url = f"https://api16-normal-c-useast2a.tiktokv.com/aweme/v1/feed/?aweme_id={video_id}"
async with aiohttp.ClientSession() as session:
async with session.get(url) as response:
if response.status == 200:
data = await response.json()
url_list = data["aweme_list"][0]["video"]["play_addr"]["url_list"]
description = data["aweme_list"][0]["desc"]
return url_list, description
else:
print("Failed to retrieve video URL list")
return [], ""
async def download_video(self, url_list, video_title):
# Pass metadata options to YoutubeDL
for url in url_list:
try:
with YoutubeDL(self.ydl_opts) as ydl:
ydl.download([url])
print(f"Download successful for {video_title}")
break
except Exception as e:
print(f"Failed to download {video_title} from {url}, trying next URL.")
continue
async def trending_videos(self):
async with TikTokApi() as api:
await api.create_sessions(
ms_tokens=[self.ms_token],
num_sessions=1,
sleep_after=3,
headless=False,
suppress_resource_load_types=["image", "media", "font", "stylesheet"],
)
async for video in api.trending.videos():
url_list, description = await self.get_video_url_list_and_desc(video.id)
video_title = f"{video.author.username}_{video.id}"
if url_list:
await self.download_video(url_list, video_title)
if __name__ == "__main__":
ms_token = "" # get your own ms_token from your cookies on tiktok.com
downloader = TikTokDownloader(ms_token)
asyncio.run(downloader.trending_videos())
Details Please add download without watermark option.