-
Notifications
You must be signed in to change notification settings - Fork 0
/
andnet-tables.py
296 lines (244 loc) · 9.09 KB
/
andnet-tables.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
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
"""
TODOs:
- [x] export jsons
- [x] export csvs
"""
from urllib.request import Request, urlopen
from bs4 import BeautifulSoup as bs
import pandas as pd
from datetime import datetime
import os, json, re
from datetime import datetime
from utils.common import fetch_data, save_json_to_file, data_root
import demjson3
# from tqdm import tqdm
baseurl='https://dispecerat.andnet.ro/index.php'
outputJsonRoot = data_root + 'andnet/'
legend = {
"1. Evenimente rutiere:": "evenimente rutiere",
"4. Drumuri naționale și autostrăzi cu circulația întreruptă:": "circulatie intrerupta",
"5. Drumuri naționale și autostrăzi cu circulația îngreunată:": "circulatie ingreunata",
"6. Drumuri naționale și autostrăzi pe care se efectueaza lucrări de reabilitare, modernizări, întretinere și reparații:": "lucrari",
"7.1 Starea generală a părții carosabile:": "starea generala",
"8. Conditii meteorologice:": "meteo",
"9. Temperaturi:": "temperatura"
}
# - - - - - - - - - - - - - - - - - - - - -
# Functions
# - - - - - - - - - - - - - - - - - - - - -
def extract_tablez(table_tag):
""" Extract data from a table, return the column headers and the table rows"""
# https://stackoverflow.com/a/44707545/107671
# SCRAPE: Get a table
# table_tag = soup.find("table", {"id" : 'SeasonSplits1_dgSeason%d_ctl00' % table_id})
# SCRAPE: Extract table column headers
columns = [th.text for th in table_tag.findAll("th")]
rows = []
# SCRAPE: Extract Table Contents
for row in table_tag.tbody.findAll("tr"):
# look for vizualizare
# rows.append ([{col.text} for col in row.findAll('td')]) # Gather all columns in the row
rrow = []
for td in row.findAll("td"):
# rows.append([{td.text}])
attrz = {}
placeholder = ''
if td.has_attr("placeholder"):
placeholder = td["placeholder"]
if td.has_attr("onclick"):
onclick = td["onclick"]
# if it has onclick and placeholder ok
# get attributes
onclickx = onclick.split("(")
onclicky = onclickx[1].split(")")
attrz = onclicky[0].split(",")
if len(attrz):
rrow.append({'text': td.text, 'placeholder': placeholder, 'attrz': attrz})
# print(len(attrz))
# breakpoint()
else:
rrow.append({'text': td.text, 'placeholder': placeholder})
# if attrz:
# breakpoint()
rows.append(rrow)
# breakpoint()
# RETURN: [columns, rows]
return [columns, rows]
def swapCoords(x):
out = []
for iter in x:
if isinstance(iter, list):
out.append(swapCoords(iter))
else:
return [x[1], x[0]]
return out
def getSection(xroads):
rez = {"title": xroads["title"], "type": xroads["type"], "roads": []}
rsize = len(xroads['data'])
# pbar = tqdm(total=rsize)
ii = 0
for p in xroads["data"]:
ii += 1
# if ii >=4:
# break
item = {
"indice_drum": p["Indicativ drum"]["text"],
"dela": p["De la km"]["text"],
"panala": p["Pana la km"]["text"],
"acces": 3614,
}
# ic(p)
# NOW PARSE GIS
zurl = (
mbaseurl
+ "?indice_drum="
+ p["Indicativ drum"]["text"]
+ "&dela="
+ p["De la km"]["text"]
+ "&panala="
+ p["Pana la km"]["text"]
+ "&acces=3614"
)
# zurl = "http://zx/gov2/dispecerat-cnadr/data/raw/sample-path.html"
# tqdm.write('>> ' + str(ii) + ' / ' + str(rsize) + ' - ' + zurl)
req = Request(
zurl, headers={"User-Agent": "Mozilla/5.0"}
) # approach url cere pagina html
try:
webpage = urlopen(req).read() # approach url obtine pagina hmtl in text (?)
except:
print("error downloading " + mbaseurl)
quit()
soup = bs(
webpage, "html.parser"
) # parseaza html text si il aseaza frumos in pagina
zcript = soup.select("body script")
zzcript = (
zcript[0]
.contents[0]
.string.replace("\t", "")
.replace("\n", "")
.replace(",}", "}")
.replace(",]", "]")
)
strx = "{" + zzcript + "}"
# json_data = ast.literal_eval(json.dumps(strx))
json_data = json.dumps(strx)
match1 = (
re.search(r"data=\[{.*data2=", json_data, re.DOTALL)
.group(0)
.replace("data=", "")
.replace(";var data2", "")
)
m1 = match1[:-1] # remove last char from string
obj1 = demjson3.decode(m1)[0]
item['Desc']=obj1['Desc']
# FIXME: reverse coords
krds = obj1['coords']
xkrds=[]
for feature in krds:
xkrds.append(swapCoords(feature))
# breakpoint()
item['coords']=xkrds
# match2 = re.search(r'data_polygon=\[{.*function addPointGeom_polygon', json_data, re.DOTALL).group(0).replace('data_polygon=','').replace(';function addPointGeom_polygon','')
rez['roads'].append(item)
# pbar.update(1)
# time.sleep(3)
return rez
def generate_slug(input_string):
input_string = input_string.strip()
input_string = re.sub(r'[^\w\s-]', '', input_string)
input_string = re.sub(r'\s+', '-', input_string)
input_string = input_string.lower()
input_string = re.sub(r'[-]+', '-', input_string)
input_string = re.sub(r'[^\w-]', '', input_string)
return input_string
# Download data
# - - - - - - - - - - - - - - - - - - - - -
req = Request(baseurl , headers={'User-Agent': 'Mozilla/5.0'}) #approach url cere pagina html
try:
webpage = urlopen(req).read() #approach url obtine pagina hmtl in text (?)
except:
print('error downloading ' + baseurl)
quit()
soup = bs(webpage, "html.parser") #parseaza html text si il aseaza frumos in pagina
content_wrapper = soup.find('div', class_ = 'categories-index')
carneWrapper = soup.find(id="pgprinc")
# div2 = soup.find("div", {"id": "pgprinc"})
carne = carneWrapper.findChildren("div", recursive=False)
# scrape Data
# - - - - - - - - - - - - - - - - - - - - -
soup = bs(str(carne), "html.parser")
zijson = {
'title':'',
'data':{}
}
# gaseste titlu
# then loop through div.
carne1 = soup.findChildren("div", recursive=False)
carne = carne1[0].findChildren("div", recursive=False)
divz1 = soup.findChildren("div", recursive=False)
# title = divz1[0].select("p[class='h5']")
titlez = carne1[0].find("p", {"class": "h2"}).text
zijson['title'] = titlez
rid = 0
for rrow in divz1[0].findAll("div", {"class": "tablerow"}):
zijson['data'][rid] = {}
zijson['data'][rid]['data'] = []
zijson['data'][rid]['title'] = rrow.find("p", {"class": "h5"}).text
# # title = rrow.select("p[class='h5']")
# TODO: check if nodata
alert = rrow.find("div", {"class": "alert"})
if alert:
zijson['data'][rid]['titley'] = alert.text
zijson['data'][rid]['type'] = 'alert'
table = rrow.find("table", {"class": "table"})
if table:
zijson['data'][rid]['type'] = 'table'
# df = pd.read_html(str(table))
zitable = extract_tablez(table)
# TODO: convert zitable list to json
for idx, zrow in enumerate(zitable[1]): # this enumerates too much
zijson['data'][rid]['data'].append(dict(zip(zitable[0], zrow)))
rid+=1
# niceData
# - - - - - - - - - - - - - - - - - - - - -
mbaseurl = "https://andnet.ro/dispecerat/dispecerat.php"
mbaseurl = "https://dispecerat.andnet.ro/"
niceJson = {}
for z,set in zijson['data'].items():
# print(set['title'])
# print(set['type'])
zidata = set['data']
if len(zidata) > 1:
niceJson[z] = {
"name": set['title'],
"data":[]
}
for row in zidata:
nicerow = {}
for kk, vv in row.items():
if kk == 'Vizualizare':
# breakpoint()
ll = vv['attrz']
nn = ll.pop(0)
nn = nn.strip('"')
nicerow['viz_name'] = nn
nicerow['map_zoom'] = ll
# del row[kk]
else:
nicerow[kk] = vv['text']
niceJson[z]['data'].append(nicerow)
# write json to files
# - - - - - - - - - - - - - - - - - - - - -
for ix, dataset in niceJson.items():
# print(dataset['name'])
if dataset['name'] in legend:
table_name = legend[dataset['name']]
else:
table_name = generate_slug(dataset['name'])
# TODO: write json to file
save_json_to_file(dataset['data'],outputJsonRoot + 'json/' + table_name + '.json')
df = pd.read_json(json.dumps(dataset['data']))
df.to_csv(outputJsonRoot + 'csv/' + table_name + '.csv', encoding='utf-8', index=False)
# print(json.dumps(niceJson, ensure_ascii=False))