Commit 3a302366 authored by 神楽坂玲奈's avatar 神楽坂玲奈

server

parent e783bb9d
...@@ -14,16 +14,48 @@ if defined?(Ocra) or defined?(Exerb) ...@@ -14,16 +14,48 @@ if defined?(Ocra) or defined?(Exerb)
require 'rb-notifu' require 'rb-notifu'
require 'sqlite3' require 'sqlite3'
require 'socket' require 'socket'
require 'digest/md5'
#open-uri protocol
require 'net/http' require 'net/http'
#websocket autoload
WebSocket::Error
WebSocket::ExceptionHandler
WebSocket::Frame::Base
WebSocket::Frame::Data
WebSocket::Frame::Handler::Base
WebSocket::Frame::Handler::Handler03
WebSocket::Frame::Handler::Handler04
WebSocket::Frame::Handler::Handler05
WebSocket::Frame::Handler::Handler07
WebSocket::Frame::Handler::Handler75
WebSocket::Frame::Incoming::Client
WebSocket::Frame::Incoming::Server
WebSocket::Frame::Outgoing::Client
WebSocket::Frame::Outgoing::Server
WebSocket::Handshake::Base
WebSocket::Handshake::Client
WebSocket::Handshake::Handler::Base
WebSocket::Handshake::Handler::Client
WebSocket::Handshake::Handler::Client01
WebSocket::Handshake::Handler::Client04
WebSocket::Handshake::Handler::Client75
WebSocket::Handshake::Handler::Client76
WebSocket::Handshake::Handler::Server
WebSocket::Handshake::Handler::Server04
WebSocket::Handshake::Handler::Server75
WebSocket::Handshake::Handler::Server76
WebSocket::Handshake::Server
exit exit
end end
# = Runtime # = Runtime
begin begin
# == initialize # == initialize
Version = "0.1.0" Version = "2.0.0"
Platform = (RUBY_PLATFORM['mswin'] || RUBY_PLATFORM['mingw']) ? :win32 : :linux Platform = (RUBY_PLATFORM['mswin'] || RUBY_PLATFORM['mingw']) ? :win32 : :linux
System_Encoding = Encoding.find("locale") rescue Encoding.find(Encoding.locale_charmap) System_Encoding = Encoding.find("locale") rescue Encoding.find(Encoding.locale_charmap)
Dir.chdir File.dirname(defined?(ExerbRuntime) ? ExerbRuntime.filepath.dup.force_encoding(System_Encoding).encode!(Encoding::UTF_8) : ENV["OCRA_EXECUTABLE"] || __FILE__) Dir.chdir File.dirname(defined?(ExerbRuntime) ? ExerbRuntime.filepath.dup.force_encoding(System_Encoding).encode!(Encoding::UTF_8) : ENV["OCRA_EXECUTABLE"] || __FILE__)
...@@ -165,9 +197,14 @@ begin ...@@ -165,9 +197,14 @@ begin
TCPServer.new('0.0.0.0', Config['port']).close rescue return #check port in use, seems eventmachine enabled IP_REUSEADDR. TCPServer.new('0.0.0.0', Config['port']).close rescue return #check port in use, seems eventmachine enabled IP_REUSEADDR.
require 'websocket-eventmachine-server' require 'websocket-eventmachine-server'
EventMachine.run do EventMachine.run do
ygopro_version = nil
connections = []
WebSocket::EventMachine::Server.start(:host => "0.0.0.0", :port => Config['port']) do |ws| WebSocket::EventMachine::Server.start(:host => "0.0.0.0", :port => Config['port']) do |ws|
ws.onopen do ws.onopen do
ws.send({'version' => Version}.to_json) connections.push ws
msg = {'version' => Version}
msg['ygopro_version'] = ygopro_version if ygopro_version
ws.send(msg.to_json)
end end
ws.onmessage do |msg, type| ws.onmessage do |msg, type|
...@@ -175,9 +212,18 @@ begin ...@@ -175,9 +212,18 @@ begin
end end
ws.onclose do ws.onclose do
puts "Client disconnected" connections.delete ws
exit if connections.empty?
end end
end end
require 'em-http'
http = EM::HttpRequest.new('https://my-card.in/ygopro_version.json').get
http.callback { |http|
md5 = Digest::MD5.file(Config['ygopro']['path']).hexdigest
ygopro_version = JSON.parse(http.response)[md5] || md5
connections.each { |ws| ws.send ({'ygopro_version' => ygopro_version}).to_json }
}
if File.file? File.join(File.dirname(Config['ygopro']['path']), 'cards.cdb') if File.file? File.join(File.dirname(Config['ygopro']['path']), 'cards.cdb')
require 'sqlite3' require 'sqlite3'
db = SQLite3::Database.new File.join(File.dirname(Config['ygopro']['path']), 'cards.cdb') db = SQLite3::Database.new File.join(File.dirname(Config['ygopro']['path']), 'cards.cdb')
...@@ -186,12 +232,10 @@ begin ...@@ -186,12 +232,10 @@ begin
thumbnails_to_download = cards - Dir.glob(File.join(File.dirname(Config['ygopro']['path']), 'pics', 'thumbnail', '*.jpg')).collect { |file| File.basename(file, '.jpg').to_i } thumbnails_to_download = cards - Dir.glob(File.join(File.dirname(Config['ygopro']['path']), 'pics', 'thumbnail', '*.jpg')).collect { |file| File.basename(file, '.jpg').to_i }
end end
unless images_to_download.empty? and thumbnails_to_download.empty? unless images_to_download.empty? and thumbnails_to_download.empty?
require 'em-http'
require 'uri' require 'uri'
http = EM::HttpRequest.new('https://my-card.in/cards/image.json').get http = EM::HttpRequest.new('https://my-card.in/cards/image.json').get
http.callback { http.callback {
response = JSON.parse http.response response = JSON.parse http.response
p response
image_url = URI(response['url']) image_url = URI(response['url'])
thumbnail_url = URI(response['thumbnail_url']) thumbnail_url = URI(response['thumbnail_url'])
Dir.mkdir File.join(File.dirname(Config['ygopro']['path']), 'pics') unless File.directory? File.join(File.dirname(Config['ygopro']['path']), 'pics') Dir.mkdir File.join(File.dirname(Config['ygopro']['path']), 'pics') unless File.directory? File.join(File.dirname(Config['ygopro']['path']), 'pics')
...@@ -199,7 +243,20 @@ begin ...@@ -199,7 +243,20 @@ begin
files = {} files = {}
thumbnails_to_download.each { |card_id| files[thumbnail_url.path.gsub(':id', card_id.to_s)] = File.join(File.dirname(Config['ygopro']['path']), 'pics', 'thumbnail', card_id.to_s + '.jpg') } thumbnails_to_download.each { |card_id| files[thumbnail_url.path.gsub(':id', card_id.to_s)] = File.join(File.dirname(Config['ygopro']['path']), 'pics', 'thumbnail', card_id.to_s + '.jpg') }
images_to_download.each { |card_id| files[image_url.path.gsub(':id', card_id.to_s)] = File.join(File.dirname(Config['ygopro']['path']), 'pics', card_id.to_s + '.jpg') } images_to_download.each { |card_id| files[image_url.path.gsub(':id', card_id.to_s)] = File.join(File.dirname(Config['ygopro']['path']), 'pics', card_id.to_s + '.jpg') }
batch_download(image_url.to_s, files, 'image/jpeg') thumbnails_count = thumbnails_to_download.size
images_count = images_to_download.size
errors_count = 0
batch_download(image_url.to_s, files, 'image/jpeg') { |http|
if http.req.path["thumbnail"]
thumbnails_count -= 1
else
images_count -= 1
end
unless http.response_header.status == 200 and http.response_header['CONTENT_TYPE'] == 'image/jpeg'
errors_count += 1
end
connections.each { |ws| ws.send ({'images_download_images' => images_count, 'images_download_thumbnails' => thumbnails_count, 'images_download_errors' => errors_count}).to_json }
}
} }
require 'rb-notifu' require 'rb-notifu'
Notifu::show :message => "缩略: #{thumbnails_to_download.size}, 完整: #{images_to_download.size}".encode(System_Encoding), :baloon => false, :type => :info, :title => "正在下载卡图".encode(System_Encoding) do |status| Notifu::show :message => "缩略: #{thumbnails_to_download.size}, 完整: #{images_to_download.size}".encode(System_Encoding), :baloon => false, :type => :info, :title => "正在下载卡图".encode(System_Encoding) do |status|
...@@ -211,13 +268,13 @@ begin ...@@ -211,13 +268,13 @@ begin
end end
end end
def batch_download(main_url, files, content_type=nil) def batch_download(main_url, files, content_type=nil, &block)
connections = {} connections = {}
count = {total: files.size, error: 0} count = {total: files.size, error: 0}
[10*100, files.size].min.times { do_download(main_url, files, content_type, count, connections) } [10*100, files.size].min.times { do_download(main_url, files, content_type, count, connections, &block) }
end end
def do_download(main_url, files, content_type, count, connections) def do_download(main_url, files, content_type, count, connections, &block)
if connections.size < 10 if connections.size < 10
connection = EventMachine::HttpRequest.new(main_url) connection = EventMachine::HttpRequest.new(main_url)
connections[connection] = 0 connections[connection] = 0
...@@ -232,13 +289,13 @@ begin ...@@ -232,13 +289,13 @@ begin
remote_path, local_path = files.shift remote_path, local_path = files.shift
connections[connection] += 1 connections[connection] += 1
connection.get(path: remote_path, keepalive: connections[connection] != 100).callback { |http| connection.get(path: remote_path, keepalive: connections[connection] != 100).callback { |http|
puts File.basename local_path #puts File.basename local_path
count[:error] = 0 count[:error] = 0
count[:total] -= 1 count[:total] -= 1
if http.response_header['CONNECTION'] != 'keep-alive' if http.response_header['CONNECTION'] != 'keep-alive'
connection.close connection.close
connections.delete(connection) connections.delete(connection)
do_download(main_url, files, content_type, count, connections) while !files.empty? and (connections.size < 10 or connections.values.min < 100) do_download(main_url, files, content_type, count, connections, &block) while !files.empty? and (connections.size < 10 or connections.values.min < 100)
end end
if http.response_header.status == 200 and (!content_type or http.response_header['CONTENT_TYPE'] == content_type) if http.response_header.status == 200 and (!content_type or http.response_header['CONTENT_TYPE'] == content_type)
...@@ -247,12 +304,13 @@ begin ...@@ -247,12 +304,13 @@ begin
puts http.response_header.http_status puts http.response_header.http_status
end end
yield http
if count[:total].zero? if count[:total].zero?
connections.each_key { |connection| connection.close } connections.each_key { |connection| connection.close }
connections.clear connections.clear
puts 'all done'
EM.stop
end end
}.errback { |http| }.errback { |http|
puts http.error puts http.error
connection.close connection.close
...@@ -260,12 +318,11 @@ begin ...@@ -260,12 +318,11 @@ begin
files[remote_path] = local_path files[remote_path] = local_path
count[:error] += 1 count[:error] += 1
if count[:error] <= 10*100 if count[:error] <= 10*100
do_download(main_url, files, content_type, count, connections) while !files.empty? and (connections.size < 10 or connections.values.min < 100) do_download(main_url, files, content_type, count, connections, &block) while !files.empty? and (connections.size < 10 or connections.values.min < 100)
else else
connections.each_key { |connection| connection.close } connections.each_key { |connection| connection.close }
connections.clear connections.clear
puts 'network error' puts 'network error'
EM.stop
end end
} }
end end
...@@ -455,10 +512,13 @@ begin ...@@ -455,10 +512,13 @@ begin
class <<self class <<self
def join(*args) def join(*args)
end end
def expand_path(*args) def expand_path(*args)
end end
def dirname(*args) def dirname(*args)
end end
def open(*args) def open(*args)
result = ExerbRuntime.open('unicode.data') result = ExerbRuntime.open('unicode.data')
if block_given? if block_given?
...@@ -481,7 +541,9 @@ begin ...@@ -481,7 +541,9 @@ begin
parse ARGV.first.dup.force_encoding(System_Encoding).encode!(Encoding::UTF_8) parse ARGV.first.dup.force_encoding(System_Encoding).encode!(Encoding::UTF_8)
else else
register if !registed? register if !registed?
if File.file? 'ruby\bin\rubyw.exe' if File.file? 'nw.exe'
spawn 'nw.exe'
elsif File.file? 'ruby\bin\rubyw.exe'
spawn 'ruby\bin\rubyw.exe', '-KU', 'lib/main.rb' spawn 'ruby\bin\rubyw.exe', '-KU', 'lib/main.rb'
else else
System.web(Config['url']) System.web(Config['url'])
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment