rubyhexagon 0.0.1 → 0.0.5

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
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
- @log = Logger.new(File.expand_path(E621::Config.paths["logging"]))
41
- #@log = Logger.new(STDOUT)
42
- #@log.level = Logger::DEBUG
43
- =begin
44
- 10.times do
45
- Thread.new do
46
- loop do
47
- name,tclient,max,post = queue.pop
48
- next unless post
49
- Dir.mkdir(name) unless File.exist?(name)
50
- post.download("#{name}/#{"0"*(7-post.id.to_s.length)}#{post.id}.#{post.md5}.#{post.file_ext}")
51
- mt.synchronize do
52
- unless tasks.has_key?(name) then
53
- tasks.store(name,1)
54
- else
55
- tasks[name] += 1
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.formatter = proc do |sev,dat,prog,msg|
69
- "#{Time.now.strftime("%b %e, %Y %I:%M:%S %p")} #{msg}#$/"
70
- end
71
- @log.level = Logger::INFO
72
- @log.info("New server instance started.")
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
- tm << {"file"=>f,"time"=>File.atime(f),"size"=>File.size(f)}
82
+ size += File.size(f)
83
83
  end
84
- tm = tm.sort{|f1,f2|f1["time"]<=>f2["time"]}
85
- while size > 30*2**30 do
86
- unlink = tm.shift
87
- size -= unlink["size"]
88
- File.unlink(unlink["file"])
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
- query = c.gets.parse
101
- rescue
102
- @log.error("[#{c.remote_address.ip_address}]: Error #$!.")
103
- next
104
- end
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
- rid = {"rid"=>rid}.to_json
116
- c.print rid
117
- when "page" then
118
- page, rid = query["page"],query["rid"]
119
- File.open("searches/#{rid}.json"){|f|query=f.read.parse}
120
- @log.info("[#{c.remote_address.ip_address}]: Search page #{query["name"]}/#{page}.")
121
- search = Search.new(query["query"])
122
- search.page = page
123
- posts = {"posts"=>search.posts}.to_json
124
- c.print posts
125
- when "show" then
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
- else
136
- cached = ""
137
- body = post.download_data
138
- File.open("cache/#{post.md5[0,2]}/#{post.md5}","w") do |f|
139
- f.print body
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")}: #{msg}#$/"
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
@@ -36,8 +36,12 @@ module E621
36
36
  def name=(name)
37
37
  @name = name
38
38
  end
39
+
40
+ def posts=(posts)
41
+ @posts = posts
42
+ end
39
43
 
40
- def to_json
44
+ def to_json(n=nil)
41
45
  json_hash = Hash.new
42
46
  instance_variables.each do |i|
43
47
  v = instance_variable_get(i)
data/lib/rubyhexagon.rb CHANGED
@@ -30,5 +30,5 @@ require "pool"
30
30
 
31
31
  module E621
32
32
  Name = "rubyhexagon"
33
- Version = "0.0.1"
33
+ Version = "0.0.5"
34
34
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: rubyhexagon
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.1
4
+ version: 0.0.5
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors: