-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathscraper.py
75 lines (63 loc) · 2.46 KB
/
scraper.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
import csv
import datetime
import json
import asyncio
import aiohttp
import dotenv
import os
from random import randint
dotenv.load_dotenv()
async def get_data_async(date: str):
auth = os.getenv("auth")
url = "https://c-api-gateway.tkg.spdigital.io/skalbox/api"
headers = {"User-Agent": "Mozilla/5.0 (X11; Ubuntu; Linux x86_64; rv:124.0) Gecko/20100101 Firefox/124.0",
"Accept": "application/json, text/plain, */*",
"Accept-Language": "en-US,en;q=0.5",
"Content-Type": "application/json",
"Authorization": auth
}
payload = {"charts:hourly": {"accountNos": ["8941355284"], "date": date}}
async with aiohttp.ClientSession() as session:
try:
async with session.post(url, headers=headers, json=payload) as response:
if response.status != 200:
print(f"Error: HTTP status {response.status}")
return None
return json.loads(await response.text())
except Exception as e:
print(f"Error: {e}")
return None
def save_data(data):
electricity_data = []
for d in data:
for i in d["charts:hourly"]["data"]["electricity"]["consumption"]:
electricity_data.append([i["period"], i["current"], i["average"], i["efficient"]])
with open("hourly_electricity_data.csv", "a", encoding='utf-8') as f:
w = csv.writer(f)
w.writerows(electricity_data)
async def main():
date = datetime.date.today()
date -= datetime.timedelta(days=1)
tasks = []
while True:
data = await get_data_async(str(date))
if data is None:
print("Error occurred, stopping.")
break
elif data["status"] == 150:
print("No data available, stopping.")
break
elif not data["charts:hourly"]["data"]["electricity"]["consumption"]:
print("No data available, stopping.")
break
tasks.append(data)
date -= datetime.timedelta(days=1)
await asyncio.sleep(randint(1, 5)) # add delay to avoid hitting rate limit
save_data(tasks)
def pull_current_data():
date = datetime.date.today()
data = get_data_async(date)
period, current, average, efficient = data[0]["charts:hourly"]["data"]["electricity"]["consumption"]
return {"period": period, "current": current, "average": average, "efficient": efficient}
if __name__ == "__main__":
asyncio.run(main())