Skip to content

Instantly share code, notes, and snippets.

@ngocjohn
Created June 1, 2024 22:23
Show Gist options
  • Save ngocjohn/b1c1f3730cc6f7079ae0d2b3bddd57ad to your computer and use it in GitHub Desktop.
Save ngocjohn/b1c1f3730cc6f7079ae0d2b3bddd57ad to your computer and use it in GitHub Desktop.
import os
import requests
# Configuration
api_key = "[YOUR_API_KEY]]"
vehicle_id = "[YOUR_VEHICLE_VIN_ID]"
base_url = "https://api.mercedes-benz.com/vehicle_images/v2"
image_params = {
"roofOpen": "false",
"night": "false",
"background": "false",
"cropped": "false",
"cropPosition": "both",
"fileFormat": "png",
"uniformImage": "none",
}
headers = {"x-api-key": api_key, "accept": "application/json"}
output_folder = "[YOUR_OUTPUT_FOLDER]"
# Ensure output folder exists
if not os.path.exists(output_folder):
os.makedirs(output_folder)
# Function to get image IDs
def get_image_ids(vehicle_id):
response = requests.get(
f"{base_url}/vehicles/{vehicle_id}", params=image_params, headers=headers
)
response.raise_for_status() # Raise an error for bad status codes
return response.json()
# Function to download an image by its ID
def download_image(image_id, filename):
image_url = f"{base_url}/images/{image_id}"
image_headers = {"x-api-key": api_key, "accept": "*/*"}
response = requests.get(image_url, headers=image_headers)
response.raise_for_status() # Raise an error for bad status codes
with open(filename, "wb") as file:
file.write(response.content)
# Main script
try:
image_ids = get_image_ids(vehicle_id)
for angle, image_id in image_ids.items():
filename = os.path.join(output_folder, f"{angle}.png")
print(f"Downloading {angle} image...")
download_image(image_id, filename)
print("All images downloaded successfully.")
except requests.RequestException as e:
print(f"An error occurred: {e}")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment