-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathupdate-data.py
68 lines (49 loc) · 1.92 KB
/
update-data.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
#!/usb/bin/env python3
import sys
__author__ = "jikuja"
__license__ = "CC0 1.0"
__copyright__ = """<PROGRAM NAME> - <DESCRIPTION>
Written in 2016 by jikuja
To the extent possible under law, the author(s) have dedicated all copyright and related and neighboring rights to
this software to the public domain worldwide. This software is distributed without any warranty.
You should have received a copy of the CC0 Public Domain Dedication along with this software.
If not, see <http://creativecommons.org/publicdomain/zero/1.0/>."
"""
import json
import requests
import re
from lxml import html
def add_data(num, txt):
data_array.append({"id": num, "value": txt})
data_map[num] = txt
page = requests.get("http://finlex.fi/fi/laki/ajantasa/1982/19820182")
# TODO: use local cache
tree = html.fromstring(page.content)
foos = tree.xpath('//p[@class="py"]/text()')
data_array = []
data_map = {}
for foo in foos:
print(foo, end="")
if re.match("^\d+\.", foo) or re.match("^\d+ [a-zöäå]\.", foo):
print(" MATCH")
splitted = foo.split(". ")
num = splitted[0]
txt = splitted[1]
data_array.append({"id": num, "value": txt})
data_map[num] = txt
else:
print("")
# fixes
add_data("173", "Rautatien tasoristeyksen lähestymismerkki")
add_data("174", "Rautatien tasoristeyksen lähestymismerkki")
add_data("175", "Rautatien tasoristeyksen lähestymismerkki")
if len(sys.argv) > 1 and sys.argv[1] == "dry":
sys.exit(0)
with open("merkit_array.json", "w") as json_file:
json_file.write(json.dumps(data_array))
with open("merkit_array.js", "w") as json_file:
json_file.write("liikennemerkitCallback(" + json.dumps(data_array) + ");")
with open("merkit_map.json", "w") as json_file:
json_file.write(json.dumps(data_map))
with open("merkit_map.js", "w") as json_file:
json_file.write("liikennemerkitCallback(" +json.dumps(data_map) + ");")