Class: TweetWatch::CLI

Inherits:
Thor
  • Object
show all
Includes:
Client, Utils
Defined in:
lib/tweet_watch/cli.rb

Instance Method Summary collapse

Methods included from Utils

#escape_str, #load_config, #print_dm, #print_tweet

Methods included from Client

#client, #streaming_client

Instance Method Details

#limitsObject



18
19
20
21
22
23
24
25
26
27
28
29
30
# File 'lib/tweet_watch/cli.rb', line 18

def limits
  load_config(options)
  resp = Twitter::REST::Request.new(client, 'get', "/1.1/application/rate_limit_status.json" ).perform
  
  current_time = Time.now.to_i
  template = "   %-40s %5d remaining, resets in %3d seconds\n"
  resp.body[:resources].each do |category,resources|
    puts category.to_s
    resources.each do |resource,info|
      printf template, resource.to_s, info[:remaining], info[:reset] - current_time
    end
  end
end

#monitorObject



109
110
111
112
113
# File 'lib/tweet_watch/cli.rb', line 109

def monitor      
  load_config(options)
  m = TweetWatch::Monitor.new(options)
  m.run        
end

#timelineObject



37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
# File 'lib/tweet_watch/cli.rb', line 37

def timeline
  load_config(options)
  
  if options[:stream]
    sc = streaming_client(options)
    puts "Starting stream...".colorize(:light_cyan)
    sc.user do |obj|
      if obj.class == Twitter::Tweet
        print_tweet obj
      elsif obj.class == Twitter::DirectMessage
        print_dm obj
      end
    end
  else
    opts = {count: options[:count]}
    client(options).home_timeline(opts).each do |tw|
      print_tweet(tw)
    end
  end 
  
end

#watchObject



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
# File 'lib/tweet_watch/cli.rb', line 66

def watch
  load_config(options)
          
  sc = streaming_client(options)
  file = CSV.open(options[:output], "wb")
  tw_config = TweetWatch.config
  
  tweeters = options[:tweeters] ? options[:tweeters] : tw_config.tweeters
  
  unless File.size(options[:output]) > 0
    file << %W(timelined_at tweet_id screen_name text tweet_created_at is_reply is_quote)
  end
        
  puts "Starting stream...".colorize(:light_cyan)
  sc.user do |obj|
    time = Time.now
    
    if obj.class == Twitter::Tweet          
      if options[:tweeters_only].nil? || (options[:tweeters_only] && tweeters.include?(obj..screen_name))
        print_tweet obj
      end
      
      if tw_config.tweeters.empty? || tw_config.has_tweeter?(obj.user.screen_name)
        puts "recording tweet data".colorize(:red)            
        file << [time.utc, obj.id, obj.user.screen_name, obj.text,obj.created_at.getutc,obj.user.screen_name, obj.reply?, obj.quote?]
        file.flush
      end
    elsif obj.class == Twitter::DirectMessage
      print_dm obj
    elsif obj.class == Twitter::Streaming::StallWarning
      warn "Falling behind!"
    else
      # do nothing for now
      #puts "untracked tweet obj #{obj.class}".colorize(color: :black, background: :light_white)
    end
  
  end
end