"""Create a review queue; this script does not select or publish an image."""
import json
import os
import sys
import urllib.error
import urllib.request
PAYLOAD = {
"query": "Old Point Loma Lighthouse, Cabrillo National Monument, San Diego, exterior, landscape",
"k": 5,
"filters": {
"commercial": True,
"derivatives": True,
"orientation": "landscape",
"min_width": 1600,
},
}
def review_queue(response):
if not isinstance(response, dict) or not isinstance(response.get("results"), list) or not response.get("query_id"):
raise ValueError("Expected query_id and results from Lightdrift")
return {
"query_id": response["query_id"],
"relaxed": response.get("relaxed", []),
"degraded": response.get("degraded"),
"candidates": [
{"status": "needs_review", "image": hit}
for hit in response["results"]
],
}
def main():
key = os.environ.get("LIGHTDRIFT_API_KEY")
if not key:
raise SystemExit("Set LIGHTDRIFT_API_KEY in your environment first.")
request = urllib.request.Request(
"https://api.lightdrift.ai/v1/search",
data=json.dumps(PAYLOAD).encode(),
headers={"X-API-Key": key, "Content-Type": "application/json"},
method="POST",
)
try:
with urllib.request.urlopen(request, timeout=45) as response:
queue = review_queue(json.load(response))
except urllib.error.HTTPError as error:
raise SystemExit(f"Search returned HTTP {error.code}; inspect account/API status before retrying.") from None
except (urllib.error.URLError, TimeoutError):
raise SystemExit("Search outcome is uncertain; inspect account usage before retrying.") from None
except (ValueError, TypeError):
raise SystemExit("Unexpected response; inspect API status before retrying.") from None
print(json.dumps(queue, indent=2))
if not queue["candidates"]:
print("No candidates; review the brief and filters before another search.", file=sys.stderr)
if __name__ == "__main__":
main()