-
Notifications
You must be signed in to change notification settings - Fork 902
/
KW.py
79 lines (64 loc) · 2.39 KB
/
KW.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
#!/usr/bin/env python3
# coding=utf-8
"""
This parser returns Kuwait's electricity system load (assumed to be equal to electricity production)
Source: Ministry of Electricity and Water / State of Kuwait
URL: https://www.mew.gov.kw/en/
Scroll down to see the system load gauge
Shares of Electricity production in 2017: 65.6% oil, 34.4% gas (source: IEA; https://www.iea.org/statistics/?country=KUWAIT&indicator=ElecGenByFuel)
"""
import re
from datetime import datetime
from logging import Logger, getLogger
from typing import Optional
import arrow
from requests import Session
def fetch_production(
zone_key: str = "KW",
session: Optional[Session] = None,
target_datetime: Optional[datetime] = None,
logger: Logger = getLogger(__name__),
):
if target_datetime:
raise NotImplementedError("This parser is not yet able to parse past dates")
# Kuwait very rarely imports power, so we assume that production is equal to consumption
# "Kuwait imports power in an emergency and only for a few hours at a time"
# See https://github.com/tmrowco/electricitymap-contrib/pull/2457#pullrequestreview-408781556
consumption_dict = fetch_consumption(
zone_key=zone_key, session=session, logger=logger
)
consumption = consumption_dict["consumption"]
datapoint = {
"zoneKey": zone_key,
"datetime": arrow.now("Asia/Kuwait").datetime,
"production": {"unknown": consumption},
"source": "mew.gov.kw",
}
return datapoint
def fetch_consumption(
zone_key: str = "KW",
session: Optional[Session] = None,
target_datetime: Optional[datetime] = None,
logger: Logger = getLogger(__name__),
):
if target_datetime:
raise NotImplementedError("This parser is not yet able to parse past dates")
r = session or Session()
url = "https://www.mew.gov.kw/en"
response = r.get(url)
load = re.findall(r"\((\d{4,5})\)", response.text)
load = int(load[0])
consumption = load
datapoint = {
"zoneKey": zone_key,
"datetime": arrow.now("Asia/Kuwait").datetime,
"consumption": consumption,
"source": "mew.gov.kw",
}
return datapoint
if __name__ == "__main__":
"""Main method, never used by the electricityMap backend, but handy for testing."""
print("fetch_consumption() ->")
print(fetch_consumption())
print("fetch_production() ->")
print(fetch_production())