public
Last active

get Roland's Gastown's photos

  • Download Gist
getGastownPhotos.rb
Ruby
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
#!/usr/bin/env ruby
require 'json'
require 'net/http'
require 'pp'
require 'Time'
require 'uri'
require 'parseconfig'
 
flickr_config = ParseConfig.new('flickr.conf').params
api_key = flickr_config['api_key']
user_id = flickr_config['user_id']
 
# get photos taken before september 1, 2010
sep12010 = Time.local(2010, 9, 1, 0, 0) # may want Time.utc if you don't want local time
max_taken_date = sep12010.to_i.to_s
woe_id = "26332810" # Gastown http://www.flickr.com/places/info/26332810
tags = "cameraphone%2Cvancouver"
content_type = "1" # photos only
per_page = "250" # geo photos limited by flickr to only 250 per page
extras="original_format,date_taken,geo,tags,o_dims,views,url_sq,url_t,url_s,url_m,url_o" # get all the meta data!
page = 1
 
 
def getResponse(url)
 
http = Net::HTTP.new("api.flickr.com",80)
 
request = Net::HTTP::Get.new(url)
resp = http.request(request)
if resp.code != "200"
$stderr.puts "Error: #{resp.code} from:#{url}"
raise JSON::ParserError # This is a kludge. Should return a proper exception instead!
end
 
result = JSON.parse(resp.body)
return result
end
 
photos_to_retrieve = 250
first_page = true
photos_per_page = 0
while photos_to_retrieve > 0
search_url = "/services/rest/?method=flickr.photos.search&api_key="+api_key+"&format=json&nojsoncallback=1&content_type="+content_type+"&tags="+tags+"&woe_id="+woe_id+"&per_page="+per_page+"&user_id="+user_id+"&extras="+extras+"&max_taken_date="+max_taken_date+"&page="+page.to_s
photos_on_this_page = getResponse(search_url)
if first_page
first_page = false
photos_per_page = photos_on_this_page["photos"]["perpage"].to_i
photos_to_retrieve = photos_on_this_page["photos"]["total"].to_i - photos_per_page
else
photos_to_retrieve -= photos_per_page
end
page += 1
$stderr.puts photos_on_this_page
print JSON.generate(photos_on_this_page)
end

Please sign in to comment on this gist.

Something went wrong with that request. Please try again.