CommonThread

Twitter Hacking

Posted by ben, Thu Jun 05 13:59:00 UTC 2008

I got overloaded with work the other night so I decided to take a break from it and hack out something fairly useless but fun. I landed on a twitter bot that updates a few times daily with weather updates for my area or others I might care about. Right now I only have a Birmingham Weather and a Montgomery Weather.

The code requires 2 gems twitter & yahoo-weather. you can install them both with:

sudo gem install yahoo-weather twitter --no-ri --no-rdoc

Then the code was very simple for querying and updating:

#! /usr/bin/env ruby

require 'rubygems'
require 'yahoo-weather'
require 'twitter'

client = YahooWeather::Client.new

cities = [['35212', 'wxbhm'], ['36108', 'wxmgm']]

cities.each do |city|
  response = client.lookup_location(city[0])
  forecast = "Today's weather: #{response.forecasts.first.text}, Hi: #{response.forecasts.first.high}F, Lo: #{response.forecasts.first.low}F (Currently #{response.condition.text}, #{response.condition.temp}F)" 
  Twitter::Base.new(city[1], 'SUPER_SECRET').update(forecast)
end

Then to make it happen automagically, welcome cron:

00 13 * * * /home/deploy/twitter_wx/update_wx.rb
45 21 * * * /home/deploy/twitter_wx/update_wx.rb

It updates at 8:00am & 4:45pm. I had to put the dates in UTC and take into consideration that I am Central Timezone and add 5 hours to the time for now … if someone knows a better way to handle this in cron please let me know.

Filed Under: | Tags:

Comments

Have your say

A name is required. You may use HTML in your comments.




Recent Articles

Categories