-
Notifications
You must be signed in to change notification settings - Fork 902
/
JP-KY.py
141 lines (129 loc) · 4.22 KB
/
JP-KY.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
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
#!/usr/bin/env python3
# coding=utf-8
import re
from datetime import datetime
from logging import Logger, getLogger
from typing import Optional, Union
# The arrow library is used to handle datetimes
import arrow
# The request library is used to fetch content through HTTP
from bs4 import BeautifulSoup
from requests import Session, get
from . import occtonet
def fetch_production(
zone_key: str = "JP-KY",
session: Optional[Session] = None,
target_datetime: Optional[datetime] = None,
logger: Logger = getLogger(__name__),
) -> Union[dict, list]:
"""Requests the last known production mix (in MW) of a given zone."""
if target_datetime:
raise NotImplementedError("This parser is not yet able to parse past dates")
data = {
"zoneKey": zone_key,
"datetime": None,
"production": {
"biomass": 0,
"coal": 0,
"gas": 0,
"hydro": None,
"nuclear": None,
"oil": 0,
"solar": None,
"wind": None,
"geothermal": None,
"unknown": 0,
},
"storage": {},
"source": "www.kyuden.co.jp",
}
# url for consumption and solar
url = "https://www.kyuden.co.jp/td_power_usages/pc.html"
r = get(url)
r.encoding = "utf-8"
html = r.text
soup = BeautifulSoup(html, "lxml")
# get hours, minutes
ts = soup.find("p", class_="puProgressNow__time").get_text()
hours = re.findall("[\d]+(?=時)", ts)[0]
minutes = re.findall("(?<=時)[\d]+(?=分)", ts)[0]
# get date
ds = soup.find("div", class_="puChangeGraph")
date = re.findall("(?<=chart/chart)[\d]+(?=.gif)", str(ds))[0]
# parse datetime
dt = "".join(
[
date[:4],
"-",
date[4:6],
"-",
date[6:],
" ",
"{0:02d}:{1:02d}".format(int(hours), int(minutes)),
]
)
dt = arrow.get(dt).replace(tzinfo="Asia/Tokyo").datetime
data["datetime"] = dt
# consumption
cons = soup.find("p", class_="puProgressNow__useAmount").get_text()
cons = re.findall(
"(?<=使用量\xa0)[-+]?[.]?[\d]+(?:,\d\d\d)*[\.]?\d*" + "(?:[eE][-+]?\d+)?(?=万kW/)",
cons,
)
cons = cons[0].replace(",", "")
# convert from 万kW to MW
cons = float(cons) * 10
# solar
solar = soup.find("td", class_="puProgressSun__num").get_text()
# convert from 万kW to MW
solar = float(solar) * 10
# add nuclear power plants
# Sendai and Genkai
url_s = "".join(
[
"http://www.kyuden.co.jp/php/nuclear/sendai/rename.php?",
"A=s_power.fdat&B=ncp_state.fdat&_=1520532401043",
]
)
url_g = "".join(
[
"http://www.kyuden.co.jp/php/nuclear/genkai/rename.php?",
"A=g_power.fdat&B=ncp_state.fdat&_=1520532904073",
]
)
sendai = get(url_s).text
sendai = re.findall(
"(?<=gouki=)[-+]?[.]?[\d]+(?:,\d\d\d)*[\.]?\d*" + "(?:[eE][-+]?\d+)?(?=&)",
sendai,
)
genkai = get(url_g).text
genkai = re.findall(
"(?<=gouki=)[-+]?[.]?[\d]+(?:,\d\d\d)*[\.]?\d*" + "(?:[eE][-+]?\d+)?(?=&)",
genkai,
)
nuclear = 0
for sendai_i in sendai:
nuclear += float(sendai_i)
for genkai_i in genkai:
nuclear += float(genkai_i)
# convert from 万kW to MW
nuclear = nuclear * 10
# add the exchange JP-CG->JP-KY
exch_list = occtonet.fetch_exchange("JP-KY", "JP-CG")
# find the nearest exchanges in time to consumption timestamp
nearest_exchanges = sorted(exch_list, key=lambda exch: abs(exch["datetime"] - dt))
# take the nearest exchange
exch = nearest_exchanges[0]
# check that consumption and exchange timestamps are within a 15 minute window
if abs(dt - exch["datetime"]).seconds <= 900:
generation = cons - exch["netFlow"]
data["production"]["solar"] = solar
data["production"]["nuclear"] = nuclear
data["production"]["unknown"] = generation - nuclear - solar
return data
else:
return []
if __name__ == "__main__":
"""Main method, never used by the Electricity Map backend, but handy for testing."""
print("fetch_production() ->")
print(fetch_production())