rubyhexagon 0.0.1 → 0.0.5
Sign up to get free protection for your applications and to get access to all the features.
- data/bin/e621_server +116 -89
- data/bin/set-sync +1 -1
- data/lib/pool.rb +5 -1
- data/lib/rubyhexagon.rb +1 -1
- metadata +1 -1
data/bin/e621_server
CHANGED
@@ -21,6 +21,7 @@
|
|
21
21
|
$:.unshift(File.expand_path(File.dirname(__FILE__)+"/../lib"))
|
22
22
|
|
23
23
|
require "rubyhexagon"
|
24
|
+
require "openssl"
|
24
25
|
require "thread"
|
25
26
|
require "logger"
|
26
27
|
require "socket"
|
@@ -35,115 +36,141 @@ E621::Config.config = File.expand_path("~/.hexagon/conf.json")
|
|
35
36
|
api = API.new
|
36
37
|
tasks = Hash.new
|
37
38
|
mt = Mutex.new
|
38
|
-
server = TCPServer.new(5621)
|
39
39
|
Process.daemon
|
40
|
-
|
41
|
-
|
42
|
-
|
43
|
-
=
|
44
|
-
|
45
|
-
|
46
|
-
|
47
|
-
|
48
|
-
|
49
|
-
|
50
|
-
|
51
|
-
|
52
|
-
|
53
|
-
|
54
|
-
|
55
|
-
|
40
|
+
$0 = "e621 Proxy Server"
|
41
|
+
Dir.chdir(File.expand_path("~/.hexagon")) do
|
42
|
+
@log = Logger.new(File.expand_path(E621::Config.paths["logging"]))
|
43
|
+
@log.formatter = proc do |sev,dat,prog,msg|
|
44
|
+
"#{Time.now.strftime("%b %e, %Y %I:%M:%S %p")} [#{sev.pad(5)}] #{msg}#$/"
|
45
|
+
end
|
46
|
+
begin
|
47
|
+
begin
|
48
|
+
File.open("run"){|f|Process.kill("TERM",f.read.to_i)}
|
49
|
+
rescue
|
50
|
+
end
|
51
|
+
sleep 2
|
52
|
+
File.open("run","w"){|f|f.puts $$}
|
53
|
+
server = TCPServer.new(5621)
|
54
|
+
#@log = Logger.new(STDOUT)
|
55
|
+
#@log.level = Logger::DEBUG
|
56
|
+
queue = Queue.new
|
57
|
+
10.times do
|
58
|
+
Thread.new do
|
59
|
+
loop do
|
60
|
+
post = queue.pop
|
61
|
+
next if File.exist?("cache/#{post.md5[0,2]}/#{post.md5}")
|
62
|
+
mt.synchronize do
|
63
|
+
Dir.mkdir("cache") unless File.exist?("cache")
|
64
|
+
Dir.mkdir("cache/#{post.md5[0,2]}") unless File.exist?("cache/#{post.md5[0,2]}")
|
65
|
+
end
|
66
|
+
body = post.download_data
|
67
|
+
mt.synchronize do
|
68
|
+
File.open("cache/#{post.md5[0,2]}/#{post.md5}","w") do |f|
|
69
|
+
f.print body
|
70
|
+
end
|
71
|
+
end
|
56
72
|
end
|
57
|
-
count = tasks[name]
|
58
|
-
message = "Got #{" "*(max.to_s.length+2-count.to_s.length)}#{count}/#{max}"+" "*16
|
59
|
-
@log.debug message
|
60
|
-
tclient.puts message
|
61
|
-
tclient.close if count >= max
|
62
73
|
end
|
63
74
|
end
|
64
|
-
end
|
65
|
-
end
|
66
|
-
=end
|
67
75
|
|
68
|
-
@log.
|
69
|
-
|
70
|
-
|
71
|
-
|
72
|
-
|
73
|
-
Dir.chdir(File.expand_path("~/.hexagon")) do
|
74
|
-
Thread.new do
|
75
|
-
loop do
|
76
|
-
files,size,tm = Dir["cache/**/*"].reject{|x|!File.file?(x)},0,Array.new
|
77
|
-
files.each do |f|
|
78
|
-
size += File.size(f)
|
79
|
-
end
|
80
|
-
if size > 30*2**30 then
|
76
|
+
@log.level = Logger::INFO
|
77
|
+
@log.info("New server instance started.")
|
78
|
+
Thread.new do
|
79
|
+
loop do
|
80
|
+
files,size,tm = Dir["cache/**/*"].reject{|x|!File.file?(x)},0,Array.new
|
81
81
|
files.each do |f|
|
82
|
-
|
82
|
+
size += File.size(f)
|
83
83
|
end
|
84
|
-
|
85
|
-
|
86
|
-
|
87
|
-
|
88
|
-
|
84
|
+
if size > 30*2**30 then
|
85
|
+
files.each do |f|
|
86
|
+
tm << {"file"=>f,"time"=>File.atime(f),"size"=>File.size(f)}
|
87
|
+
end
|
88
|
+
tm = tm.sort{|f1,f2|f1["time"]<=>f2["time"]}
|
89
|
+
while size > 30*2**30 do
|
90
|
+
unlink = tm.shift
|
91
|
+
size -= unlink["size"]
|
92
|
+
File.unlink(unlink["file"])
|
93
|
+
end
|
89
94
|
end
|
95
|
+
sleep 3600
|
90
96
|
end
|
91
|
-
sleep 3600
|
92
97
|
end
|
93
|
-
end
|
94
|
-
begin
|
95
98
|
Dir["searches/*"].each{|f|File.unlink(f)}
|
96
99
|
loop do
|
97
100
|
client = server.accept
|
98
101
|
Thread.new(client) do |c|
|
99
102
|
begin
|
100
|
-
|
101
|
-
|
102
|
-
|
103
|
-
|
104
|
-
|
105
|
-
case query["action"]
|
106
|
-
when "search" then
|
107
|
-
name,query = query["name"],query["query"]
|
108
|
-
@log.info("[#{c.remote_address.ip_address}]: Search init #{name}/#{query.join("+")}.")
|
109
|
-
Dir.mkdir("searches") unless File.exist?("searches")
|
110
|
-
rid = Digest::SHA2.hexdigest(name+Time.now.strftime("%s"))
|
111
|
-
File.open("searches/#{rid}.json","w") do |f|
|
112
|
-
search = {"name"=>name,"query"=>query}
|
113
|
-
f.print search.to_json
|
103
|
+
begin
|
104
|
+
query = c.gets.parse
|
105
|
+
rescue
|
106
|
+
@log.error("[#{c.remote_address.ip_address}]: Error #$!.")
|
107
|
+
next
|
114
108
|
end
|
115
|
-
|
116
|
-
|
117
|
-
|
118
|
-
|
119
|
-
|
120
|
-
|
121
|
-
|
122
|
-
|
123
|
-
|
124
|
-
|
125
|
-
|
126
|
-
post = Post.new(query["post"])
|
127
|
-
Dir.mkdir("cache") unless File.exist?("cache")
|
128
|
-
Dir.mkdir("cache/#{post.md5[0,2]}") unless File.exist?("cache/#{post.md5[0,2]}")
|
129
|
-
body = String.new
|
130
|
-
if File.exist?("cache/#{post.md5[0,2]}/#{post.md5}") then
|
131
|
-
cached = " (cached)"
|
132
|
-
File.open("cache/#{post.md5[0,2]}/#{post.md5}") do |f|
|
133
|
-
body = f.read
|
109
|
+
case query["action"]
|
110
|
+
when "search" then
|
111
|
+
name,query = query["name"],query["query"]
|
112
|
+
@log.info("[#{c.remote_address.ip_address}]: Search init #{name}/#{query.join("+")}.")
|
113
|
+
Dir.mkdir("searches") unless File.exist?("searches")
|
114
|
+
rid = Digest::SHA2.hexdigest(name+Time.now.strftime("%s"))
|
115
|
+
Thread.new do
|
116
|
+
s = Search.new(query)
|
117
|
+
s.each_post do |post|
|
118
|
+
queue << post
|
119
|
+
end
|
134
120
|
end
|
135
|
-
|
136
|
-
|
137
|
-
|
138
|
-
|
139
|
-
|
121
|
+
File.open("searches/#{rid}.json","w") do |f|
|
122
|
+
search = {"name"=>name,"query"=>query}
|
123
|
+
f.print search.to_json
|
124
|
+
end
|
125
|
+
rid = {"rid"=>rid}.to_json
|
126
|
+
c.print rid
|
127
|
+
when "page" then
|
128
|
+
page, rid = query["page"],query["rid"]
|
129
|
+
File.open("searches/#{rid}.json"){|f|query=f.read.parse}
|
130
|
+
@log.info("[#{c.remote_address.ip_address}]: Search page #{query["name"]}/#{page}.")
|
131
|
+
search = Search.new(query["query"])
|
132
|
+
search.page = page
|
133
|
+
posts = {"posts"=>search.posts}.to_json
|
134
|
+
c.print posts
|
135
|
+
when "show" then
|
136
|
+
post = Post.new(query["post"])
|
137
|
+
body = String.new
|
138
|
+
mt.synchronize do
|
139
|
+
Dir.mkdir("cache") unless File.exist?("cache")
|
140
|
+
Dir.mkdir("cache/#{post.md5[0,2]}") unless File.exist?("cache/#{post.md5[0,2]}")
|
141
|
+
end
|
142
|
+
if File.exist?("cache/#{post.md5[0,2]}/#{post.md5}") then
|
143
|
+
cached = " (cached)"
|
144
|
+
mt.synchronize do
|
145
|
+
File.open("cache/#{post.md5[0,2]}/#{post.md5}") do |f|
|
146
|
+
body = f.read
|
147
|
+
end
|
148
|
+
end
|
149
|
+
else
|
150
|
+
cached = ""
|
151
|
+
body = post.download_data
|
152
|
+
mt.synchronize do
|
153
|
+
File.open("cache/#{post.md5[0,2]}/#{post.md5}","w") do |f|
|
154
|
+
f.print body
|
155
|
+
end
|
156
|
+
end
|
140
157
|
end
|
158
|
+
@log.info("[#{c.remote_address.ip_address}]: Show post #{query["post"]["id"].pad(6," ")}#{cached}.")
|
159
|
+
file = {"name"=>"#{post.id.pad(8)}.#{post.md5}.#{post.file_ext}","data"=>Base64.encode64(body)}
|
160
|
+
c.print file.to_json
|
161
|
+
end
|
162
|
+
rescue
|
163
|
+
@log.error("Error: #$!!")
|
164
|
+
raise
|
165
|
+
if c && !c.closed? then
|
166
|
+
err = {"error"=>true,"message"=> $!.to_s}
|
167
|
+
c.puts err.to_json
|
168
|
+
end
|
169
|
+
ensure
|
170
|
+
if c && !c.closed? then
|
171
|
+
c.close
|
141
172
|
end
|
142
|
-
@log.info("[#{c.remote_address.ip_address}]: Show post #{query["post"]["id"].pad(6," ")}#{cached}.")
|
143
|
-
file = {"name"=>"#{post.id.pad(8)}.#{post.md5}.#{post.file_ext}","data"=>Base64.encode64(body)}
|
144
|
-
c.print file.to_json
|
145
173
|
end
|
146
|
-
c.close
|
147
174
|
end
|
148
175
|
end
|
149
176
|
rescue Errno
|
data/bin/set-sync
CHANGED
@@ -61,7 +61,7 @@ E621::Config.config = File.expand_path("~/.hexagon/conf.json")
|
|
61
61
|
api = API.new("user")
|
62
62
|
@log = Logger.new(File.expand_path(E621::Config.paths["logging"]))
|
63
63
|
@log.formatter = proc do |sev,dat,prog,msg|
|
64
|
-
"#{Time.now.strftime("%b %e, %Y %I:%M:%S %p")}
|
64
|
+
"#{Time.now.strftime("%b %e, %Y %I:%M:%S %p")} [#{sev.pad(5)}] #{msg}#$/"
|
65
65
|
end
|
66
66
|
@log.level = Logger::INFO
|
67
67
|
@log.info("Program #$0 started.")
|
data/lib/pool.rb
CHANGED
data/lib/rubyhexagon.rb
CHANGED