Tiếng Việt
Tích Hợp Với Python
Hướng dẫn sử dụng Python để tự động hóa xuất bản video và cào dữ liệu từ TikTok thông qua thư viện requests hoặc httpx.
1. Cài đặt thư viện
bash
pip install requests2. Kịch bản Tự động Đăng Video (publish_video.py)
python
import os
import requests
API_BASE_URL = "https://api.stackvid.dev/api"
API_KEY = os.getenv("STACKVID_API_KEY", "tk_live_YOUR_KEY")
headers = {
"X-API-Key": API_KEY,
"Content-Type": "application/json"
}
def upload_and_publish(video_path: str, caption: str, channel_name: str = None):
# 1. Kiểm tra kích thước file
file_size = os.path.getsize(video_path)
print(f"📁 Tệp video: {video_path} ({file_size} bytes)")
# 2. Yêu cầu cấp Presigned URL
print("⚡ 1. Yêu cầu cấp Presigned Upload URL...")
presigned_payload = {
"fileType": "VIDEO",
"contentType": "video/mp4",
"fileSizeBytes": file_size
}
r = requests.post(f"{API_BASE_URL}/uploads/presigned-url", json=presigned_payload, headers=headers)
r.raise_for_status()
presigned_data = r.json()
upload_url = presigned_data["uploadUrl"]
public_url = presigned_data["publicUrl"]
print("✅ Đã nhận upload URL!")
# 3. Tải trực tiếp file binary lên uploadUrl
print("🚀 2. Tải video lên hệ thống...")
with open(video_path, "rb") as f:
put_headers = {"Content-Type": "video/mp4"}
put_res = requests.put(upload_url, data=f, headers=put_headers)
put_res.raise_for_status()
print(f"✅ Tải lên hoàn tất! Public URL: {public_url}")
# 4. Xuất bản lên TikTok
print("📱 3. Gửi lệnh xuất bản lên TikTok...")
publish_payload = {
"videoUrl": public_url,
"caption": caption,
"targetChannel": channel_name,
"privacyLevel": 0,
"allowComment": True
}
pub_res = requests.post(f"{API_BASE_URL}/tiktok/upload", json=publish_payload, headers=headers)
pub_res.raise_for_status()
result = pub_res.json()
print("🎉 ĐĂNG BÀI THÀNH CÔNG!")
print(f"Link video: {result.get('shareUrl')}")
print(f"Số dư ví: {result.get('remainingBalance')} credits")
return result
if __name__ == "__main__":
upload_and_publish("promo.mp4", "Review sản phẩm mới nhất cực hot! #review #trending")3. Kịch bản Cào Video Creator (crawl_creator.py)
python
import requests
def crawl_creator_videos(username: str, count: int = 35):
url = f"{API_BASE_URL}/tiktok/user/posts"
params = {"username": username, "count": count}
res = requests.get(url, headers=headers, params=params)
res.raise_for_status()
data = res.json()
print(f"📊 Thu thập thành công {len(data.get('itemList', []))} video của @{username}:")
for item in data.get("itemList", []):
video = item.get("video", {})
stats = item.get("stats", {})
print(f"• ID: {item.get('id')} | Views: {stats.get('playCount'):,} | Likes: {stats.get('diggCount'):,}")
print(f" Link không watermark: {video.get('playAddr')}")
if __name__ == "__main__":
crawl_creator_videos("lamine.yamal", count=10)