Last active
May 9, 2017 21:57
-
-
Save bhelx/66a392199c310529d0d2 to your computer and use it in GitHub Desktop.
Recursively fetch Craigslist apartments using their map's json api
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
REGION = 'neworleans' | |
BASE_URL = "https://#{REGION}.craigslist.org" | |
parse_listings = lambda do |url| | |
results = JSON.parse Net::HTTP.get(URI(url)) | |
results.first.each do |item| | |
if item.key? 'GeoCluster' | |
parse_listings.call("#{BASE_URL}#{item['url']}") | |
else | |
post_id = item['PostingID'].to_i | |
puts "Listing: #{post_id}" | |
attrs = { | |
title: item['PostingTitle'], | |
price: item['Ask'].to_i, | |
uploaded: DateTime.strptime(item['PostedDate'], '%s'), | |
lat: item['Latitude'], | |
lng: item['Longitude'], | |
bedrooms: item['Bedrooms'].to_i, | |
post_id: post_id | |
} | |
if listing = Listing.unscoped.where(post_id: post_id).first | |
listing.update attrs | |
else | |
Listing.create attrs | |
end | |
end | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment