Most subscribers use OpenShorts on one day and cancel. Autopilot makes the
value recur: once switched on, every new upload on the user's YouTube channel
(listed through the existing Upload-Post connection, no scraping) is sent
through the normal /api/process pipeline, the user gets an email when the
clips are ready, and with autopublish on the best clips are scheduled on
their socials, one a day.
- cloud/autopilot.py: poller, in-process job submission as the user,
completion hook, autopublish, and /api/autopilot{,/videos,/run}
- guard rails: only videos published after switching on, max 3 days old,
1 automatic job per user per day, first N minutes per video, Shorts skipped,
paid plans only, dedupe by (user, video) across the deploy handover
- dashboard: new Autopilot tab (sidebar + mobile menu, #app?tab=autopilot)
- connect page can return to the Autopilot tab
- analytics: ClipsDelivered/JobFailed now report source=url and the plan
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
112 lines
4.2 KiB
Python
112 lines
4.2 KiB
Python
"""Upload-Post white-label profiles for managed users.
|
|
|
|
Each OpenShorts user maps to one Upload-Post profile (a container for their
|
|
connected socials). We create it lazily and generate branded connection links so
|
|
the user connects TikTok/IG/YouTube on a page that looks like OpenShorts.
|
|
|
|
Docs: https://docs.upload-post.com/guides/user-profile-integration
|
|
"""
|
|
import httpx
|
|
from fastapi import APIRouter, Request, HTTPException
|
|
|
|
from .config import settings
|
|
from . import database
|
|
from .models import UploadPostProfile
|
|
|
|
router = APIRouter()
|
|
|
|
API_BASE = "https://api.upload-post.com/api"
|
|
CONNECT_PLATFORMS = ["tiktok", "instagram", "youtube"]
|
|
|
|
|
|
def _auth_headers():
|
|
return {"Authorization": f"Apikey {settings.managed_upload_post_key}"}
|
|
|
|
|
|
def profile_username_for(user_id) -> str:
|
|
return f"os_{user_id.hex[:12]}"
|
|
|
|
|
|
async def _create_remote_profile(username: str):
|
|
"""Create the profile in Upload-Post. Treat 'already exists' as success."""
|
|
async with httpx.AsyncClient(timeout=20) as client:
|
|
resp = await client.post(
|
|
f"{API_BASE}/uploadposts/users",
|
|
headers=_auth_headers(),
|
|
json={"username": username},
|
|
)
|
|
if resp.status_code in (200, 201):
|
|
return
|
|
text = resp.text.lower()
|
|
# Idempotency: if it already exists, that's fine.
|
|
if resp.status_code == 400 and ("exist" in text or "already" in text):
|
|
return
|
|
# Upload-Post plan cap reached — surface a clean, actionable error, not a 500.
|
|
if "profile_limit_reached" in text or "limit of" in text:
|
|
raise HTTPException(
|
|
status_code=503,
|
|
detail="Social posting is temporarily unavailable (provider profile limit). "
|
|
"Please try again later.",
|
|
)
|
|
resp.raise_for_status()
|
|
|
|
|
|
async def ensure_profile(user) -> str:
|
|
"""Return the user's Upload-Post profile username, creating it if needed."""
|
|
async with database.session() as s:
|
|
prof = await s.get(UploadPostProfile, user.id)
|
|
if prof:
|
|
return prof.profile_username
|
|
|
|
username = profile_username_for(user.id)
|
|
await _create_remote_profile(username)
|
|
|
|
async with database.session() as s:
|
|
async with s.begin():
|
|
prof = await s.get(UploadPostProfile, user.id) # re-check under txn
|
|
if prof:
|
|
return prof.profile_username
|
|
s.add(UploadPostProfile(user_id=user.id, profile_username=username))
|
|
return username
|
|
|
|
|
|
# Where the connect page sends the user back to. A closed list: the value
|
|
# comes from the client and ends up in a redirect.
|
|
RETURN_TARGETS = {
|
|
"account": "/#/account?connected=1",
|
|
"autopilot": "/#app?tab=autopilot&connected=1",
|
|
}
|
|
|
|
|
|
async def get_connect_url(username: str, return_to: str = "account") -> str:
|
|
"""Generate a branded, single-use connection URL (~1h) for the user's socials."""
|
|
back = RETURN_TARGETS.get(return_to, RETURN_TARGETS["account"])
|
|
payload = {
|
|
"username": username,
|
|
"logoImage": settings.openshorts_logo_url,
|
|
"connectTitle": "Connect your social accounts",
|
|
"connectDescription": "Link TikTok, Instagram and YouTube to post your shorts directly from OpenShorts.",
|
|
"redirectUrl": f"{settings.frontend_url}{back}",
|
|
"redirectButtonText": "Back to OpenShorts",
|
|
"platforms": CONNECT_PLATFORMS,
|
|
"showCalendar": True, # keep the scheduling calendar available in the page
|
|
}
|
|
async with httpx.AsyncClient(timeout=20) as client:
|
|
resp = await client.post(
|
|
f"{API_BASE}/uploadposts/users/generate-jwt",
|
|
headers=_auth_headers(),
|
|
json=payload,
|
|
)
|
|
resp.raise_for_status()
|
|
data = resp.json()
|
|
return data.get("access_url") or data.get("url")
|
|
|
|
|
|
@router.post("/api/social/connect")
|
|
async def social_connect(request: Request):
|
|
"""Return a branded Upload-Post connection URL for the signed-in managed user."""
|
|
from .auth import get_current_user_required
|
|
user = await get_current_user_required(request)
|
|
username = await ensure_profile(user)
|
|
access_url = await get_connect_url(username, request.query_params.get("return_to") or "account")
|
|
return {"access_url": access_url}
|