Create a gist now

Instantly share code, notes, and snippets.

What would you like to do?
This gist checks you into your most recent Foursquare location from the Reporter iOS app, if you aren't already checked in there. To read more, see http://verifyandrepair.com/02-26-2016/using-reporter-foursquare/
#!/usr/bin/env ruby
# Use Reporter as a Foursquare Client
# Assumes your Reports contain these two questions:
# "What are you doing?" and
# "Where are you?"
require 'time'
require 'json'
require 'net/http'
# To prevent encoding errors on OSX
if RUBY_VERSION =~ /2.*.*/
Encoding.default_external = Encoding::UTF_8
Encoding.default_internal = Encoding::UTF_8
end
# Set Variables - Path to Dropbox; Foursquare token; script variables
filepath = "/Users/USERNAME/Dropbox/Apps/Reporter-App/"
token = "FOURSQUARE_API_TOKEN"
new_checkin = false
id = ''
# Get the current file and parse it
datepath = Time.now.strftime("%Y-%m-%d")
file = File.open(filepath+datepath+"-reporter-export.json", encoding: 'UTF-8')
json = file.read
data = JSON.parse(json)
# Grab the data - default answers for sleep and wake, user input for everything else
imp = data["snapshots"][-1]["reportImpetus"].to_i
if imp == 3
act = "Heading to bed"
elsif imp == 4
act = "Waking up"
else
data["snapshots"][-1]["responses"].each do |response|
lat = data["snapshots"][-1]["location"]["latitude"]
long = data["snapshots"][-1]["location"]["longitude"]
ll = "#{lat},#{long}"
if response["questionPrompt"] =~ /doing/
act = response["tokens"][0]["text"]
elsif response["questionPrompt"] =~ /Where/ && response["locationResponse"] != nil
location = response["locationResponse"]["text"]
url = URI.parse("https://api.foursquare.com/v2/users/self/checkins?limit=1&oauth_token=" + token + "&v=20140823")
buffer = Net::HTTP.get(url)
checkins = JSON.parse(buffer)
place = checkins["response"]["checkins"]["items"][0]["venue"]["name"]
if place == location
puts "You're already here!"
elsif place != location
venueURI = URI.escape("https://api.foursquare.com/v2/venues/search?oauth_token=#{token}&ll=#{ll}&query=#{location}&limit=1&v=20140823")
venueURI = URI.parse(venueURI)
res = Net::HTTP.get(venueURI)
venues = JSON.parse(res)
id = venues["response"]["venues"][0]["id"]
new_checkin = true
end
end
end
end
if new_checkin == true
uri = URI('https://api.foursquare.com/v2/checkins/add?')
http = Net::HTTP.new(uri.host, uri.port)
http.use_ssl = true
http.verify_mode = OpenSSL::SSL::VERIFY_PEER
data = {
"venueId" => "#{id}",
"shout" => "#{act}",
"oauth_token" => "#{token}",
"v" => "20160222",
"broadcast" => "private",
}
body = URI.encode_www_form(data)
req = Net::HTTP::Post.new(uri)
req.add_field "Content-Type", "application/x-www-form-urlencoded"
req.body = body
res = http.request(req)
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment