Created
August 30, 2011 14:16
-
-
Save mmar/1180986 to your computer and use it in GitHub Desktop.
Tweet weather forecast
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 | |
| # Tweet weather forecast | |
| # - gets forecast from Yahoo weather API | |
| # - reads HTTPClient config from ~/.httpclient.yml and Twitter OAuth credentials from ~/.oauth.yml | |
| require 'yaml' | |
| require 'xmlsimple' | |
| require 'httpclient' | |
| require 'oauthclient' | |
| # Edit place as needed | |
| place = 'Lisbon' | |
| yahoo_woeid = '742676' # Get it from the weather.yahoo.com URL for your city, | |
| # e.g. http://weather.yahoo.com/portugal/lisbon/lisbon-742676/ | |
| twitter_place_id = 'c1430b24da8e9229' # See https://dev.twitter.com/docs/api/1/get/geo/search | |
| # You shouldn't have to edit anything from here down | |
| feed = URI "http://weather.yahooapis.com/forecastrss?w=#{yahoo_woeid}&u=c" | |
| twitter = URI 'https://api.twitter.com/1/statuses/update.json' | |
| ua_config = YAML.load_file( "#{ENV['HOME']}/.httpclient.yml" ) | |
| oauth_config = YAML.load_file( "#{ENV['HOME']}/.oauth.yml" ) | |
| ua = HTTPClient.new | |
| ua_config.each { |k,v| ua.send( "#{k}=", v ) } | |
| weather = XmlSimple.xml_in( ua.get_content( feed ), { 'ForceArray' => false } )['channel']['item'] | |
| message = "\u2601 Forecast for #{place} - " + weather['forecast'].map { |f| | |
| "#{f['day']}: #{f['text']}, high #{f['high']}\u2103, low #{f['low']}\u2103" | |
| }.join( ' ; ' ) | |
| ua = OAuthClient.new | |
| ua_config.each { |k,v| ua.send( "#{k}=", v ) } | |
| oauth_config.each { |k,v| ua.oauth_config.send( "#{k}=", v ) } | |
| ua.post twitter, { status: message, place_id: twitter_place_id } | |
| __END__ | |
| # Example .httpclient.yml | |
| protocol_retry_count: 3 | |
| connect_timeout: 120 | |
| receive_timeout: 300 | |
| # Example .oauth.yml | |
| consumer_key : <application-consumer-key> | |
| consumer_secret : <application-consumer-secret> | |
| signature_method : HMAC-SHA1 | |
| token : <oauth-token> | |
| secret : <oauth-secret> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment