-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathmass_follow.rb
61 lines (44 loc) · 1.53 KB
/
mass_follow.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
require File.expand_path('../api_credentials', __FILE__)
require 'twitter'
require 'optparse'
Twitter.configure do |config|
config.consumer_key = TwitterAPICredentials.CONSUMER_KEY
config.consumer_secret = TwitterAPICredentials.CONSUMER_SECRET
config.oauth_token = TwitterAPICredentials.OAUTH_TOKEN
config.oauth_token_secret = TwitterAPICredentials.OAUTH_SECRET
end
options = {}
OptionParser.new do |opts|
opts.banner = "Usage: mass_follow.rb [options]"
opts.on("-t", "--term TERM", "Search Term") do |term|
options[:search_term] = term
end
opts.on("-s", "--start [NUM]", "Start Page") do |start|
options[:start_page] = start.to_i
end
opts.on("-m", "--max-pages [NUM]", "Max Pages") do |max|
options[:max_pages] = max.to_i
end
end.parse!
options[:start_page] = 1 if !options[:start_page]
options[:max_pages] = 10 if !options[:max_pages]
if !options[:search_term]
puts "You must provide a search term (preceded by the '-t' or '--term' flag)"
exit
end
page_fetch_count = 0
cur_page = options[:start_page]
while page_fetch_count < options[:max_pages]
puts "searching \"#{options[:search_term]}\", page #{cur_page}"
users = Twitter.user_search(options[:search_term], :page => cur_page)
users.each do |user|
begin
Twitter.follow(user)
puts "following #{user.attrs["screen_name"]}"
rescue Twitter::Error::Forbidden => e
puts "warning: error raised attempting to follow user\"#{user.attrs['screen_name']}\": #{e.message}"
end
end
cur_page += 1
page_fetch_count += 1
end