-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlottoTweet.rb
executable file
·139 lines (113 loc) · 4.41 KB
/
lottoTweet.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
#!/usr/bin/env ruby
require 'yaml'
require 'rubygems'
require 'httpclient'
require 'optparse'
## grab the config file
config= YAML::load(File.open('./config.yaml'))
class Float
def round_to(x)
(self * 10**x).round.to_f / 10**x
end
end
class Numeric
def millions()
res= self % 1000000
res == 0 ? (self / 1000000) : ((1.0 * self) / 1000000).round_to(1);
end
end
def logtime
## return a date like "11SEP2009.2044h26s"
upcaseMonth= Time.now.strftime("%b").upcase
Time.now.strftime("%d#{upcaseMonth}%Y.%H%Mh%Ss")
end
# default options
OPTIONS = {
:force => false,
:noupdate => false,
}
ARGV.options do |o|
script_name = File.basename($0)
o.set_summary_indent(' ')
o.banner = "Usage: #{script_name} [-fn]"
o.define_head " Update twitter with current lottery jackpot totals"
o.separator ""
o.on("-f", "--force", "Force a cache update") { |OPTIONS[:force]| }
o.on("-n", "--noupdate", "Do not update twitter") { |OPTIONS[:noupdate]| }
o.separator ""
o.on_tail("-h", "--help", "Show this help message.") { puts o; exit }
o.parse!
end
########################################################################
##
## grab the superlotto page from the lotto site
url = "http://www.calottery.com/games/superlottoplus/"
client = HTTPClient.new
resp = client.get(url)
## extract current values. this section is subject to the whims of
## the calottery site. Things here can change unpredictably. If I
## have to change this even once, I will probably start using the
## WWW::Mechanize gem instead.
current={}
current['Jackpot']= resp.content.gsub(/.*id="GameLargeImageBanner1_lblCurJackpot"[^0-9*]*([0-9,]*).*/m, '\1').gsub(/,/, '')
unless (Integer(current['Jackpot']) rescue false)
puts "#{logtime()} non integer value for Jackpot: #{current['Jackpot']}"
exit
end
current['CashValue']= resp.content.gsub(/.*id="GameLargeImageBanner1_lblSLPEstCashValue"[^>]*>\$*([^<]*)<.*/m, '\1').gsub(/,/, '')
unless (Integer(current['CashValue']) rescue false)
if (current['CashValue'] !~ /available/i)
puts "#{logtime()} non integer value for CashValue: #{current['CashValue']}"
exit
end
end
current['DrawDate']= resp.content.gsub(/.*id="GameLargeImageBanner1_lblSLCJPTDate"[^0-9]*([0-9\/]*).*/m, '\1').gsub(/,/, '')
## grab the values cached from the last run. These are the values
## that were last tweeted.
cached= YAML::load(File.open('./savedItems.yaml'))
cv= current['CashValue'].downcase;
if (current['CashValue'] !~ /available/i)
cv= "around $#{current['CashValue'].to_i.millions} million"
end
current['Message']= "Amanda says: the California Lottery drawing on #{current['DrawDate']} jackpot is about $#{current['Jackpot'].to_i.millions} million. Cash value is #{cv}."
changed= (current['Message'] != cached['Message'])
if (changed || OPTIONS[:force])
require 'twitter'
## Cache the new values.
File.open("./savedItems.yaml", 'w') { |f| f.puts current.to_yaml } if changed
if (OPTIONS[:noupdate])
puts "#{logtime()} --noupdate option was set. no post to twitter"
else
begin
consumer = OAuth::Consumer.new(config['consumer_token'], config['consumer_secret'],
{:site => 'http://twitter.com'})
request_token = consumer.get_request_token
response = Net::HTTP.post_form(URI.parse('http://twitter.com/oauth/authorize'),
{"session[username_or_email]" => config['login'],
"session[password]" => config['password'],
"oauth_token" => request_token.token})
response.body =~ /<div id=\"oauth_pin\">\s*(\d+)\s*</
pin = $1
oauth = Twitter::OAuth.new(config['consumer_token'], config['consumer_secret'])
oauth.authorize_from_request(request_token.token, request_token.secret, pin)
rescue Exception => authException
## log auth error
puts "Authorization exceprion" + authException.message
puts e.backtrace.inspect
else
begin
client = Twitter::Base.new(oauth)
client.update(current['Message'])
rescue Exception => postException
## log message post error
puts "Message post exception" + postException.message
else
## log success
puts "#{logtime()} TWEET MSG ==>: '#{current['Message']}'"
end
end
end
else
## log nothing changed
puts "#{logtime()} No change, cached values are unmodified"
end