Last active
December 26, 2015 22:19
-
-
Save tadejm/7222224 to your computer and use it in GitHub Desktop.
In order to calculate commute time from a given property to work, I created this script, which copies Spareroom property lat,lng system clipboard. Works on os x only. Usage: ruby commute_time.rb -i SPAREROOM_PROPERTY_ID
This file contains hidden or 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
| require "open-uri" | |
| require "nokogiri" | |
| require "optparse" | |
| BASE_URI = "http://www.spareroom.co.uk/flatshare/flatshare_detail.pl?flatshare_id=" | |
| options = {} | |
| optparse = OptionParser.new do |opts| | |
| opts.banner = "Usage: #$0 -i ROOMID" | |
| opts.on "-i", "--id ROOMID", "SPAREROOM room ID" do |room_id| | |
| options[:room_id] = room_id | |
| end | |
| opts.on "-h", "--help", "Show usage" do |help| | |
| warn opts | |
| exit | |
| end | |
| end | |
| optparse.parse! | |
| if options[:room_id].nil? | |
| warn "No room id found" | |
| exit | |
| end | |
| def generate_url_for_room(id) | |
| BASE_URI + id | |
| end | |
| def lat_lng(doc) | |
| lat, lng = doc.search("script")[-3].content.scan(/(-?\d+.\d+)/) | |
| "#{lat.first},#{lng.first}" | |
| end | |
| def pbcopy(input) | |
| str = input.to_s | |
| IO.popen('pbcopy', 'w') { |f| f << str } | |
| str | |
| end | |
| room_url = generate_url_for_room(options[:room_id]) | |
| doc = Nokogiri::HTML(open(room_url)) | |
| pbcopy lat_lng(doc) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment