Permalink
Browse files

Twitter changed to TwitterStream to be more obvious, since there's a …

…new TwitterTimeline for stalking. UserStalker is the parser for that. Also some indentation changes!
  • Loading branch information...
1 parent 3c3c263 commit c293d782153584f512cd106d1c32f78e27186a5b Jake McGinty committed Mar 23, 2012
View
16 app.rb
@@ -9,7 +9,7 @@
stdout_mutex = Mutex.new
class TwitterBeats
- @@debug = :med # PurdyPrint debug var
+ @@debug = :high # PurdyPrint debug var
@@score_bounds = [-10,10]
attr_reader :parsers
@@ -55,18 +55,19 @@ def initialize
textmood_global = TwitterSentiment::Parser::TextMood.new(:afinn_emo)
facerecon_global = TwitterSentiment::Parser::FaceRecon.new
@parsers = {
- :text_mood => { :instance => textmood_global },
- :user_image => { :instance => facerecon_global },
- :user_stats => { :instance => TwitterSentiment::Parser::UserStats.new(textmood_global, facerecon_global) },
- :randomness => { :instance => TwitterSentiment::Parser::Randomness.new },
+ :text_mood => { :instance => textmood_global },
+ :user_image => { :instance => facerecon_global },
+ :user_stats => { :instance => TwitterSentiment::Parser::UserStats.new(textmood_global, facerecon_global) },
+ :randomness => { :instance => TwitterSentiment::Parser::Randomness.new },
+ #:user_stalker => { :instance => TwitterSentiment::Parser::UserStalker.new(textmood_global) },
}
out = TwitterSentiment::Output::Send.new
- TwitterSentiment::Input::Twitter.new({
+ TwitterSentiment::Input::TwitterStream.new({
:status_callback => lambda { |status|
stdout = []
- stdout << fmt(:separator) # separate initialization text from tweet fun
+ stdout << fmt(:separator, '', :med) # separate initialization text from tweet fun
# text weight
stdout << fmt(:info, "#{Paint["TWEET - ",:yellow]}#{status.text}")
@parsers.each do |parser, c|
@@ -86,6 +87,7 @@ def initialize
}
stdout << fmt(weights[:happiness], "h/e/c = #{weights[:happiness]}/#{weights[:excitement]}/#{weights[:randomness]}", :med)
+ stdout.reject! { |msg| msg.nil? or msg.empty? }
puts stdout.join("\n") # mutex-free debug outputs
out.send_gen weights, status, parsers
},
View
2 lib/twitter-sentiment/input/twitter.rb → ...twitter-sentiment/input/twitter_stream.rb
@@ -8,7 +8,7 @@
module TwitterSentiment
module Input
include PurdyPrint
- class Twitter
+ class TwitterStream
def initialize options
default = TwitterSentiment::Prefs::Defaults.twitter
@client = TweetStream::Client.new(TwitterSentiment::Prefs::Secrets.twitter)
View
31 lib/twitter-sentiment/input/twitter_timeline.rb
@@ -0,0 +1,31 @@
+require 'rubygems'
+require 'twitter'
+require 'purdy-print'
+require 'paint'
+require 'fiber'
+require 'twitter-sentiment/prefs/defaults'
+require 'twitter-sentiment/prefs/secrets'
+module TwitterSentiment
+ module Input
+ include PurdyPrint
+ class TwitterTimeline
+ def initialize
+ Twitter.configure do |config|
+ config = TwitterSentiment::Prefs::Secrets.twitter
+ end
+ end
+
+ def for_user user
+ tweets = []
+ page = 0
+ while not (page = Twitter.user_timeline(user, {:count => 200, :page => page})).empty?
+ tweets += page
+ end
+ return tweets
+ rescue Exception => e
+ puts e.message
+ return tweets
+ end
+ end # TwitterTimeline
+ end # Input
+end # twitter-sentiment
View
2 lib/twitter-sentiment/output/send.rb
@@ -50,7 +50,7 @@ def send_gen weights, status, parsers
streamSock.write(payload)
streamSock.close
rescue Exception
- pp :warn, "Failed to send payload across socket.", :low
+ pp :warn, "Failed to send payload across socket.", :med
end
end #Send
end #Output
View
50 lib/twitter-sentiment/parser/user_stalker.rb
@@ -0,0 +1,50 @@
+require 'purdy-print'
+require 'twitter-sentiment/parser/face_recon'
+require 'twitter-sentiment/parser/text_mood'
+Infinity = 1.0/0
+module TwitterSentiment
+ module Parser
+ class UserStalker
+ def initialize textmood=nil
+ pp :info, "User stalker initialized successfully.", :high
+ @timeline = TwitterSentiment::Input::TwitterTimeline.new
+ @textmood = textmood.nil? ? TwitterSentiment::Parser::TextMood.new(:afinn_emo) : textmood
+ end
+
+ def generate_score results
+ sum = results.values.reduce(:+)
+ sum.to_f/results.length
+ end
+
+ #@param [Twitter::User] the user in question
+ #@return data about the user
+ def gather status
+ user = status.user
+ #Call all of the included methods
+ return nil if user.nil?
+
+ tweets = @timeline.for_user user
+ return nil if tweets.nil?
+ sum = 0
+ min = Infinity
+ max = -Infinity
+ tweets.each do |tweet|
+ score = @textmood.score tweet.text
+ sum += score
+ min = score < min ? score : min
+ max = score > max ? score : max
+ end
+
+ pp :debug, "#{tweets.length} tweets parsed in the timeline.", :high
+ #Spit out data in some format
+ return {
+ :tweets => tweets.length,
+ :sum => sum,
+ :min => min,
+ :max => max,
+ :score => sum.to_f / tweets.length
+ }
+ end
+ end #UserStalker
+ end #Parser
+end #TwitterSentiment
View
36 lib/twitter-sentiment/prefs/secrets.rb
@@ -1,20 +1,20 @@
module TwitterSentiment
- module Prefs
- class Secrets
- def self.twitter
- {
- :consumer_key => 'xW42O1MOwxqqFdq7BxqSg',
- :consumer_secret => 'UcBrgpAYiikKIVJw9Rkog0QK2EzJdVcselCbC8HAVmQ',
- :oauth_token => '92918151-Bb5J5q0l9o14vxcLLvUwZwPqra4FKxFUpqE08aNUo',
- :oauth_token_secret => 'cUQtc5NnfwKdSHFJGDX2hog6EijdL1zgQuHioAElY',
- }
- end
- def self.face
- {
- :key => '94dd94ff9e7bcbba27d8cc508c5a3599',
- :secret => '2fd64732016ae293aed3eac7ad340c49',
- }
- end
- end
- end
+ module Prefs
+ class Secrets
+ def self.twitter
+ {
+ :consumer_key => 'xW42O1MOwxqqFdq7BxqSg',
+ :consumer_secret => 'UcBrgpAYiikKIVJw9Rkog0QK2EzJdVcselCbC8HAVmQ',
+ :oauth_token => '92918151-Bb5J5q0l9o14vxcLLvUwZwPqra4FKxFUpqE08aNUo',
+ :oauth_token_secret => 'cUQtc5NnfwKdSHFJGDX2hog6EijdL1zgQuHioAElY',
+ }
+ end
+ def self.face
+ {
+ :key => '94dd94ff9e7bcbba27d8cc508c5a3599',
+ :secret => '2fd64732016ae293aed3eac7ad340c49',
+ }
+ end
+ end
+ end
end

0 comments on commit c293d78

Please sign in to comment.