Discussions

Ask a Question
Back to All

Send Image from Google Street View API to CompanyCam API

Looking for some guidance. My use case is that when a lead calls and gives me their address, I want to populate the CompanyCam job with an image from Google.

I'm scraping the image from Google Street View to populate into my CompanyCam project. In the code below I am successfully calling the Google Street View API and getting the image back to upload to companycam.

I can see the photo detail in my event log, but I persistently get this error back from CompanyCam:

Failed to upload photo to CompanyCam: 500 - {"errors":["Internal server error, try again shortly"]}

I retry several times and get the same error results.

Here is my python script. I am currently on a Legacy Pro plan with CompanyCam. Would love some feedback is something sticks out obvious.

import requests
import os
from urllib.parse import quote

# Configuration
GOOGLE_API_KEY = "REMOVED"  # Replace with your Google API key
COMPANYCAM_API_KEY = "REMOVED"  # Replace with your CompanyCam API key
BASE_GOOGLE_URL = "https://maps.googleapis.com/maps/api/streetview"
BASE_COMPANYCAM_URL = "https://api.companycam.com/v2"

# Headers for CompanyCam
companycam_headers = {
    "Authorization": f"Bearer {COMPANYCAM_API_KEY}"
}

# Input data from Zapier
project_name = input_data.get("project_name")
address = input_data.get("address")

# Debug: Log raw input_data
print("Raw input_data:", input_data)

# Step 1: Validate inputs
if not project_name:
    return {"error": "Missing project_name from previous Zapier step", "input_data": input_data}
if not address:
    return {"error": "Missing address from previous Zapier step", "input_data": input_data}

# Step 2: Find the CompanyCam project by name (single page)
projects_url = f"{BASE_COMPANYCAM_URL}/projects?per_page=100&page=1"
projects_response = requests.get(projects_url, headers=companycam_headers)
projects_status = projects_response.status_code
projects_data = projects_response.json() if projects_status == 200 else projects_response.text

if projects_status != 200:
    return {
        "error": f"Failed to fetch projects: {projects_status} - {projects_data}",
        "input_data": input_data
    }

project_id = None
for project in projects_data:
    if project.get("name") == project_name:
        project_id = project.get("id")
        break

if not project_id:
    return {
        "error": f"No project found with name '{project_name}' in first 100 projects",
        "input_data": input_data,
        "projects_searched": [p["name"] for p in projects_data]
    }

# Step 3: Fetch Street View image directly using the address
streetview_url = f"{BASE_GOOGLE_URL}?size=800x600&location={quote(address)}&key={GOOGLE_API_KEY}"
streetview_response = requests.get(streetview_url, stream=True)
streetview_status = streetview_response.status_code

if streetview_status != 200:
    return {
        "error": f"Failed to fetch Street View image: {streetview_status} - {streetview_response.text}",
        "input_data": input_data
    }

# Check if Street View returned a valid image
streetview_response.raw.decode_content = True
image_content = streetview_response.content
image_size = len(image_content)
print(f"Street View image size: {image_size} bytes")

if image_size < 1000:
    return {
        "error": "No Street View image available for this address",
        "input_data": input_data,
        "image_size": image_size
    }

# Save the photo temporarily
photo_path = "/tmp/streetview_photo.jpg"
with open(photo_path, "wb") as f:
    f.write(image_content)

# Step 4: Upload the photo to CompanyCam
upload_url = f"{BASE_COMPANYCAM_URL}/projects/{project_id}/photos"
files = {
    "photo[image]": ("streetview_photo.jpg", open(photo_path, "rb"), "image/jpeg")
}

print(f"Uploading to CompanyCam project ID: {project_id}")
upload_response = requests.post(
    upload_url,
    headers=companycam_headers,
    files=files
)
upload_status = upload_response.status_code
upload_data = upload_response.json() if upload_status in [200, 201] else upload_response.text

# Clean up temporary file
os.remove(photo_path)

# Step 5: Check response and build output
if upload_status not in [200, 201]:
    return {
        "error": f"Failed to upload photo to CompanyCam: {upload_status} - {upload_data}",
        "input_data": input_data,
        "image_size": image_size,
        "project_id": project_id
    }

output = {
    "project_name": project_name,
    "project_id": project_id,
    "address": address,
    "streetview_status": streetview_status,
    "image_size": image_size,
    "upload_status": upload_status,
    "upload_data": upload_data,
    "input_data": input_data
}
return output