-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfetch_prices.py
More file actions
executable file
·40 lines (32 loc) · 1.24 KB
/
fetch_prices.py
File metadata and controls
executable file
·40 lines (32 loc) · 1.24 KB
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
31
32
33
34
35
36
37
38
39
40
#!/usr/bin/env python3
import requests
import sys
import json
def fetch_full_historical_prices():
api_url = "https://mempool.space/api/v1/historical-price?currency=USD×tamp=0"
output_file = "prices.json"
print(f"--- Starting Full Historical BTC Price Fetch from {api_url} ---")
try:
response = requests.get(api_url, timeout=30)
response.raise_for_status() # Raise an exception for HTTP errors (4xx or 5xx)
historical_data = response.json()
if not historical_data:
print("No historical price data received.")
sys.exit(1)
with open(output_file, "w") as f:
json.dump(historical_data, f, indent=4)
print(f"Full historical prices saved to: {output_file}")
except requests.exceptions.Timeout:
print("Error: The request timed out.")
sys.exit(1)
except requests.exceptions.RequestException as e:
print(f"Error fetching data: {e}")
sys.exit(1)
except json.JSONDecodeError:
print("Error: Could not decode JSON response from the API.")
sys.exit(1)
except Exception as e:
print(f"An unexpected error occurred: {e}")
sys.exit(1)
if __name__ == "__main__":
fetch_full_historical_prices()