-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfetch.py
30 lines (22 loc) · 830 Bytes
/
fetch.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
import requests
import json
# URL of the API endpoint
url = "http://localhost:8000/mysite/projects/"
try:
# Make a GET request to the API
response = requests.get(url)
response.raise_for_status() # Raise an exception for HTTP errors
# Parse the JSON response
projects = response.json()
# Specify the file path where you want to save the data
file_path = 'public/db/projects.json'
# Write the JSON data to a file
with open(file_path, 'w') as file:
json.dump(projects, file, indent=4)
print(f"Data successfully written to {file_path}")
except requests.exceptions.RequestException as e:
print(f"HTTP request failed: {e}")
except json.JSONDecodeError as e:
print(f"Failed to parse JSON response: {e}")
except IOError as e:
print(f"Failed to write to file: {e}")