-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathimport_channels.rb
51 lines (41 loc) · 1.39 KB
/
import_channels.rb
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
require 'cfpropertylist'
require 'json'
require 'open-uri'
base_url = "http://monitoring.krakow.pios.gov.pl"
config_url = base_url + "/dane-pomiarowe/wczytaj-konfiguracje"
html = open(base_url).read
stations_data = nil
html.lines.each do |l|
if l =~ /stations: (\[\{.*\}\])/
stations_data = JSON.parse($1)
break
end
end
if stations_data.nil?
puts "Error: no stations data found"
exit 1
end
data = Net::HTTP.post(URI(config_url), "measType=Auto").body
config = JSON.parse(data)
all_channels = config['config']['channels']
all_stations = config['config']['stations']
stations = all_stations.map { |j|
pm10_channel = all_channels.detect { |c| c['station_id'] == j['id'] && c['param_id'] == 'pm10' }
data = stations_data.detect { |d| d['detailsPath'].end_with?("/#{j['id']}") }
if pm10_channel && data
{
'id' => j['id'],
'name' => j['name'],
'channelId' => pm10_channel['channel_id'],
'lat' => data['position']['lat'],
'lng' => data['position']['lng']
}
else
nil
end
}.compact.sort_by { |j| j['name'].downcase }
output_path = File.expand_path(File.join(__FILE__, '..', '..',
'SmogWatch WatchKit Extension', 'Stations.plist'))
plist = CFPropertyList::List.new
plist.value = CFPropertyList.guess(stations)
plist.save(output_path, CFPropertyList::List::FORMAT_XML, formatted: true)