-
Notifications
You must be signed in to change notification settings - Fork 0
/
gas.py
78 lines (64 loc) · 2.09 KB
/
gas.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
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
import requests
import json
import psycopg2
from datetime import datetime
# Initial API URL
base_url = "https://api.octopus.energy/v1/gas-meter-points/56743804/meters/E6E11808992323/consumption/"
# Database connection parameters
db_params = {
"dbname": "energy",
"user": "admin",
"password": ".password",
"host": "localhost",
"port": "5432"
}
headers = {'Authorization': 'Basic c2tfbGl2ZV8zb0h2aHRRNkNCQmdHeWFYNTZFc2pEUXY6'}
def fetch_all_data(url):
all_results = []
while url:
response = requests.get(url, headers=headers)
if response.status_code == 200:
data = response.json()
all_results.extend(data['results'])
# Get the next URL, or None if it doesn't exist
url = data.get('next')
print(f"Fetched {len(data['results'])} records. Next URL: {url}")
else:
raise Exception(
f"API request failed with status code {response.status_code}")
return all_results
def save_to_database(data):
conn = psycopg2.connect(**db_params)
cur = conn.cursor()
# Create table if it doesn't exist
cur.execute("""
CREATE TABLE IF NOT EXISTS gas_consumption (
id SERIAL PRIMARY KEY,
consumption FLOAT,
interval_start TIMESTAMP UNIQUE,
interval_end TIMESTAMP
)
""")
# Insert data
for result in data:
cur.execute("""
INSERT INTO gas_consumption (consumption, interval_start, interval_end)
VALUES (%s, %s, %s)
ON CONFLICT (interval_start) DO UPDATE
SET consumption = EXCLUDED.consumption,
interval_end = EXCLUDED.interval_end
""", (
result['consumption'],
datetime.fromisoformat(result['interval_start']),
datetime.fromisoformat(result['interval_end'])
))
conn.commit()
cur.close()
conn.close()
def main():
all_data = fetch_all_data(base_url)
print(f"Total records fetched: {len(all_data)}")
save_to_database(all_data)
print("Data successfully saved to database.")
if __name__ == "__main__":
main()