-
Notifications
You must be signed in to change notification settings - Fork 0
/
extractor.rb
39 lines (32 loc) · 985 Bytes
/
extractor.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
require 'net/http'
require 'json'
latitude = 57.165746
longitude = -2.102081
range = 20 # km
url = "http://api.luftdaten.info/v1/filter/area=#{latitude},#{longitude},#{range}"
uri = URI(url)
response = Net::HTTP.get(uri)
objects = JSON.parse(response)
locations = []
location_hashes = {}
objects.each do |object|
sensor_data = object["sensordatavalues"]
location_data = object["location"]
id = location_data["id"]
location = location_hashes[id] || { "id": id }
location["timestamp"] = object["timestamp"]
location["latitude"] = location_data["latitude"]
location["longitude"] = location_data["longitude"]
location["altitude"] = location_data["altitude"]
sensor_data.each do |sensor|
location[sensor["value_type"]] = sensor["value"]
end
location_hashes[id] = location
end
location_hashes.each_value do |value|
locations << value
end
full_model_to_export = { "locations": locations }
File.open("extracted.json", "w") do |f|
f.write(full_model_to_export.to_json)
end