-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbuild.py
executable file
·391 lines (319 loc) · 11.6 KB
/
build.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
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
#!/usr/bin/env python3
#-*- coding: utf-8 -*-
import os
import sys
import subprocess
import inspect
import multiprocessing
import shutil
import getpass
config = {
'display_name': 'weather',
'logToFile': False,
'path': {
'cwd': os.path.abspath(os.getcwd()),
'bin': os.path.abspath('./build/src/qweather'),
'build': os.path.abspath('./build/'),
},
'cmake_args': '-DCMAKE_PREFIX_PATH=/usr/local/Cellar/qt/5.15.0'
}
def main():
menu = {}
menu['1'] = ['Build release', 'build_release']
menu['2'] = ['Build debug', 'build_debug']
menu['3'] = ['Run desktop', 'run_desktop']
menu['4'] = ['Run mobile', 'run_mobile']
menu['5'] = ['Debug (GDB)', 'debug']
menu['d'] = ['Generate database', 'database']
menu['p'] = ['pkgbuild', 'pkgbuild']
menu['0'] = ['Requirements', 'requirements']
print('\n********************')
print (' {}'.format(config['display_name']))
print('********************')
for item in menu:
print (' ' + item + '. ' + menu[item][0])
selection = input('> ')
# check if in menu
if selection in menu:
eval(menu[selection][1] + '()')
# exec function
if '()' in selection:
eval(selection)
main()
return
def pkgbuild():
log('create .srcinfo')
run('makepkg --printsrcinfo > .SRCINFO')
log('install')
run('makepkg -f')
return
def database():
# pip install bs4
# pip install lxml
# pip install dbf
import json
import requests
from bs4 import BeautifulSoup
import re
import urllib
import dbf
import math
observation_data = [
'http://www.bom.gov.au/sa/observations/saall.shtml',
'http://reg.bom.gov.au/act/observations/canberra.shtml',
'http://www.bom.gov.au/nsw/observations/nswall.shtml',
'http://www.bom.gov.au/nt/observations/ntall.shtml',
'http://www.bom.gov.au/qld/observations/qldall.shtml',
'http://www.bom.gov.au/tas/observations/tasall.shtml',
'http://www.bom.gov.au/vic/observations/vicall.shtml',
'http://www.bom.gov.au/wa/observations/waall.shtml',
]
area_data = [
'ftp://ftp.bom.gov.au/anon/home/adfd/spatial/IDM00013.dbf'
]
# http://www.bom.gov.au/australia/radar/about/radar_site_info.shtml
radar_data = [
'http://www.bom.gov.au/australia/radar/info/nsw_info.shtml',
'http://www.bom.gov.au/australia/radar/info/vic_info.shtml',
'http://www.bom.gov.au/australia/radar/info/qld_info.shtml',
'http://www.bom.gov.au/australia/radar/info/wa_info.shtml',
'http://www.bom.gov.au/australia/radar/info/sa_info.shtml',
'http://www.bom.gov.au/australia/radar/info/tas_info.shtml',
'http://www.bom.gov.au/australia/radar/info/nt_info.shtml',
]
# http://www.bom.gov.au/catalogue/data-feeds.shtml#forecasts
# we only need the precis short form data
# ftp://ftp.bom.gov.au/anon/gen/fwo/[id].xml
forecast_data = [
'IDN11060',
'IDD10207',
'IDQ11295',
'IDS10044',
'IDT16710',
'IDV10753',
'IDW14199',
]
# function to get distance between lat_lon
def get_distance(ax, ay, bx, by):
distance = math.sqrt( ((ax-bx)**2)+((ay-by)**2) )
return distance
# radar site data
radar_site_data = []
lat_lon_pattern = re.compile("([0-9]{1,3}[.][0-9]{1,3})")
for url in radar_data:
page = requests.get(url)
soup = BeautifulSoup(page.text, 'html.parser')
print(url)
#print(soup.prettify())
for site in soup.find_all("div", {"class": "site-info"}):
site_info = []
for item in site.find_all("li"):
text = item.get_text().lower()
if (text.startswith('location')):
match = lat_lon_pattern.findall(text)
site_info.append(float('-' + match[0])) # south is negative
site_info.append(float(match[1]))
if (text.startswith('link')):
link = str(item)
id_start = link.find('/products/') + 10
id_end = link.find('.loop.')
radar_id = link[id_start: id_end]
site_info.append(radar_id)
print(site_info)
radar_site_data.append(site_info)
# area code data
area_code_data = []
db = urllib.request.urlretrieve(area_data[0], 'database.dbf')
#db = requests.get(area_data[0])
table = dbf.Table(db[0]) # table is closed
table.open()
for item in table:
array = [item['aac'].strip(), item['pt_name'].strip(), item['state_code'].strip(), item['lat'], item['lon']]
area_code_data.append(array)
#print(array)
# forecast area data
# add forecast url to the area code data
forecast_area_data = []
for url in forecast_data:
new_url = 'ftp://ftp.bom.gov.au/anon/gen/fwo/{}.xml'.format(url)
source = urllib.request.urlretrieve(new_url, 'xml.xml')
page = open(source[0],'r')
soup = BeautifulSoup(page.read(), 'xml')
print(url)
#print(soup.prettify())
for area in soup.find_all('area'):
if ('aac' in area.attrs and 'type' in area.attrs):
if (area.attrs['type'] == 'metropolitan' or area.attrs['type'] == 'location'):
# convert aac -> lat_lon
aac = area.attrs['aac']
for item in area_code_data:
item_aac = item[0]
if (item_aac == aac):
item.append(url)
forecast_area_data.append(item)
print(item)
print('areas found: {}'.format(len(area_code_data)))
print('forecast found: {}'.format(len(forecast_area_data)))
# observation station data
#detailed_pattern = re.compile('(?<=places\/)(\w*)(?=\/)(?:\/KHEF\/\?location=|\/)(\w*)')
detailed_pattern = re.compile('(?<=places\/)(\w*)(?=\/)(?:\/)(\w*)')
station_data = []
for url in observation_data:
page = requests.get(url)
soup = BeautifulSoup(page.text, 'html.parser')
print(url)
#print(soup.prettify())
# get all links on page
for link in soup.find_all('a'):
a = link.get('href')[1:][:-6].split('/') # trim .shtml and first /
if (a[0] == 'products' and len(a) == 3):
b = str(a[2]).split('.')
id_state = b[0]
id_wmo = b[1]
# for each link, get state and name of station
json = requests.get('http://www.bom.gov.au/fwo/{}/{}.{}.json'.format(id_state, id_state, id_wmo)).json()
header = json['observations']['header']
if (len(header) > 0):
header = header[0]
else:
log('no head {}'.format(id_wmo))
continue
state = header['state_time_zone']
name = header['name']
data = json['observations']['data']
if (len(data) > 0): # may have no data
data = data[0]
else:
log('no data {}'.format(name))
continue
lat = data['lat']
lon = data['lon']
# find the closest forecast station
smallest_distance = 999999
closest_forecast_station = []
for item in forecast_area_data:
d = get_distance(lat, lon, item[3], item[4])
if (d < smallest_distance):
smallest_distance = d
closest_forecast_station = item
forecast_url = closest_forecast_station[5]
aac = closest_forecast_station[0]
# find the closest radar station
smallest_distance = 999999
closest_radar_station = []
for item in radar_site_data:
d = get_distance(lat, lon, item[0], item[1])
if (d < smallest_distance):
smallest_distance = d
closest_radar_station = item
radar_station = closest_radar_station[2]
# detailed forecast info
# http://www.bom.gov.au/places/[state]/[location]/forecast/detailed/
# http://www.bom.gov.au/places/sa/KHEF/?location=Yatala
search_url = 'http://www.bom.gov.au/places/search/?q={}%2C{}'.format(lat, lon)
r = str(requests.get(search_url).url)
match = re.findall(detailed_pattern, r)
'''
if ('location=' in r):
else:
match = re.findall(detailed_pattern_2, r)
'''
detailed = '{}/{}'.format(match[0][0], match[0][1])
array = [id_wmo, id_state, forecast_url, aac, radar_station, lat, lon, state, name, detailed]
station_data.append(array)
print(array)
# write json
with open('database.json', 'w', encoding='utf-8') as f:
f.writelines('{\n')
f.writelines(' "stations" : [\n')
for item in station_data:
f.writelines(' ["{}", "{}", "{}", "{}", "{}", {}, {}, "{}", "{}", "{}"],\n'.format(item[0], item[1], item[2], item[3], item[4], item[5], item[6], item[7], item[8], item[9]))
f.writelines(' ]\n')
f.writelines('}\n')
# to big to use json dump
#json.dump(station_data, f, ensure_ascii=False, indent=4)
return
def run_desktop():
try:
run('''
cd build/src
./qweather
''')
except KeyboardInterrupt:
log('exit')
return
def run_mobile():
try:
run('''
cd build/src
QT_QUICK_CONTROLS_MOBILE=true QT_QUICK_CONTROLS_STYLE=Plasma ./qweather
''')
except KeyboardInterrupt:
log('exit')
return
def debug():
run('''
cd build/src
gdb qweather
''')
return
def build_debug():
run('''
mkdir -p build
cd build
cmake .. {0} -DDEFINE_DEBUG=ON -DCMAKE_BUILD_TYPE=Debug
make
cd ..
'''.format(config['cmake_args']))
return
def build_release():
run('''
mkdir -p build
cd build
cmake .. {0} -DCMAKE_INSTALL_PREFIX=/usr -DCMAKE_BUILD_TYPE=Release
make
sudo make install
'''.format(config['cmake_args']))
return
def requirements():
if sys.platform.startswith('darwin'):
run('brew install cmake')
run('brew install qt5')
if sys.platform.startswith('linux'):
run('yay -S --noconfirm --needed cmake extra-cmake-modules gdb')
return
def log(str=''):
print(str)
if not config['logToFile']:
return
with open("log.txt", "a") as f:
f.write(str + '\n')
return
# run commands
# params:
# cwd
# show cmd
def run(command, params = {}):
# clean command
cmd = inspect.cleandoc(command)
# show output
show_cmd = False
if 'show_cmd' in params:
show_cmd = params['show_cmd']
if show_cmd:
print(cmd + '\n')
working_dir = os.getcwd()
if 'cwd' in params:
working_dir = params['cwd']
# exec
subprocess.run(cmd, shell=True, cwd=working_dir)
return
def run_sudo(command, params = {}):
cmd = inspect.cleandoc(command)
password = getpass.getpass('[sudo] password: ')
cmd = 'echo {}|sudo -S {}'.format(password, cmd)
run(cmd, params)
return
if __name__ == '__main__':
os.system('cls||clear')
main()