-
Notifications
You must be signed in to change notification settings - Fork 0
/
check_do_rt.rb
171 lines (143 loc) · 4.19 KB
/
check_do_rt.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
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
#! ruby -Ku
# -*- coding: utf-8 -*-
require 'bundler'
Bundler.require
require_relative 'rtconfig.rb'
require_relative 'userconfig.rb'
require_relative 'tweetdb.rb'
require_relative 'retweetdb.rb'
require 'date'
require 'rexml/document'
require 'uri'
require 'net/http'
## Create Initial Data
dir = File.expand_path(File.dirname($0))
config, cu, userconf = UserConfig.config(dir, ARGV[0])
db_dir = dir + '/db/'
ct = config['token']
# 下準備
consumer = OAuth::Consumer.new(
ct['consumer_key'],
ct['consumer_secret'],
site: 'https://twitter.com'
)
access_token = OAuth::AccessToken.new(
consumer,
cu['access_token'],
cu['access_token_secret']
)
TWEET_DB = "#{db_dir}tweetdb_#{userconf}.txt"
RT_DB = "#{db_dir}rtdb_#{userconf}.txt"
def expand_url(url)
uri = url.is_a?(URI) ? url : URI.parse(url)
out = ''
begin
Net::HTTP.start(uri.host, uri.port) do |io|
r = io.head(uri.path)
out = r['Location'] || uri.to_s
end
rescue => ex
puts ex.to_s
rescue Timeout::Error => ex
retry
end
out
end
# DBオープン
tdb = TweetDb.new(TWEET_DB)
# DBオープン
rdb = RetweetDb.new(RT_DB)
#configオープン
rtconfig = RtConfig.config(dir)
# statusからURLを取り出す
tdb.records.each do |record|
sleep(0.2)
# 調査対象URL抜き出し
urlstrs = record[:status].scan(/http.+html/)
# livedoor経由のtwitterポストが短縮URLになってこうしないととれないことがある
if urlstrs.length == 0
urlstrs = record[:status].scan(%r{http://lb.to/[a-zA-Z0-9]+})
end
if urlstrs.length == 0
urlstrs = record[:status].scan(%r{http://t.co/[a-zA-Z0-9]+})
end
# URLが抜き出せなかったら無視
if urlstrs.length == 0
# 抜き出せなかった
STDERR.puts 'url not match!'
STDERR.puts record[:status]
next
end
# 短縮URL伸張
targeturl = expand_url(urlstrs[0])
expanded = targeturl.scan(%r{http://lb.to/[a-zA-Z0-9]+})
if (expanded.length != 0)
targeturl = expand_url(expanded[0])
end
# tweet数取得API準備
urlstr = '/1/urls/count.json?url=' + URI.encode(targeturl)
Net::HTTP.version_1_2 # おまじない
Net::HTTP.start('cdn.api.twitter.com', 443, use_ssl: true) do |http|
#error が帰ってきたら3秒waitを入れて5度tryする
response = nil
5.times do |t|
begin
response = http.get(urlstr, 'Connection' => 'Keep-Alive')
if response.code == '200'
break
else
STDOUT.puts "#{urlstr} failed.(code = #{response.code}) #{t} try."
STDOUT.puts response
end
sleep(3)
rescue
STDERR.puts "#{urlstr} failed. throwed '#{$!}'"
end
end
# それでもエラーだったらスキップする
if response.code != '200'
STDERR.puts "cannot call cdn.api.twitter.com! url = #{urlstr} code = #{response.code}"
break
end
response_str = response.body.chomp
puts "#{urlstr} count is #{response_str}."
parsed = JSON.parse(response_str)
rt_count = parsed["count"].to_i
if rdb.rt[record[:id]].nil?
puts "new record id:#{record[:id]} count: #{rt_count} status: 0"
rdb.rt[record[:id]] = {
id: record[:id],
rt_count: rt_count,
rt_count_new: rt_count,
rt_status: 0
}
else
print "update record id:#{record[:id]} "
print "count: #{rt_count}(#{rdb.rt[record[:id]][:rt_count]}) "
puts "status: #{rdb.rt[record[:id]][:rt_status]}"
rdb.rt[record[:id]][:rt_count_new] = rt_count
end
twitstring = nil
kiriban = rtconfig['kiriban'].sort do |v1, v2|
v2['status'] <=> v1['status']
end
kiriban.each do |k|
if rt_count >= k['count']
if rdb.rt[record[:id]].nil? || rdb.rt[record[:id]][:rt_status] <= k['status'] - 1
twitstring = "#{k['head']} #{record[:status]} #{k['foot']}"
rdb.rt[record[:id]][:rt_status] = k['status']
end
break
end
end
unless twitstring.nil?
blog_title = /#{config['basic']['title']}/
twitresult = access_token.post(
'https://api.twitter.com/1.1/statuses/update.json',
status: twitstring.sub(blog_title, '')
)
puts twitresult.body
end
end
end
rdb.save(RT_DB)