forked from mroth/pigstream
-
Notifications
You must be signed in to change notification settings - Fork 0
/
pig.rb
78 lines (69 loc) · 1.97 KB
/
pig.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
#!/usr/bin/env ruby
require 'tweetstream'
require 'oj'
require 'twitter'
require 'colored'
#
# secret tokens: load from environment variables
#
CONSUMER_KEY = ENV['CONSUMER_KEY']
CONSUMER_SECRET = ENV['CONSUMER_SECRET']
OAUTH_TOKEN = ENV['OAUTH_TOKEN']
OAUTH_TOKEN_SECRET = ENV['OAUTH_TOKEN_SECRET']
#
# configure tweetstream instance
#
TweetStream.configure do |config|
config.consumer_key = CONSUMER_KEY
config.consumer_secret = CONSUMER_SECRET
config.oauth_token = OAUTH_TOKEN
config.oauth_token_secret = OAUTH_TOKEN_SECRET
config.auth_method = :oauth
config.parser = :oj
end
#
# configure twitter gem instance
#
Twitter.configure do |config|
config.consumer_key = CONSUMER_KEY
config.consumer_secret = CONSUMER_SECRET
config.oauth_token = OAUTH_TOKEN
config.oauth_token_secret = OAUTH_TOKEN_SECRET
end
#
# set up an array of terms we will track twitter for
#
TERMS = ["beat level angry birds cant", "beat level angry birds can't", "beat level angry birds cannot"]
#
# method to generate the pig's snort msg
#
def snortle
phrases=["oink", "snort", "heh heh"]
snort = ""
rand(3..8).times do
snort += phrases.sample.upcase + " "
end
snort += "HEH HEH HEH" #always end with laugh
end
#
# instantiate client and main logic methods
#
@client = TweetStream::Client.new
@client.on_error do |message|
puts "ERROR: #{message}"
end
#
# when we see a matching term, tweet a response
# (this is wrapped in a bunch of formatting to have pretty colored logs)
#
$stdout.sync = true
LIVE = true
@client.track(TERMS) do |status|
puts " ** #{status.user.screen_name} ".green + status.text.white + " (\##{status.id.to_s})"
msg = "@#{status.user.screen_name} #{snortle}..."
puts " -> ".red + msg
if LIVE
response = Twitter.update(msg, :in_reply_to_status_id => status.id)
puts " -> ".red + "posted as http://twitter.com/#{response.user.screen_name}/status/#{response.id.to_s}"
end
end