-
Notifications
You must be signed in to change notification settings - Fork 0
/
parser-crs-url.py
executable file
·194 lines (167 loc) · 6.76 KB
/
parser-crs-url.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
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
#!/usr/bin/python3
import os.path
import sys
import re
import click
from selenium import webdriver
from selenium.webdriver.firefox.options import Options
from selenium.webdriver.common.by import By
from selenium.common.exceptions import NoSuchElementException
from time import sleep
import yaml
from selenium.webdriver.firefox.options import Options
def get_reviry_params(url):
options = Options()
options.set_preference('intl.accept_languages', 'cz-CZ, cz')
driver = webdriver.Firefox(options=options)
driver.get(url)
output = dict()
elements = driver.find_elements(By.XPATH, "//tr[contains(@id,'revir_')]")
clicks = [x.get_attribute("onClick") for x in elements]
for item in clicks:
driver.execute_script(item)
sleep(6)
try:
name = driver.find_element(By.TAG_NAME, "H3").text
except NoSuchElementException:
print("ERROR name:", item)
name = "ERROR:" + str(item)
allitems = driver.find_elements(By.TAG_NAME, "FIELDSET")
desc = ""
for field in allitems:
if field.text.startswith("Popis"):
desc = field.text
mapy_link = ""
try:
mapy = driver.find_element(By.XPATH, "//a[contains(@href,'/inc/mapy.php')]")
mapy_link = f"https://www.rybsvaz.cz/{mapy.get_attribute('href')}"
except NoSuchElementException:
print("ERROR MAPA:", item)
output[name] = [desc, mapy_link, driver.page_source]
driver.back()
return output
def strip_list(inp):
output = []
for item in inp:
if item is not None:
output.append(item.strip())
def revir_data(text, revir_name):
jmeno = revir_name
jmeno_add = ""
GPS = {}
for line in text.split("\n"):
#print(">>:", line, file=sys.stderr)
stripped_line = line.strip()
print("REVIR:", jmeno, file=sys.stderr)
# podrevir
search = re.search('^\d+\..*ha', stripped_line)
if search:
jmeno_add = stripped_line
print("NAME:", jmeno_add, file=sys.stderr)
continue
# reka
search1 = re.search('^\(?GPS.*Z:(.+)K:(.+)', stripped_line)
# rybnik
search2 = re.search('^\(GPS(.*)', stripped_line)
# ostani
search3 = re.findall(r'GPS\S*\s+([^N]+N\S*\s+[^E]+E)', line)
if search1:
zacatek = search1.groups()[0].split("N")
konec = search1.groups()[1].split("N")
GPS[jmeno_add] = (convert(zacatek[0]), convert(zacatek[1]), convert(konec[0]), convert(konec[1]))
print("adding reka", zacatek, konec, file=sys.stderr)
elif search2:
zacatek = search2.group(0).split("N")
if len(zacatek) < 2:
raise Exception(f"BAD GPS {zacatek}")
GPS[jmeno_add] = (convert(zacatek[0]), convert(zacatek[1]))
print("adding rybnik", zacatek)
else:
for item in search3:
#print(">>>>>>>>>>>", search)
zacatek = item.split("N")
GPS[f"ZAKAZ ci RYBNIK: {item}"] = (convert(zacatek[0]), convert(zacatek[1]))
print("ZAKAZ ci RYBNIK", zacatek, file=sys.stderr)
return jmeno, GPS
def convert(coor):
output = 0
found = re.search(r"(\d+)\D+(\d+)\D+([0-9.]+)", coor)
if found:
output = float(found.group(1)) + (float(found.group(2)) / 60.0) + (float(found.group(3))) / 3600.0
else:
found = re.search(r"(\d+\.\d+)", coor)
if found:
output = float(found.group(1))
else:
print(sys.stderr, "ERROR convert coordinates:", coor, file=sys.stderr)
return output
def output_to_file(filename, reviry, color="00ff00", icon_rybnik="https://icones.pro/wp-content/uploads/2022/09/icones-de-poisson-orange.png", icon_nevim="https://www.freeiconspng.com/img/41652"):
with open(filename, mode="w") as outfile:
intro="""<?xml version="1.0" encoding="UTF-8"?>
<kml xmlns="http://www.opengis.net/kml/2.2">
<Document>
"""
intro +=f"""
<Style id="reka">
<LineStyle>
<color>ff{color}</color>
<width>4</width>
</LineStyle>
</Style>
<Style id="rybnik">
<IconStyle>
<color>ff{color}</color>
<Icon><href>{icon_rybnik}</href></Icon>
</IconStyle>
</Style>
<Style id="nevim">
<IconStyle>
<color>ff{color}</color>
<Icon><href>{icon_nevim}</href></Icon>
</IconStyle>
</Style>
"""
outfile.write(intro)
count=0
for revir_name, data in reviry.items():
count += 1
for k, v in data['GPS'].items():
outfile.write('<Placemark>\n')
outfile.write(f'<name>{revir_name}</name>')
description = data["data"]
outfile.write(f'<description>\nJmeno: {k}\n\n{description}</description>')
if "ZAKAZ" in k:
outfile.write('<styleUrl>#nevim</styleUrl>\n')
outfile.write('<Point> <coordinates>{0},{1}\n</coordinates> </Point>\n'.format(v[1], v[0]))
elif len(v) == 2:
outfile.write('<styleUrl>#rybnik</styleUrl>\n')
outfile.write('<Point> <coordinates>{0},{1}\n</coordinates> </Point>\n'.format(v[1], v[0]))
elif len(v) == 4:
outfile.write('<styleUrl>#reka</styleUrl>\n')
outfile.write('<LineString><tessellate>1</tessellate><coordinates>{0},{1}\n{2},{3}\n</coordinates> </LineString>\n'.format(v[1], v[0], v[3], v[2]))
outfile.write('</Placemark>')
outfile.write('</Document>\n')
outfile.write('</kml>')
@click.command()
@click.option("--url", "-u", default="https://www.rybsvaz.cz/pages_cz/reviry/reviry.php?page=reviry%2Freviry&lang=cz&fromIDS=&typ=mpr&id_svaz=7&id_r1=471")
@click.option("--outputfile", "-o", default="crs-reviry-mimopstruhove.kml")
@click.option("--quiet", "-q", is_flag=True, default=False)
def parser(url, outputfile, quiet):
reviry = {}
tempfile = f"{outputfile}.yaml"
if os.path.exists(tempfile):
with open(tempfile) as fd:
data = yaml.safe_load(fd)
else:
data = get_reviry_params(url)
with open(tempfile, "w") as fd:
fd.write(yaml.safe_dump(data))
for int_name, fields in data.items():
text = fields[0]
map_item = fields[1]
page_source = fields[2]
jmeno, GPS = revir_data(text, int_name)
reviry[jmeno] = {'GPS': GPS, 'data': text}
output_to_file(outputfile, reviry)
if __name__ == '__main__':
parser()