-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathquickstart_requests.py
More file actions
59 lines (46 loc) · 2.25 KB
/
Copy pathquickstart_requests.py
File metadata and controls
59 lines (46 loc) · 2.25 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
"""TokPortal quickstart with plain `requests` (no SDK) - presigned upload flow.
create bundle -> POST /upload/video -> PUT file -> configure -> publish -> read account
TOKPORTAL_API_KEY=sk_... python quickstart_requests.py ./launch.mp4
NOTE: creating a bundle debits credits immediately. Not run against a sandbox.
"""
import datetime as dt
import os
import sys
import requests
BASE = os.environ.get("TOKPORTAL_BASE_URL", "https://app.tokportal.com/api/ext")
KEY = os.environ["TOKPORTAL_API_KEY"]
video_file = sys.argv[1] if len(sys.argv) > 1 else "./launch.mp4"
publish_date = (dt.date.today() + dt.timedelta(days=7)).isoformat()
session = requests.Session()
session.headers["X-API-Key"] = KEY
def api(method, path, **kwargs):
response = session.request(method, f"{BASE}{path}", timeout=60, **kwargs)
if not response.ok:
raise SystemExit(f"{method} {path} -> {response.status_code} {response.text} "
f"(request id {response.headers.get('X-TokPortal-Request-ID')})")
return response.json()
bundle = api("POST", "/bundles", json={
"bundle_type": "account_and_videos", "platform": "tiktok", "country": "US",
"title": "US launch", "videos_quantity": 1,
})
bundle_id = bundle["data"]["id"]
presigned = api("POST", "/upload/video", json={
"filename": os.path.basename(video_file), "content_type": "video/mp4", "bundle_id": bundle_id,
})["data"]
with open(video_file, "rb") as fh:
put = requests.request(presigned.get("method", "PUT"), presigned["upload_url"],
data=fh, headers={"Content-Type": "video/mp4"}, timeout=600)
put.raise_for_status()
api("PUT", f"/bundles/{bundle_id}/account", json={
"username": "mybrand.us", "visible_name": "My Brand", "biography": "Official account",
})
api("PUT", f"/bundles/{bundle_id}/videos/1", json={
"video_type": "video", "video_url": presigned["public_url"],
"description": "Day 1 #launch", "target_publish_date": publish_date,
})
api("POST", f"/bundles/{bundle_id}/publish")
current = api("GET", f"/bundles/{bundle_id}")["data"]
print(current["status"], current.get("saved_account_id"))
if current.get("saved_account_id"):
account = api("GET", f"/accounts/{current['saved_account_id']}")["data"]
print(account["username"], account["profile_url"])