Skip to content

Instantly share code, notes, and snippets.

@JEG2
Created August 1, 2012 19:20
Show Gist options
  • Save JEG2/3229912 to your computer and use it in GitHub Desktop.
Save JEG2/3229912 to your computer and use it in GitHub Desktop.
A simple bot for tweeting OGE's daily price signal
require "rest_client"
require "twitter"
URL = "http://www.oge.com/residential-customers/products-and-services/" +
"Positive-Energy-Smart-Grid/Pages/PriceSignal.aspx"
DATE_RE = Time.now.strftime("%A,\\s+%B\\s+%d,\\s+%Y")
unless ARGV.size == 4
abort "USAGE: #{$PROGRAM_NAME} CONSUMER_KEY CONSUMER_SECRET " +
"OAUTH_TOKEN OAUTH_TOKEN_SECRET"
end
# fetch the pricing page
pricing_html = RestClient.post(URL, "SelectedPlanField" => "ResidentialVpp")
prices = { }
# find peak and off-peak pricing
price_row = pricing_html[ %r{ <div\b(?=[^>]*\bclass="PriceRow")[^>]*>
\s*
<div\b(?=[^>]*\bclass="PriceDate")[^>]*>
#{DATE_RE}
</div>
(?<div> (?: [^<]++ |
<div[^>]*+> \g<div> </div> )+ )
</div> }x ]
price_row.scan( %r{ <div\b(?=[^>]*\bclass="PriceLabel")[^>]*>([^<]+)</div>
\s*
<div\b(?=[^>]*\bclass="Price")[^>]*>([^<]+)</div> }x ) do
prices[$1] = $2
end
# find any critical events
pricing_html.scan( %r{ <div\b(?=[^>]*\bclass="EventLabel")[^>]*>
Critical\s+Price\s+Event
</div>
\s*
<div\b(?=[^>]*\bclass="EventDate")[^>]*>
#{DATE_RE}
</div>
\s*
<div\b(?=[^>]*\bclass="EventTime")[^>]*>
([^<]+)
</div>
\s*
<div\b(?=[^>]*\bclass="Price")[^>]*>
([^<]+) }x ) do
prices[$1] = $2
end
# send a tweet
Twitter.configure do |config|
config.consumer_key = ARGV[0]
config.consumer_secret = ARGV[1]
config.oauth_token = ARGV[2]
config.oauth_token_secret = ARGV[3]
end
tweet = "Today's OGE Price Signal: " +
prices.map { |time, price| "#{time}: #{price}" }.join(", ")
Twitter.update(tweet)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment