Skip to content

Instantly share code, notes, and snippets.

@craigeley
Last active December 15, 2015 02:14
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save craigeley/119d3f5a74417717bc16 to your computer and use it in GitHub Desktop.
Save craigeley/119d3f5a74417717bc16 to your computer and use it in GitHub Desktop.
Best used with Hazel to grabs location data from the Foursqure API after an IFTTT-generated file changes in your Dropbox.
#!/usr/bin/ruby
# encoding: utf-8
# Grab Foursquare location data from an IFTTT-generated text file
# Because of the Grep searches, this will work best with this recipe: https://ifttt.com/recipes/187849
# Change line 15 with the location of your IFTTT file
# Make sure to change line 25 with your personal client ID and secret generated by Foursquare
# Once you have the data, you can output or write it however you like.
require 'open-uri'
require 'net/http'
require 'json'
require 'time'
line = File.open('/Users/USERNAME/Dropbox/Data/places.txt', encoding: 'UTF-8') {|f| f.readlines[-1].strip}
/-\s(?<link>http.*)/ =~ line
/^(?<time>.*?)\s-/ =~line
uri = URI(link)
data = Net::HTTP.get_response(uri).to_hash
longURL = data["location"].join
/\/(?<venueID>\w*)$/ =~ longURL
url = "https://api.foursquare.com/v2/venues/"+venueID+"?client_id=#INSERT_YOUR_CLIENTID_HERE&client_secret=INSERT_YOUR_CLIENTSECRET_HERE&v=20130815"
buffer = open(url).read
data = JSON.parse(buffer)
name = data["response"]["venue"]["name"]
zip = data["response"]["venue"]["location"]["postalCode"]
city = data["response"]["venue"]["location"]["city"]
state = data["response"]["venue"]["location"]["state"]
country = data["response"]["venue"]["location"]["country"]
lat = data["response"]["venue"]["location"]["lat"]
long = data["response"]["venue"]["location"]["lng"]
Puts = "#{name}|#{zip}|#{city}|#{state}|#{country}|#{lat}|#{long}"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment