#!/usr/bin/env python3 # Listing reel from a link: the photos and facts of a public listing page # (Zillow, Realtor.com, Redfin or FurnishedFinder) become the photo reel. The # read is 2 credits, bringing the photos in is free, each small photo # sharpened is 2 credits, and the final is 15 credits for each started 10 # seconds (a 30-second reel: 45). The Preview is free. # The calls of the page https://api.oneupai.com/docs/recipes/listing-from-link, # in its order, as one program. # # export ONEUP_API_KEY="oneup_live_..." # your Builder key # python3 run.py "https://www.zillow.com/homedetails/..." # Python 3.8+ # # ONEUP_BASE_URL changes the host (default https://api.oneupai.com). Every # answer is printed as it arrives. Standard library only. import json, os, sys, time, urllib.error, urllib.request, uuid API = os.environ.get("ONEUP_BASE_URL", "https://api.oneupai.com") KEY = os.environ["ONEUP_API_KEY"] # The default Python User-Agent is refused before a request reaches the API; # any name for your app works. AGENT = "my-app/1.0" if len(sys.argv) < 2: raise SystemExit("Give the listing page address as the first argument") LISTING = sys.argv[1] def call(method, path, body=None, headers=None): """One API call: the answer is printed and returned; a refusal stops here.""" request = urllib.request.Request( API + path, method=method, data=None if body is None else json.dumps(body).encode(), headers={ "Authorization": f"Bearer {KEY}", "Content-Type": "application/json", "User-Agent": AGENT, **(headers or {}), }, ) try: with urllib.request.urlopen(request, timeout=60) as response: answer = json.load(response) except urllib.error.HTTPError as error: problem = json.load(error) print(json.dumps(problem)) raise SystemExit(f"{error.code} {problem['error']}: {problem['message']}") print(json.dumps(answer)) return answer def retry_key(): return {"Idempotency-Key": str(uuid.uuid4())} def wait_for(ticket): """Waits for a queued job (every 2 s for 30 s, then every 5 s); its result.""" started = time.monotonic() status = call("GET", f"/v1/queue/status/{ticket}") while status["status"] not in ("COMPLETED", "FAILED"): time.sleep(2 if time.monotonic() - started < 30 else 5) status = call("GET", f"/v1/queue/status/{ticket}") return call("GET", f"/v1/queue/result/{ticket}") def render(project_id, version_id, kind): r = call( "POST", "/v1/renders", { "alias": "oneup/render-timeline", "project_id": project_id, "version_id": version_id, "kind": kind, }, retry_key(), ) render_id = r["render_id"] while r["status"] not in ("COMPLETED", "FAILED"): time.sleep(5) r = call("GET", f"/v1/renders/{render_id}") return r # 1. Read the listing (2 credits; the same retry key within 15 minutes # returns the first answer without a charge). listing = call( "POST", "/v1/run/oneup/web-read-listing", {"url": LISTING}, retry_key() )["output"] facts = listing["facts"] # 2. Bring the photos into your account (free; within an hour of the read). imported = call( "POST", "/v1/uploads/from-extraction", { "extraction_id": listing["extraction_id"], "urls": [p["url"] for p in listing["photos"][:40]], }, ) # 3. Sharpen each photo whose shorter side is under 1080 pixels (2 credits # each), and use the sharper copy in its place. small = { p["url"] for p in listing["photos"] if p.get("width", 1080) < 1080 or p.get("height", 1080) < 1080 } photos = [] for item in imported["uploads"]: if item["url"] not in small: photos.append(item["upload_id"]) continue job = call( "POST", "/v1/queue/submit/oneup/image-upscale", {"image_url": item["file_url"]}, retry_key(), ) sharper = wait_for(job["ticket"]) photos.append(sharper["assets"][0]["asset_id"]) if not photos: raise SystemExit("no photo could be brought in") # 4. The photo reel: each photo 3 s with a push-in or a pan, 0.4 s # crossfades, a 4-second closing card, the address and price over the # opening. project = call("POST", "/v1/projects", {"agent_slug": "oneup/render-timeline"}) project_id = project["project_id"] push_in = { "op": "kenburns", "from": {"x": 0.5, "y": 0.5, "zoom": 1}, "to": {"x": 0.5, "y": 0.5, "zoom": 1.18}, "ease": "ease_in_out", } pan = { "op": "kenburns", "from": {"x": 0.58, "y": 0.5, "zoom": 1.18}, "to": {"x": 0.42, "y": 0.5, "zoom": 1.18}, "ease": "ease_in_out", } segments = [ { "id": f"p{i + 1}", "source": {"$asset": photo}, "duration": 3000, "effects": [push_in if i % 2 == 0 else pan], "transition_out": {"op": "crossfade", "duration": 400}, } for i, photo in enumerate(photos) ] segments.append( { "id": "card", "source": {"$asset": photos[-1]}, "duration": 4000, "effects": [{"op": "fade", "from": 1, "to": 0, "duration": 600}], } ) timeline = { "v": "1.1", "output": {"aspect": "9:16", "resolution": 1080, "fps": 30, "container": "mp4"}, "segments": segments, "overlays": [ { "id": "t1", "kind": "text", "content": facts["address"], "style": "lower_third", "position": "middle", "range": {"from": 0, "to": 3000}, "enter": {"op": "fade", "duration": 250}, "exit": {"op": "fade", "duration": 200}, }, { "id": "t2", "kind": "text", "content": f"{facts['price']} · {facts['beds']} bd · {facts['baths']} ba", "style": "lower_third", "position": "bottom", "range": {"from": 0, "to": 3000}, }, ], } version = call("PUT", f"/v1/projects/{project_id}/timeline", {"timeline": timeline}) # 5. A free Preview, then the final, then the MP4's link (good for an hour). render(project_id, version["version_id"], "preview") final = render(project_id, version["version_id"], "final") call("GET", f"/v1/queue/result/{final['ticket']}")