Skip to content

Instantly share code, notes, and snippets.

@bcelenza
Created August 7, 2012 13:56
Show Gist options
  • Save bcelenza/3285552 to your computer and use it in GitHub Desktop.
Save bcelenza/3285552 to your computer and use it in GitHub Desktop.
#!/usr/bin/env ruby
require 'net/https'
require 'uri'
require 'rubygems'
require 'tweetstream'
require 'mail'
# defaults/config
normal_hours = "R2C2"
all_hours = "R2C3"
# mail configuration
Mail.defaults do
delivery_method :smtp, {
:address => 'smtp.gmail.com',
:port => '587',
:user_name => 'bregan.batphone@gmail.com',
:password => '*******',
:authentication => :plain,
:enable_starttls_auto => true
}
end
# tweetstream configuration
TweetStream.configure do |config|
config.consumer_key = '*****'
config.consumer_secret = '*****'
config.oauth_token = '*****'
config.oauth_token_secret = '*****'
config.auth_method = :oauth
end
client = TweetStream::Daemon.new('bda-batphone')
client.on_error do |message|
puts message
end
client.on_timeline_status do |status|
# determine which list to send the notification to
current_hour = Time.now.hour
notification_cell = all_hours
notification_list = "All Hours"
if current_hour > 15 || current_hour < 3 then
notification_cell = normal_hours
notification_list = "Normal Hours"
end
# authenticate with google docs API
http = Net::HTTP.new('www.google.com', 443)
http.use_ssl = true
path = '/accounts/ClientLogin'
query_string = 'accountType=GOOGLE&Email=*****&Passwd=*****'
headers = { 'Content-Type' => 'application/x-www-form-urlencoded' }
resp = http.post(path, query_string, headers)
cl_string = resp.body[/Auth=(.*)/, 1]
# get the list of email addresses from the spreadsheet
headers["Authorization"] = "GoogleLogin auth=#{cl_string}"
http = Net::HTTP.new('spreadsheets.google.com', 443)
http.use_ssl = true
response = http.get("/feeds/cells/twd7OcXl8o-iP-rkssXPrAQ/od6/private/full/#{notification_cell}", headers)
addresses = response.body.match(/\<content[^\>]+\>([^\<]+)\</)[1].split(',')
#split T-Mobile addresses out to individually send
tmobile_addresses = addresses.collect{ |address|
if address.include? "tmomail.net"
address
end
}.reject{|address| address.nil?}
# remove tmobile addresses from the regular list
non_tmobile_addresses = addresses.reject do |address|
address.include? "tmomail.net"
end
# send the emails (split into chunks of 75, since gmail limits to 100 max)
non_tmobile_addresses.each_slice(75).to_a.each do |address_chunk|
outgoing_mail = Mail.new
outgoing_mail.from = "BDA #{notification_list} <bregan.batphone@gmail.com>"
outgoing_mail.to = "BDA #{notification_list} <bregan.batphone@gmail.com>"
outgoing_mail.bcc = address_chunk
outgoing_mail.subject = status.text
outgoing_mail.deliver
end
# send the emails to the remaining tmobile users
tmobile_addresses.each do |address|
outgoing_mail = Mail.new
outgoing_mail.from = "BDA #{notification_list} <bregan.batphone@gmail.com>"
outgoing_mail.to = address
outgoing_mail.subject = status.text
outgoing_mail.deliver
end
end
client.userstream
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment