-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathvis.py
107 lines (75 loc) · 3.2 KB
/
vis.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
import requests
import json
import typing as t
import datetime
URL_VNV = (
"http://px.hagstofa.is/pxis/api/v1/is/Efnahagur/visitolur/1_vnv/1_vnv/VIS01000.px"
)
URL_VH = "https://talnaefni.fasteignaskra.is/talnaefni/v1/ibudavisitala"
BODY_VNV = {
"query": [
{"code": "Vísitala", "selection": {"filter": "item", "values": ["CPI"]}},
{"code": "Liður", "selection": {"filter": "item", "values": ["index"]}},
],
"response": {"format": "json"},
}
def visitala_neysluverds() -> t.Dict[str, float]:
"""Gets latest consumer price index for Statistics of Iceland"""
r = requests.post(URL_VNV, json=BODY_VNV)
json_response = json.loads(r.content)
vnv: t.Dict[str, float] = dict()
for row in json_response["data"]:
month = row["key"][0].replace("M", "-")
vnv[month] = float(row["values"][0])
return vnv
def visitala_ibudaverds() -> t.Dict[str, float]:
"""Gets latest property price index for all properties in the capital region."""
r = requests.get(URL_VH)
json_response = json.loads(r.content)
vh: t.Dict[str, float] = dict()
for row in json_response:
month = f"{row['Ar']}-{str(row['Manudur']).rjust(2, '0')}"
vh[month] = float(row["Vst_heild"])
return vh
def months_between(d1: datetime.date, d2: datetime.date) -> int:
return (d1.year - d2.year) * 12 + d1.month - d2.month
def check_data(data: t.Dict[str, float], origin: datetime.date) -> None:
"""Checks if the data is within expected length raises Exception if errors are found."""
latest_date = datetime.datetime.strptime(
f"{list(data.keys())[-1]}-01", "%Y-%m-%d"
).date()
if datetime.date.today() - datetime.timedelta(days=90) > latest_date:
raise ValueError("Latest date is not within boundaries")
if len(data) != months_between(latest_date, origin) + 1:
raise ValueError("Missing dates in the dataset")
def main() -> int:
"""Constructs vis.js accoring to the original format of the file"""
vnv = visitala_neysluverds()
vh = visitala_ibudaverds()
# Simple data checks to make sure not to break
# the website if data is missing.
check_data(vnv, datetime.date(1988, 5, 1))
check_data(vh, datetime.date(1994, 1, 1))
with open("vis.js", "w", encoding="utf-8") as f:
f.writelines("// Vísitölur húsnæðisverðs og neysluverðs\n")
f.writelines(
"// Sjá https://fasteignaskra.is/gogn/fasteignagattin/fasteignavidskipti/visitolur-ibuda-og-leiguverds/\n"
)
f.writelines(
"// og https://hagstofa.is/talnaefni/efnahagur/verdlag/visitala-neysluverds/\n\n"
)
f.writelines("var vis = {\n")
for key, vnv_value in vnv.items():
vh_value = vh.get(key)
# Ignore latest records if both vnv and vh do not exist
# Before 1994, there are only records available for vnv
if int(key[:4]) >= 1994 and not vh_value:
continue
f.writelines(f'"{key}" : {{ vnv: {vnv_value}')
if vh_value:
f.writelines(f", vh: {vh_value}")
f.writelines(" },\n")
f.writelines("};")
return 0
if __name__ == "__main__":
exit(main())