forked from fcoury/intweet
/
consumer.rb
104 lines (84 loc) · 2.15 KB
/
consumer.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
require 'rubygems'
require 'tweetstream'
require 'daemons'
require 'gmail_sender'
require 'prowl'
require 'redis'
require 'yaml'
require 'config'
require 'ostruct'
require 'logger'
class Array
def joinizzle
return "" if empty?
array = self.clone
last = array.pop
return last if array.empty?
"#{array.join(", ")} and #{last}"
end
end
config = Intweet::Config.new
r = Redis.new
options = {
:dir => "log",
:log_output => true,
:dir_mode => :normal
}
Daemons.send(:run_proc, 'consumer', options) do
while true
puts "Checking delivery queue..."
alerts = []
body = []
terms = []
users = []
total = 0
while tweet_str = r.pop_head('tweets')
tweet = YAML.load(tweet_str)
config.terms.each do |term|
if tweet.text =~ /#{term}/
if total < 10
alerts << term
body << "#{tweet.user.screen_name}: #{tweet.text}\n\n"
users << tweet.user.screen_name
end
total += 1
end
end
end
body << "... and #{total - 10} more ..." if total > 10
if alerts.empty?
puts "Nothing to deliver..."
else
alerts.uniq!
if config.notify_by_email
subject = "[intweet] Alerts for #{alerts.joinizzle}"
puts "Sending: #{subject} with #{total} tweets"
g = GmailSender.new(config.gmail_user, config.gmail_password)
g.send(config.email, subject, body.join(""))
end
if config.notify_by_prowl
str = []
users.uniq.each do |user|
str << user
if str.size == users.size
break
elsif str.size > 3
str << "#{total - 3} more..."
break
end
end
description = "Tweets from: #{str.joinizzle}"
puts "Sending prowl: #{description}"
Prowl.add(
:apikey => "d3e9dcf1f3e276767e4868da841d349c56b17608",
:application => "Intweet Alerts",
:event => "Alerts: #{alerts.joinizzle}",
:description => description
)
end
end
puts "Sleeping..."
sleep config.send_period
puts ""
end
end