-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathgetData.py
46 lines (37 loc) · 1.56 KB
/
getData.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
import json
import requests
import datetime
import tqdm
startDate = datetime.datetime(2018, 6, 1)
today = datetime.datetime.today()
months_to_add = ((today.year - startDate.year) * 12 + today.month - startDate.month)
delta = datetime.timedelta(30)
regionid = 17
currentDate = startDate
file = "./sql.txt"
f = open(file, 'w')
for id in range(regionid):
for i in tqdm.tqdm(range(months_to_add)):
dateFrom = currentDate.strftime("%Y-%m-%d")
dateFromurl = dateFrom + "T00:00Z"
dateTo = (currentDate + delta).strftime("%Y-%m-%d") + "T00:00Z"
dateTourl = dateTo + "T00:00Z"
url = f'https://api.carbonintensity.org.uk/regional/intensity/{dateFrom}/{dateTo}/regionid/{regionid}'
headers = {
'Accept': 'application/json'
}
r = requests.get(url, headers=headers)
data = json.loads(r.text)
regionid = data["data"]["regionid"]
dnoregion = data["data"]["dnoregion"]
shortname = data["data"]["shortname"]
for item in data["data"]["data"]:
date = item["from"].split("T")[0]
time = item["from"].split("T")[1].split("Z")[0]
intensity_forecast = item["intensity"]["forecast"]
generationmix = json.dumps(item["generationmix"])
# 创建SQL语句
sql = f"INSERT INTO region (regionid, dnoregion, shortname, date, time, forecast, data) VALUES ({regionid}, '{dnoregion}', '{shortname}', '{date}', '{time}', '{intensity_forecast}', '{generationmix}');\n"
f.write(sql)
currentDate += delta
f.close()