Skip to content

Instantly share code, notes, and snippets.

@common-nighthawk
Last active February 3, 2017 20:21
Show Gist options
  • Save common-nighthawk/05c5818d931204589fb688fc60288785 to your computer and use it in GitHub Desktop.
Save common-nighthawk/05c5818d931204589fb688fc60288785 to your computer and use it in GitHub Desktop.
require 'headless' ; require 'httparty' ; require 'watir-webdriver'
$DATETIME = Time.now.utc.strftime("%Y%m%d%H%M")
$DD_USER_ID = 123456789
$G_CITY_ALIAS = "locality"
$G_STATE_ALIAS = "administrative_area_level_1"
class Place
attr_accessor :lat, :lng, :city, :state
def initialize(opts={})
@lat, @lng, @city, @state = opts[:lat], opts[:lng], opts[:city], opts[:state]
end
def to_json
{ city: city, state: state, lat: lat, lng: lng }.to_json
end
end
def google_plus_endpoint(user_id)
"https://plus.google.com/#{user_id}"
end
def location_api_endpoint(place)
"https://maps.googleapis.com/maps/api/geocode/json?latlng=#{place.lat},#{place.lng}&key=HA!YOUWISH^"
end
def database_endpoint(resource='')
"https://location-location-location.firebaseio.com/#{resource}.json?auth=HA!YOUWISH^"
end
def set_headless
headless = Headless.new ; headless.start ; headless
end
def set_browser
Watir::Browser.new(:chrome)
end
def fetch_page(headless, browser)
browser.goto(google_plus_endpoint($DD_USER_ID))
browser.span(text: "About").when_present.click
sleep(5)
blob = browser.body.html
browser.quit
headless.destroy
blob
end
def set_place_coords(place, blob)
lat, lng = blob.match(/q=loc:(-?\d*\.\d*)\+(-?\d*\.\d*)/).captures
place.lat, place.lng = lat, lng
end
def set_place_names(place)
location_details = JSON.parse(HTTParty.get(location_api_endpoint(place)).body)["results"][0]["address_components"]
city = location_details.detect { |l| l["types"].include?($G_CITY_ALIAS) }["long_name"].downcase
state = location_details.detect { |l| l["types"].include?($G_STATE_ALIAS) }["long_name"].downcase
place.city, place.state = city, state
end
def post_location!(place)
system("curl -X PATCH -d '{\"latest\": \"#{$DATETIME}\"}' '#{database_endpoint}'")
system("curl -X PUT -d '#{place.to_json}' '#{database_endpoint('datetimes/'+$DATETIME)}'")
end
my_place = Place.new
headless, browser = set_headless, set_browser
blob = fetch_page(headless, browser)
set_place_coords(my_place, blob)
set_place_names(my_place)
post_location!(my_place)
#while true; do /usr/local/bin/ruby /home/ubuntu/foo.rb; sleep 3600; done &
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment