-
Notifications
You must be signed in to change notification settings - Fork 0
/
add-neightbourhood-to-csv.rb
executable file
·54 lines (45 loc) · 1.16 KB
/
add-neightbourhood-to-csv.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
52
53
54
#!/usr/bin/env ruby
require 'rubygems'
require 'parseconfig'
require 'pp'
require 'typhoeus'
require 'json'
def getFlickrResponse(url, params)
url = "https://api.flickr.com/" + url
result = Typhoeus::Request.get(url,
:params => params )
return JSON.parse(result.body)
end
flickr_config = ParseConfig.new('flickr.conf').params
api_key = flickr_config['api_key']
# pp api_key
first = true
ARGF.each do |line|
if first
first = false
printf("colour,lat,long,date,neighbourhood\n")
next
end
averagecolour_lat_lon_date = line.chomp
fields = averagecolour_lat_lon_date.split(',')
lat = fields[1]
lon = fields[2]
base_url = "services/rest/"
url_params = {:method => "flickr.places.findByLatLon",
:api_key => api_key,
:format => "json",
:nojsoncallback => "1",
:lat => lat,
:lon => lon
}
woeid_rsp = getFlickrResponse(base_url, url_params)
PP::pp(woeid_rsp, $stderr)
place = woeid_rsp["places"]["place"]
if place == []
$stderr.printf("place is nil, skipping\n")
next
end
woe_name = place[0]["woe_name"]
printf("%s,%s\n", averagecolour_lat_lon_date, woe_name)
sleep(0.5)
end