-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpost-number.rb
51 lines (40 loc) · 1.32 KB
/
post-number.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
require 'twitter'
require 'yt'
# API Keyを取得
client = Twitter::REST::Client.new do |conf|
conf.consumer_key = ENV['TWITTER_CONSUMER_KEY']
conf.consumer_secret = ENV['TWITTER_CONSUMER_SECRET']
conf.access_token = ENV['TWITTER_ACCESS_TOKEN']
conf.access_token_secret = ENV['TWITTER_ACCESS_TOKEN_SECRET']
end
Yt.configuration.api_key = ENV['YOUTUBE_API_KEY']
# configを取得
channel_id = ARGV[0]
# 登録者数取得
channel = Yt::Channel.new(id: channel_id)
num_after = channel.subscriber_count
# 数値がおかしいときは終了
if num_after == 0
puts "ERROR: 登録者数がゼロになっています。ツイートせず終了します。"
exit 1
end
# 前回との差分を取得
list_file = "/var/tmp/number_" + channel_id + ".txt"
num_before = num_after
File.open(list_file, "r") do |f|
num_before = f.read.to_i
end
num_diff = num_after - num_before
#Tweetする
if num_diff <= 0
puts Time.now.strftime("%m/%d %H:%M").to_s + "現在: " + num_after.to_s
else
channel_name = channel.title
item = "YouTubeチャンネル " + channel_name + " の登録者数が " + num_after.to_s + " になりました (" + Time.now.strftime("%m/%d %H:%M").to_s + "現在)"
client.update(item)
puts "[ツイートしました]" + item
# 数字を保存
File.open(list_file, "w") do |f|
f.puts(num_after)
end
end