Skip to content

Instantly share code, notes, and snippets.

@superfeedr
Created July 20, 2010 05:04
Show Gist options
  • Save superfeedr/482554 to your computer and use it in GitHub Desktop.
Save superfeedr/482554 to your computer and use it in GitHub Desktop.
require 'rubygems'
require 'simplegeo' # gem install sg-ruby
require 'nokogiri' # gem install nokogiri
require 'typhoeus' # gem install typhoeus
require 'sinatra' # gem install sinatra
##
# About:
# This little script, sacca.rb allows you to save a gowalla user's checkins to a layer on SimpleGeo.
# it's using PubSubHubbub (Gowalla's hub is at http://hub.gowalla.com/) to subscribe to the Atom feed
# of this user. Whenever the user checks in, a notification is sent, and the data is saved to SimpleGeo.
##
# To start : ruby sacca.rb
# Then, in yoru browser, go to http://$host:$port/start
# Then, checkin!
# Don't forget to change these params :)
$sg_token = 'token'
$sg_secret = 'secret'
$gowalla_user = 'juliuseren'
$sg_layer = "com.gowalla.#{$gowalla_user}"
$port = 4568
$host = 'host.tld'
set :port, $port
configure do
SimpleGeo::Client.set_credentials($sg_token, $sg_secret)
end
##
# Accepts subscriptions vcerification and unsubscription verification
# See PubSubHubub spec http://pubsubhubbub.googlecode.com/svn/trunk/pubsubhubbub-core-0.3.html#verifysub
get '/' do
if params['hub.topic'] == "http://gowalla.com/users/#{$gowalla_user}/checkins.atom"
params['hub.challenge'] # confirmed subscription
else
"" # We don't want to confirm subscription
end
end
##
# Called upon checkin. See http://pubsubhubbub.googlecode.com/svn/trunk/pubsubhubbub-core-0.3.html#contentdistribution
post '/' do
# Parse the ATOM
xml = Nokogiri::XML(request.body.read)
xml.xpath("/atom:feed/atom:entry", {"atom" => "http://www.w3.org/2005/Atom"}).each do |entry|
point = entry.at_xpath("./geo:point", {"geo" => "http://www.georss.org/georss"})
if !point.nil?
title = entry.at_xpath("./atom:title", {"atom" => "http://www.w3.org/2005/Atom"}).text
link = entry.at_xpath("./atom:link[@rel='alternate']", {"atom" => "http://www.w3.org/2005/Atom"})["href"]
id = entry.at_xpath("./atom:id", {"atom" => "http://www.w3.org/2005/Atom"}).text
lat, lon = point.text.split(" ")
record = SimpleGeo::Record.new({
:id => id,
:created => Time.now,
:lat => lat,
:lon => lon,
:layer => $sg_layer,
:properties => {
:title => title,
:link => link
}
})
puts "Saving #{id} : #{lat}, #{lon} (#{title} => #{link})"
SimpleGeo::Client.add_record(record)
end
end
end
get '/start' do
# Perform Susbcription to the feed.
res = Typhoeus::Request.post("http://hub.gowalla.com/", :params => {
:"hub.topic" => "http://gowalla.com/users/#{gowalla_user}/checkins.atom",
:"hub.callback" => "http://#{host}:#{port}/",
:"hub.mode" => "subscribe",
:"hub.verify" => "sync"})
if res.code == 204
puts "App is now subscribed to http://gowalla.com/users/#{gowalla_user}/checkins.atom, please checkin on gowalla!"
else
puts "App couldn't be checked in. Please make sure your app is accessible from outside (not behind a firewall)"
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment