-
Notifications
You must be signed in to change notification settings - Fork 0
/
plot.py
61 lines (47 loc) · 1.73 KB
/
plot.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
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
import json
import re
import matplotlib.pyplot as plt
import requests
from datetime import datetime
import os
FREECURRENCYAPI = os.environ.get('FREECURRENCYAPI')
if not FREECURRENCYAPI:
raise ValueError("API key not found in environment variables")
response = requests.get(f"https://api.freecurrencyapi.com/v1/latest?apikey=${FREECURRENCYAPI}¤cies=EUR&base_currency=AUD")
data = response.json()
# Get the current timestamp
timestamp = datetime.now().strftime('%Y%m%d%H%M%S')
# Update consolidated JSON file
consolidated_file_path = 'data/storage.json'
with open(consolidated_file_path, 'r') as consolidated_file:
consolidated_data = json.load(consolidated_file)
# Append new data with timestamp
consolidated_data.append({'date': timestamp, 'data': data})
# Write the updated data back to the file
with open(consolidated_file_path, 'w') as consolidated_file:
json.dump(consolidated_data, consolidated_file, indent=2)
# Specify the path to the consolidated JSON file
consolidated_file_path = 'data/storage.json'
# Read data from the consolidated JSON file
with open(consolidated_file_path, 'r') as consolidated_file:
consolidated_data = json.load(consolidated_file)
dates = []
values = []
for entry in consolidated_data:
try:
date = entry["date"]
eur_value = entry["data"]["EUR"]
dates.append(date)
values.append(eur_value)
except KeyError as e:
print(f"KeyError: {e} in entry: {entry}")
plt.figure(figsize=(10, 5))
plt.plot(dates, values, marker='o')
plt.title("EUR Value Over Time")
plt.xlabel("Date and Time")
plt.ylabel("EUR Value")
plt.xticks(rotation=45)
plt.tight_layout()
chart_file = "eur_value_chart.png"
plt.savefig(chart_file)
print(f"Chart has been saved to {chart_file}")