-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathclock.rb
67 lines (56 loc) · 1.68 KB
/
clock.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
require 'rubygems'
require 'clockwork'
require 'json'
require 'twitter'
require 'redis'
include Clockwork
include Twitter
include JSON
# init REDIS
uri = URI.parse(ENV['REDISTOGO_URL'])
redis = Redis.new(:host => uri.host, :port => uri.port, :password => uri.password)
# Configure Twitter Credentials
Twitter.configure do |c|
c.consumer_key = ENV['CONSUMER_KEY']
c.consumer_secret = ENV['CONSUMER_SECRET']
c.oauth_token = ENV['OAUTH_TOKEN']
c.oauth_token_secret = ENV['OAUTH_TOKEN_SECRET']
end
# Initialize your Twitter client
client = Twitter::Client.new
handler do |job|
# target tag
tag = ENV["TAG"]
# target user
user = ENV['USER']
# target feed
list = ENV['LIST']
# include entities to checking hashtags
opts = {:include_entities => true}
# add since after first fetch to avoid duplicate posting
since_id = redis.get('since_id')
opts = opts.merge({:since_id => since_id}) if since_id
# gel list tweets
list = Twitter.list_timeline(user,list,opts)
# integer to store tweet order num
i = 0
# loop in tweets
list.each do |t|
# dive if it has hashtags
unless t.attrs["entities"].empty? && t.attrs["entities"]["hashtags"].empty?
# collect tweets with target hastag
htag = ""
t.attrs["entities"]["hashtags"].keep_if{|ht| htag = ht["text"]; ht["text"].downcase == tag }.each do |gt|
# save since id
redis.set('since_id',t.id) if i==0
# increment counter! important
i+=1
# remove tag and retweet it on target account
client.update(t.text.gsub("##{htag}",""))
# puts t.text.gsub("##{htag}","")
end
end
end
end
# do it every 25 seconds
every(25.seconds, 'clone_tweets')