Last active
December 15, 2015 01:58
-
-
Save kenmickles/5183335 to your computer and use it in GitHub Desktop.
A Ruby script to connect Jawbone Up to Foursquare.
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
#!/usr/bin/env ruby | |
## | |
# This script searches the (unofficial) Jawbone API for new meal checkins and posts | |
# them to Foursquare. Set your crontab to run it hourly for best results. | |
# | |
# I'm kind of hoping Jawbone adds this functionality to the Up app in the near | |
# future, because, let's be honest, this is pretty kludgy. | |
# | |
# `gem install httparty foursquare2` to begin | |
require 'httparty' | |
require 'foursquare2' | |
# create your own foursquare app (https://foursquare.com/developers/apps) to generate an access token | |
# (or just use mine: http://37i.net/foursquare_oauth.php) | |
foursquare_token = '' | |
# jawbone login credentials | |
jawbone_email = '[email protected]' | |
jawbone_password = '' | |
# log in to jawbone API | |
if (jawbone_token = HTTParty.post("https://jawbone.com/user/signin/login", { query: | |
{ service: "nudge", email: jawbone_email, pwd: jawbone_password } })['token']).nil? | |
puts "Failed to login to Jawbone API" | |
exit | |
end | |
# date can be passed as first argument | |
date = ARGV[0] ? DateTime.parse(ARGV[0]) : DateTime.now | |
# fetch today's meals and filter down to the past hour | |
response = HTTParty.get("https://jawbone.com/nudge/api/v.1.31/users/@me/meals", :query => { :date => date.strftime("%Y%m%d") }, :headers => { "x-nudge-token" => jawbone_token }) | |
recent_meals = (response['data']['items'] || []).select { |m| m['time_completed'].to_i > Time.now.to_i - 3600 } | |
exit if recent_meals.length < 1 | |
# fetch foursquare checkins from the past 12 hours | |
fs = Foursquare2::Client.new(:oauth_token => foursquare_token) | |
todays_checkins = fs.user_checkins(:afterTimestamp => date.to_time.to_i - 43200).items.collect { |c| c.venue.id } | |
recent_meals.each do |meal| | |
# if meal is tagged with a place name other than home, assume it's a foursquare venue | |
if meal['place_name'] and meal['place_type'].downcase != "home" and meal['place_name'].downcase != "home" | |
# search foursquare for the place | |
ll = "#{meal['place_lat']},#{meal['place_lon']}" | |
venue = fs.search_venues(:ll => ll, :query => meal['place_name']).groups.first.items.first rescue nil | |
# if a venue is found and it hasn't been checked into today, do so now | |
if venue and !todays_checkins.include? venue.id | |
fs.add_checkin(:venueId => venue.id, :broadcast => 'public', :ll => ll) | |
end | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment