Browse files

Prowl integration

  • Loading branch information...
1 parent 28828b0 commit d4ea4acb16dd18bbd390d83caf5f1d73f5bd61ef @fcoury committed Jan 23, 2010
Showing with 94 additions and 41 deletions.
  1. +2 −1 .gitignore
  2. +10 −0 config.yml.example
  3. +79 −32 consumer.rb
  4. +3 −8 producer.rb
View
3 .gitignore
@@ -1,3 +1,4 @@
+log/*
+*.log
*.pid
config.yml
-*.log
View
10 config.yml.example
@@ -0,0 +1,10 @@
+terms: ['webbynode', 'fcoury']
+twitter_user: twit
+twitter_password: mypass
+notify_by_prowl: true
+prowl_apikey: youasbjdahsdabdhiadbasdbas
+notify_by_email: true
+email: user@me.com
+gmail_user: user
+gmail_password: pass
+send_period: 60
View
111 consumer.rb
@@ -2,60 +2,107 @@
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
+
begin
config = Intweet::Config.new
r = Redis.new
- Daemons.run_proc('consumer', :multiple => true) do
- logger = Logger.new("consumer-#{$$}.log", "daily")
- logger.info "Checking delivery queue..."
+ options = {
+ :dir => "log",
+ :log_output => true,
+ :dir_mode => :normal
+ }
- alerts = []
- body = []
- terms = []
- total = 0
+ Daemons.run_proc('consumer', options) do
+ while true
+ puts "Checking delivery queue..."
- while tweet_str = r.pop_head('tweets')
- tweet = YAML.load(tweet_str)
+ alerts = []
+ body = []
+ terms = []
+ users = []
+ total = 0
- config.terms.each do |term|
- if tweet.text =~ /#{term}/
- if total < 10
- alerts << term
- body << "#{tweet.user.screen_name}: #{tweet.text}\n\n"
- end
+ while tweet_str = r.pop_head('tweets')
+ tweet = YAML.load(tweet_str)
- total += 1
+ 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
- end
-
- body << "... and #{total - 10} more ..." if total > 10
- if alerts.empty?
- logger.info "Nothing to deliver..."
- else
- alerts.uniq!
+ body << "... and #{total - 10} more ..." if total > 10
- subject = "[intweet] Alerts for #{alerts.join(" ")}"
+ if alerts.empty?
+ ThisLogger.log.info "Nothing to deliver..."
+ else
+ alerts.uniq!
- logger.info "Sending: #{subject} with #{total} tweets"
+ 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.each do |user|
+ str << user
+ if str.size == users.size
+ break
+ elsif str.size > 3
+ str << "#{total - 3} more..."
+ break
+ end
+ end
- g = GmailSender.new(config.gmail_user, config.gmail_password)
- g.send(config.email, subject, body.join(""))
- end
+ description = "Tweets from: #{str.joinizlle}"
- logger.info "Sleeping..."
- sleep config.send_period
- logger.info ""
+ 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
rescue
- logger.error $!
+ puts $!
end
View
11 producer.rb
@@ -3,15 +3,10 @@
require 'redis'
require 'yaml'
require 'config'
-require 'logger'
-
-config = Intweet::Config.new
-logger = Logger.new("producer.log", "daily")
-
-logger.info "Started. Monitoring terms: #{config.terms.inspect}"
+c = Intweet::Config.new
r = Redis.new
-TweetStream::Daemon.new('fcoury', 'tempra13twit').track(config.terms) do |status|
- logger.info "Found: #{status}"
+
+TweetStream::Daemon.new(c.twitter_user, c.twitter_password).track(*c.terms) do |status|
r.push_tail 'tweets', status.to_yaml
end

0 comments on commit d4ea4ac

Please sign in to comment.