-
Notifications
You must be signed in to change notification settings - Fork 1
/
input_url.rb
40 lines (37 loc) · 1.08 KB
/
input_url.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
require 'rubygems'
require 'uri'
require 'tokyotyrant'
include TokyoTyrant
require 'yaml'
settings = YAML.load_file("settings.yaml")
path = ARGV.shift
if path =~ /\.(jpg|gif|png)/
rdb = RDBTBL::new
unless rdb.open(settings["tokyotyrant"]["host"].to_s, settings["tokyotyrant"]["port"].to_i)
ecode = rdb.ecode
STDERR.printf("open error: %s\n", rdb.errmsg(ecode))
end
qry = RDBQRY::new(rdb)
qry.addcond("uri", RDBQRY::QCSTREQ, path)
hit = qry.searchget
if hit.size == 0
# 初回投入
value = {"uri" => path, "accessed_at" => Time.now.to_i.to_s, "count"=> "0"}
key = rdb.rnum + 1
unless rdb.put(key, value)
rdb.put(key, value)
ecode = rdb.ecode
STDERR.printf("open error: %s\n", rdb.errmsg(ecode))
end
else
# 既出のURL。カウントアップ
count = hit.fetch("count", "0").to_i + 1
value = {"uri" => path, "accessed_at" => Time.now.to_i.to_s, "count"=>count.to_s}
key = rdb.rnum + 1
unless rdb.put(key, value)
ecode = rdb.ecode
STDERR.printf("open error: %s\n", rdb.errmsg(ecode))
end
end
rdb.close
end