-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.py
More file actions
21 lines (19 loc) · 718 Bytes
/
Copy pathmain.py
File metadata and controls
21 lines (19 loc) · 718 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
import requests
from PIL import Image
url = "https://api.nasa.gov/planetary/apod"
api_key = "YOUR_API_KEY"
def save_apod_image(date, file_path):
# Construct the API request URL
params = {"date": date, "api_key": api_key}
response = requests.get(url, params=params)
# Get the image URL from the API response
data = response.json()
image_url = data["url"]
# Download the image and save it to file
response = requests.get(image_url, stream=True)
if response.status_code == 200:
with open(file_path, "wb") as f:
for chunk in response.iter_content(1024):
f.write(chunk)
else:
print("Failed to download image: %s" % response.status_code)