Last active
December 11, 2016 06:17
-
-
Save kyontan/f329c52ec12e52767e538e8c968d6000 to your computer and use it in GitHub Desktop.
謎分類器
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env ruby | |
helpers do | |
def img(path, l = nil, width=100, height=100) | |
html = "<img src=\"/#{path}\" width=#{width} height=#{height}>" | |
html = "<a href=\"#{l}\">#{html}</a>" if l | |
html | |
end | |
def crop(a, res) | |
puts "Create thumbnail: #{res}" | |
`convert -crop 250x250+360+50 #{a} #{res}` | |
# `convert -threshold 50000 -crop 250x250+360+50 #{a} #{res}` | |
end | |
end | |
get "/?" do | |
%w(characters tate yoko rd ru grouped status).map{|x| "<a href=\"/#{x}\">/#{x}</a><br>" }.join | |
# pngs = Dir.glob("tmp/?????.png") | |
# pngs.shuffle[0..200].sort.map do |x| | |
# n = x[4..7] | |
# c = x[8] | |
# img(x, "/move/#{n}/#{c}") | |
# end.join | |
# send_file Pathname(settings.public_dir) + "index.html" | |
# send_file Pathname(settings.views) + "index.html" | |
end | |
get /^\/.$/ do | |
c = request.path[-1] | |
# map = {yoko: "-04aeinrv", tate: "26cglptx", ru: "15dhmuyq", rd: "35bfjkosw"} | |
# from = map.select{|x| /#{c}/ === map[x] }.keys.first.to_s | |
# pngs = Dir.glob("tmp/????#{c}.png") | |
orig_pngs = Dir.glob("*.png") | |
# orig_pngs = Dir.glob("grouped/#{from}/*.png") | |
pngs = orig_pngs.map do |x| | |
n = "tmp/" + x.scan(/\d+/).join + "_crop.png" | |
crop(x, n) unless File.exists?(n) | |
n | |
end | |
img(Dir.glob("grouped/#{c}/*").first, nil, 854, 480) + pngs.map do |x| | |
n = x[4..7] | |
# c = x[8] | |
img(x, "/move/#{n}/#{c}") | |
end.join | |
end | |
%w(tate yoko rd ru).each do |k| | |
get "/#{k}" do | |
# c = request.path[-1] | |
# pngs = Dir.glob("tmp/????#{c}.png") | |
pngs = Dir.glob("./*.png").map{|x| "tmp/" + x.scan(/\d+/).join + "_crop.png" } | |
img(Dir.glob("grouped/#{k}/*").first, nil, 854, 480) + pngs[0..500].compact.map do |x| | |
n = x[4..7] | |
# c = x[8] | |
img(x, "/move/#{n}/#{k}") | |
end.join | |
end | |
end | |
get "/timeline/:minute" do | |
min = params[:minute].to_i | |
pngs = [*0..59].map do |s| | |
sec = s + min * 60 | |
fname = "out#{"%04d" % [sec]}d.png" | |
Dir.glob("**/*/#{fname}").first | |
end | |
pngs.compact.map do |x| | |
n = x[4..7] | |
c = x[8] | |
html = img(x, "/reorder/#{x}", 854/2, 480/2) | |
html = x[8] + html + "<br>" if /grouped/ === x | |
html | |
end.join | |
# send_file Pathname(settings.public_dir) + "index.html" | |
# send_file Pathname(settings.views) + "index.html" | |
end | |
get "/move/:file/:char" do | |
file = params[:file] | |
char = params[:char] | |
files = Dir.glob("grouped/*/out#{file}d.png") + Dir.glob("out#{file}d.png") | |
if files.empty? | |
puts "File not found: #{file}" | |
halt 404, "File already not found" | |
end | |
files.each do |f| | |
`mv #{f} grouped/#{char}/` | |
end | |
`rm tmp/#{file}*.png` | |
halt 200 | |
# redirect "/" | |
end | |
get "/status" do | |
list = Dir.glob("grouped/?/*.png") | |
ans = list.sort_by{|x| x[13..16].to_i }.inject([]){|acc, x| acc[x[13..16].to_i] = x[8]; acc }.map{|x| x = "?" if x.nil?; x }.join | |
{ | |
count: Dir.glob("tmp/*.png").count, | |
ans: ans | |
}.to_json | |
end | |
get "/ans" do | |
list = Dir.glob("grouped/?/*.png") | |
ans = list.sort_by{|x| x[13..16].to_i }.inject([]){|acc, x| acc[x[13..16].to_i] = x[8]; acc }.map{|x| x = "?" if x.nil?; x }.join | |
ans.scan(/[a-z0-5]+/).join | |
end | |
get "/characters" do | |
pngs = Dir.glob("grouped/*/*.png").group_by{|x| x[8] }.map{|k, v| v.first } | |
pngs.map do |x| | |
# n = x[4..7] | |
c = x[8] | |
img(x, "/#{c}", 854, 480) | |
end.join | |
end | |
get "/grouped" do | |
large_group = %w(yoko tate ru rd).map{|x| "grouped/#{x}" } | |
Dir.glob("grouped/?").map{|x| | |
"<a href=\"/#{x}\">#{x[-1]}</a>" | |
}.join + large_group.map{|x| "<a href=\"/#{x}\">#{x[x.index(?/)+1..-1]}</a>" }.join | |
end | |
get "/grouped/:c" do | |
c = params[:c] | |
pngs = Dir.glob("grouped/#{c}/*.png") | |
pngs.map do |x| | |
n = x[4..7] | |
c = x[8] | |
img(x, "/reorder/#{x}", 854/2, 480/2) | |
end.join | |
end | |
get /reorder\/.*/ do | |
`mv .#{request.path[8..-1]} ./` | |
basename = File.basename(request.path) | |
thumbname = "tmp/" + basename.scan(/\d+/).join + "_crop.png" | |
crop("./" + basename, thumbname) | |
end | |
get /.*.png/ do | |
send_file ".#{request.path}" | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment