update system to use find Highlights *not tested
This commit is contained in:
1 parent
38e0cc6764
commit
7c96a5131b
8 files changed
+211
-86
No files matched your search
+64
-62
@@ -1,72 +1,74 @@
|
||||
#!/usr/bin/env python3
|
||||
from tiktok_uploader.upload import upload_video
|
||||
from pathlib import Path
|
||||
import json
|
||||
import time
|
||||
from playwright.sync_api import sync_playwright
|
||||
import requests
|
||||
from pathlib import Path
|
||||
|
||||
COOKIES_FILE = "cookies.txt"
|
||||
SECRETS_FILE = Path(__file__).parent.parent / "tiktok_secrets.json"
|
||||
# if your file is at /home/richard/python_scripts/tiktok_secrets.json
|
||||
# use: Path("/home/richard/python_scripts/tiktok_secrets.json")
|
||||
|
||||
def get_last_video_link(username):
|
||||
"""Grab newest video link from profile"""
|
||||
url = f"https://www.tiktok.com/@{username}"
|
||||
with sync_playwright() as p:
|
||||
browser = p.chromium.launch(headless=True)
|
||||
context = browser.new_context(storage_state=COOKIES_FILE) if Path(COOKIES_FILE).exists() else browser.new_context()
|
||||
page = context.new_page()
|
||||
page.goto(url, timeout=60000)
|
||||
page.wait_for_selector('[data-e2e="user-post-item"] a', timeout=15000)
|
||||
first_video = page.query_selector('[data-e2e="user-post-item"] a')
|
||||
link = first_video.get_attribute('href') if first_video else None
|
||||
browser.close()
|
||||
return link
|
||||
def load_secrets():
|
||||
with open(SECRETS_FILE) as f:
|
||||
secrets = json.load(f)
|
||||
return secrets["client_key"], secrets["client_secret"], secrets["access_token"], secrets.get("refresh_token", "")
|
||||
|
||||
def upload_to_tiktok(video_path, description, hashtags=[], username="YOUR_USERNAME_HERE"):
|
||||
tags = " ".join([f"#{t}" for t in hashtags])
|
||||
full_desc = f"{description}\n\n{tags}"
|
||||
API_BASE = "https://open.tiktokapis.com/v2"
|
||||
|
||||
def upload_to_tiktok_api(video_path: str, title: str, privacy="PUBLIC_TO_EVERYONE"):
|
||||
client_key, client_secret, access_token, refresh_token = load_secrets()
|
||||
|
||||
failed = upload_video(
|
||||
filename=str(video_path),
|
||||
description=full_desc,
|
||||
cookies=COOKIES_FILE,
|
||||
headless=True,
|
||||
browser="chrome"
|
||||
)
|
||||
video_path = Path(video_path)
|
||||
file_size = video_path.stat().st_size
|
||||
|
||||
if len(failed) != 0:
|
||||
print(f"❌ Failed {video_path}: {failed}")
|
||||
return None
|
||||
headers = {
|
||||
"Authorization": f"Bearer {access_token}",
|
||||
"Content-Type": "application/json; charset=UTF-8"
|
||||
}
|
||||
|
||||
print(f"✅ Uploaded {video_path} - fetching link...")
|
||||
time.sleep(8) # give TikTok time to process
|
||||
# 1. INIT
|
||||
init_payload = {
|
||||
"source_info": {
|
||||
"source": "FILE_UPLOAD",
|
||||
"video_size": file_size,
|
||||
"chunk_size": file_size,
|
||||
"total_chunk_count": 1
|
||||
}
|
||||
}
|
||||
r = requests.post(f"{API_BASE}/post/publish/video/init/", headers=headers, json=init_payload)
|
||||
r.raise_for_status()
|
||||
data = r.json()
|
||||
if data.get("error", {}).get("code") != "ok":
|
||||
raise Exception(f"Init failed: {data}")
|
||||
|
||||
# Try 3 times, TikTok takes ~10-30s to show new video
|
||||
for i in range(3):
|
||||
link = get_last_video_link(username)
|
||||
if link:
|
||||
print(f"🔗 {link}")
|
||||
return link
|
||||
print(f" Waiting for TikTok to publish... retry {i+1}/3")
|
||||
time.sleep(10)
|
||||
|
||||
return f"https://www.tiktok.com/@{username} (uploaded, but link not ready yet - check profile)"
|
||||
publish_id = data["data"]["publish_id"]
|
||||
upload_url = data["data"]["upload_url"]
|
||||
|
||||
# 2. UPLOAD
|
||||
with open(video_path, "rb") as f:
|
||||
put_headers = {
|
||||
"Content-Range": f"bytes 0-{file_size-1}/{file_size}",
|
||||
"Content-Type": "video/mp4"
|
||||
}
|
||||
requests.put(upload_url, headers=put_headers, data=f).raise_for_status()
|
||||
|
||||
# 3. PUBLISH
|
||||
publish_payload = {
|
||||
"publish_id": publish_id,
|
||||
"post_info": {
|
||||
"title": title[:2200],
|
||||
"privacy_level": privacy,
|
||||
"disable_duet": False,
|
||||
"disable_comment": False,
|
||||
"disable_stitch": False,
|
||||
"video_cover_timestamp_ms": 1000
|
||||
}
|
||||
}
|
||||
pr = requests.post(f"{API_BASE}/post/publish/", headers=headers, json=publish_payload)
|
||||
pr.raise_for_status()
|
||||
return pr.json()
|
||||
|
||||
if __name__ == "__main__":
|
||||
USERNAME = "your_tiktok_username" # <-- CHANGE THIS, no @
|
||||
|
||||
with open("highlights.json") as f:
|
||||
data = json.load(f)
|
||||
|
||||
for h in data['highlights'][:3]:
|
||||
video_file = f"highlights/highlight_{h['id']}_{h['start_timestamp'].replace(':','-')}.mp4"
|
||||
if not Path(video_file).exists():
|
||||
continue
|
||||
|
||||
desc = h['transcript'][:150]
|
||||
link = upload_to_tiktok(
|
||||
video_file,
|
||||
description=desc,
|
||||
hashtags=["fyp", "clutch", "highlight", "pog"],
|
||||
username=USERNAME
|
||||
)
|
||||
print(f"LINK: {link}\n")
|
||||
result = upload_to_tiktok_api(
|
||||
"/home/richard/python_scripts/download/clips/ProductiveConfidentWombatThunBeast/ProductiveConfidentWombatThunBeast_gaussian_9_16.mp4",
|
||||
"Testing upload #Twitch #TeamPGP #Stellaris"
|
||||
)
|
||||
print(result)
|
||||
Reference in new issue
Block a user