tiktok 1.22.2023
🧩 Syntax:
import json
import os
import requests
import time
def download_video(url, name):
try:
response = requests.get(url, stream=True)
filename = url.split("/")[-1]
with open(f"C:\\Users\\admin\\Videos\\tik\\{name}-{filename}", "wb") as file:
for chunk in response.iter_content(chunk_size=1024):
if chunk:
file.write(chunk)
print(f"Video {name}-{filename} downloaded successfully from {url}!")
except requests.exceptions.RequestException as e:
print(f"Error: {e}")
return response
# Read the JSON file
try:
with open("C:\\Users\admin\Documents\user_data.json", "r") as file:
data = json.load(file)
except json.decoder.JSONDecodeError as e:
print(f"Error: {e}")
# Time limit between downloads in seconds
time_limit = 1
# Create the folder if it doesn't exist
if not os.path.exists("C:\\Users\\admin\\Videos\\tik"):
os.makedirs("C:\\Users\\admin\\Videos\\tik")
# Iterate through the list of videos
for video in data["videos"][:300]:
url = video["url"]
name = video["name"]
response = download_video(url, name)
if 'Retry-After' in response.headers:
time_limit = int(response.headers['Retry-After'])
else:
time_limit = 1
time.sleep(time_limit)