-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtwits.rb
executable file
·82 lines (68 loc) · 1.72 KB
/
twits.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
#!/usr/bin/env ruby
##
# Quick little script to display all twitter posts based on a search term
# Required gems:
# * twitter
# * htmlentities
# * term-ansicolor
##
require 'rubygems'
require 'twitter'
require 'htmlentities'
require 'term/ansicolor'
require 'active_support'
search = ARGV.join ' OR '
last_seen = 0
start_delay = 5
max_delay = 300
sleep_count = 0
ctrlc_at=Time.now
delay = start_delay
coder = HTMLEntities.new
def usage
puts "Usage: #{$0} <search>"
end
class Color
class << self
include Term::ANSIColor
end
end
if search.nil? or search.empty?
puts "Error: Please specify a search term to display"
usage
exit
end
puts "Showing all \"#{Color.bold search}\" twitter messages"
while(1)
begin
Twitter::Search.new(search).to_a.reverse.each do |r|
next if r.id <= last_seen
delay = start_delay
last_seen = r.id
created_at = Time.parse(r.created_at)
puts <<-EOTXT
|#{Color.bold Color.green r.from_user}| #{coder.decode(r.text)}
|#{Color.bold Color.blue 'Posted'}| #{created_at}
EOTXT
end
sleep delay
delay = (delay * 1.8).to_i unless delay >= max_delay
rescue Interrupt
exit if (Time.now - ctrlc_at) < 5.seconds
puts "Press Ctrl-C again to exit"
# Reset the delay and record when the interrupt was recieved in order to exit if we recieve another signal in a short time
delay = start_delay
ctrlc_at = Time.now
rescue SocketError => e
puts "Network error, will try again in 60 seconds"
sleep 60
rescue Crack::ParseError => e
puts "Error: #{e}"
sleep 10
rescue => e
puts "#{e.class.to_s} encountered: #{e.to_s}"
puts e.backtrace
puts "Sleeping for 60 seconds before trying again"
sleep 60
end
end