-
Notifications
You must be signed in to change notification settings - Fork 902
/
GCCIA.py
89 lines (71 loc) · 2.59 KB
/
GCCIA.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
#!/usr/bin/env python3
# coding=utf-8
"""
This parser returns Gulf Cooperation Council countries (United Arab Emirates, Bahrain, Saudi Arabia, Oman, Qatar, and Kuwait) electricity demand (only consumption, production data is not available)
Kuwait has a good data source and parser of its own, but should it become unavailable this parser can return data for Kuwait as well
Source: Gulf Cooperation Council Interconnection Authority
URL: https://www.gccia.com.sa/
Scroll down to see the system demand map
Kuwait shares of Electricity production in 2017: 65.6% oil, 34.4% gas (source: IEA; https://www.iea.org/statistics/?country=KUWAIT&indicator=ElecGenByFuel)
"""
# TODO get this data for the other countries as well
import re
from datetime import datetime
from logging import Logger, getLogger
from sys import stderr
from typing import Optional
import arrow
from requests import Session
from .lib.exceptions import ParserException
COUNTRY_CODE_MAPPING = {
"AE": "uae",
"BH": "bah",
"KW": "kuw",
"OM": "omn",
"QA": "qat",
"SA": "ksa",
}
TIME_ZONE_MAPPING = {
"AE": "Asia/Dubai",
"BH": "Asia/Bahrain",
"KW": "Asia/Kuwait",
"OM": "Asia/Muscat",
"QA": "Asia/Qatar",
"SA": "Asia/Riyadh",
}
def fetch_consumption(
zone_key,
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.gccia.com.sa/"
response = r.get(url)
pattern = COUNTRY_CODE_MAPPING[zone_key] + r'-mw-val">\s*(\d+)'
match = re.findall(pattern, response.text)
if not match:
# if no data, the text becomes " - "
raise ParserException(
"GCCIA.py", "data is currently not available", zone_key=zone_key
)
consumption = int(match[0])
datapoint = {
"zoneKey": zone_key,
"datetime": arrow.now(TIME_ZONE_MAPPING[zone_key]).datetime,
"consumption": consumption,
"source": "www.gccia.com.sa", # URL won't work without WWW
}
return datapoint
if __name__ == "__main__":
"""Main method, never used by the electricityMap backend, but handy for testing."""
for i in COUNTRY_CODE_MAPPING:
print("fetch_consumption('{0}') ->".format(i))
try:
print(fetch_consumption(i))
except IndexError as error:
print("Could not fetch consumption data for {0}".format(i), file=stderr)
print(type(error), ":", error, file=stderr)
print()