Permalink
Browse files

Adding a cron task to update twitter profile location

  • Loading branch information...
1 parent 3f1a856 commit 7a53b39e4455ed779d586fa94974a59c46af32c4 @lstoll committed May 7, 2010
Showing with 71 additions and 40 deletions.
  1. +9 −1 README.md
  2. +8 −0 Rakefile
  3. +4 −39 app.rb
  4. +6 −0 env.rb
  5. +44 −0 lib/location.rb
View
@@ -5,6 +5,14 @@ and makes it available via JSON-P
## Installation
-Runs on heroku. Requires the memcache addon (heroku addons:add memcache:5mb)
+Runs on heroku. Requires the memcache addon
+
+ heroku addons:add memcache:5mb
Also requires the TWITTER\_USERNAME and TWITTER\_PASSWORD environment variables set
+
+ heroku config:add TWITTER_USERNAME=xxxx TWITTER_PASSWORD=xxxx
+
+And if you want it to update your twitter _profile_ location, enable cron
+
+ heroku addons:add cron:daily
View
@@ -0,0 +1,8 @@
+require 'env'
+require 'lib/location'
+
+desc "Cron task - updates twitter profile"
+task :cron do
+ # Update twitter profile location
+ TWITTER_CLIENT.update_profile(:location => stringify_location(get_location, :short))
+end
View
43 app.rb
@@ -1,59 +1,24 @@
require 'sinatra'
require 'rack'
-require 'twitter'
-require 'geokit'
-include Geokit::Geocoders
-require 'memcached'
require 'json'
+require 'lib/location'
configure :development do
Sinatra::Application.reset!
use Rack::Reloader
end
configure do
- CACHE = Memcached.new
-end
-
-def get_location
- begin
- CACHE.get 'lstoll-loc-data'
- rescue Memcached::NotFound
- # If it's more than 8 hrs old, it's expired
- expires_in = 8 * 60 * 60
- puts "Re-loading data from twitter"
- httpauth = Twitter::HTTPAuth.new(ENV['TWITTER_USERNAME'], ENV['TWITTER_PASSWORD'])
- client = Twitter::Base.new(httpauth)
- coords = nil
- updated = nil
- page = 0
- until coords
- if tweet = client.user_timeline(:page => page).detect { |t| t.coordinates }
- coords = tweet.coordinates.coordinates
- updated = tweet.created_at
- end
- page += 1
- end
- # need to switch coord order
- require 'pp'
- loc = GoogleGeocoder.reverse_geocode([coords[1], coords[0]])
- loc_data = { :city => [loc.city, loc.district, 'N/A'].compact[0],
- :state => [loc.state, 'N/A'].compact[0],
- :country => [loc.country, 'N/A'].compact[0],
- :updated => [updated, 'N/A'].compact[0]
- }
- CACHE.set 'lstoll-loc-data', loc_data, expires_in
- loc_data
- end
+ require 'env'
end
get "/" do
loc = get_location
if loc[:city] == 'N/A'
"<html><body><h1>Location Currently Unavailable...</body></html>"
else
- loc_str = "#{loc[:city]}, #{loc[:state]}, #{loc[:country]}"
- redirect "http://maps.google.com/maps?q=#{CGI::escape(loc_str)}&z=6"
+ query = CGI::escape(stringify_location(get_location))
+ redirect "http://maps.google.com/maps?q=#{query}&z=6"
end
end
View
6 env.rb
@@ -0,0 +1,6 @@
+require 'memcached'
+require 'twitter'
+
+CACHE = Memcached.new
+httpauth = Twitter::HTTPAuth.new(ENV['TWITTER_USERNAME'], ENV['TWITTER_PASSWORD'])
+TWITTER_CLIENT = Twitter::Base.new(httpauth)
View
@@ -0,0 +1,44 @@
+require 'twitter'
+require 'geokit'
+
+include Geokit::Geocoders
+
+def get_location
+ begin
+ CACHE.get 'lstoll-loc-data'
+ rescue Memcached::NotFound
+ # If it's more than 8 hrs old, it's expired
+ expires_in = 8 * 60 * 60
+ puts "Re-loading data from twitter"
+ coords = nil
+ updated = nil
+ page = 0
+ until coords
+ if tweet = TWITTER_CLIENT.user_timeline(:page => page).detect do
+ |t| t.coordinates
+ end
+ coords = tweet.coordinates.coordinates
+ updated = tweet.created_at
+ end
+ page += 1
+ end
+ # need to switch coord order
+ require 'pp'
+ loc = GoogleGeocoder.reverse_geocode([coords[1], coords[0]])
+ loc_data = { :city => [loc.city, loc.district, 'N/A'].compact[0],
+ :state => [loc.state, 'N/A'].compact[0],
+ :country => [loc.country, 'N/A'].compact[0],
+ :updated => [updated, 'N/A'].compact[0]
+ }
+ CACHE.set 'lstoll-loc-data', loc_data, expires_in
+ loc_data
+ end
+end
+
+def stringify_location(loc, type=:long)
+ if type == :short
+ "#{loc[:city]}, #{loc[:country]}"
+ else
+ "#{loc[:city]}, #{loc[:state]}, #{loc[:country]}"
+ end
+end

0 comments on commit 7a53b39

Please sign in to comment.