-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathILC_bot.rb
executable file
·54 lines (46 loc) · 1.35 KB
/
ILC_bot.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
#!/usr/bin/env ruby
#require 'byebug'
require 'yaml'
require 'twitter'
require 'holiday_japan'
def tweets?(date)
return [false, ''] if date.saturday? || date.sunday? || date.national_holiday?
next_friday = date.upto(date + 6).select { |d| d.friday? }.first
tweet_day = next_friday.downto(next_friday - 4).reject { |d| d.national_holiday? }.first
if date == tweet_day
return [true, date.friday? ? '' : '※金曜日は祝日です']
else
[false, '']
end
end
if __FILE__ == $0
conf = YAML.load_file("#{File.dirname(__FILE__)}/config/#{ARGV[0]}.yml")
client = Twitter::REST::Client.new do |c|
c.consumer_key = conf['consumer_key']
c.consumer_secret = conf['consumer_secret']
c.access_token = conf['access_token']
c.access_token_secret = conf['access_token_secret']
end
t, m = tweets?(Date.today)
if t
succeed = false
errors = []
text = ['ILC', 'ILC。', 'ILC!', 'ILC……', 'ILC! ILC!', 'ILCです'][Date.today.yday % 6] + m
3.times do
begin
client.update(text)
rescue Twitter::Error => e
errors << e.to_s
sleep 1
else
succeed = true
break
end
end
# if all retweet attempts failed, put errors to STDERR
unless succeed
STDERR.puts 'All attempts have failed!'
errors.each { |e| STDERR.puts e }
end
end
end