module Utils
Public Class Methods
bell()
click to toggle source
ベルを鳴らす(Ubuntuのみ)
# File lib/kaki/utils.rb, line 74 def bell `paplay /usr/share/sounds/freedesktop/stereo/complete.oga` end
combination(a, b)
click to toggle source
aの中からb個選ぶ組み合わせの数
@return [Integer]
# File lib/kaki/utils.rb, line 98 def combination(a, b) Utils.permutation(a, b) / Utils.factorial(b) end
delete_empty_folder(fname = './')
click to toggle source
指定したディレクトリの空フォルダを削除
# File lib/kaki/utils.rb, line 63 def delete_empty_folder(fname = './') Dir.chdir(fname) Dir.glob("*").each do |name| next unless File.directory?(name) Dir.rmdir(p name) if Dir.glob("./#{name}/*").empty? rescue next end end
delete_non_img(fname)
click to toggle source
画像ファイルでなければ削除
@return [Boolean]
# File lib/kaki/utils.rb, line 51 def delete_non_img(fname) if Utils.imgexist?(fname) false else FileUtils.rm(fname) true end end
factorial(n)
click to toggle source
階乗
@return [Integer]
# File lib/kaki/utils.rb, line 82 def factorial(n) result = 1 2.upto(n) {|i| result *= i} result end
generate_random_strings(num, string_length = nil)
click to toggle source
アルファベット小文字を使った長さ string_length の文字列を、重複しないようにランダムに num 個生成する。string_length が指定されないときは最短の文字列を使う
@return [Array]
# File lib/kaki/utils.rb, line 118 def generate_random_strings(num, string_length = nil) table = [*"a".."z"] limit = [0, 26, 702, 18278, 475254, 12356630] result = [] generate_string1 = ->(n, l) { st = "" l.times do a, n = n % 26, n / 26 st = table[a] + st end st } generate_string2 = ->(n) { idx = limit.find_index {|i| i > n} generate_string1.(n - limit[idx - 1], idx) } if string_length and 26 < string_length raise "Given length of strings too big." end num_table = Set.new if string_length n = Utils.repeated_permutation(26, string_length) raise "Given length of strings too small." if n < num while num_table.size < num num_table << rand(n) end num_table.each {|i| result << generate_string1.(i, string_length)} else idx = limit.find_index {|i| i >= num} raise "Result Array too big." unless idx while num_table.size < num num_table << rand(limit[idx]) end num_table.each {|i| result << generate_string2.(i)} end result end
getfile(url, filename, max = 0, ua = nil)
click to toggle source
Web上のバイナリファイルの保存
@return [Boolean]
# File lib/kaki/utils.rb, line 17 def getfile(url, filename, max = 0, ua = nil) count = 0 opt = ua ? {'User-Agent' => ua} : {} begin open(filename, 'wb') do |file| URI.open(url, opt) {|data| file.write(data.read)} end true rescue puts "ファイル入出力エラー: " + $!.message.encode("UTF-8") count += 1 return false if count > max #max回までリトライする puts "リトライ: #{count}" sleep(1) retry end end
imgexist?(url)
click to toggle source
画像か?
# File lib/kaki/utils.rb, line 9 def imgexist?(url) s = FastImage.size(url) s ? s != [0, 0] : s end
key_wait(breakable = true)
click to toggle source
一文字入力
@return [String]
# File lib/kaki/utils.rb, line 39 def key_wait(breakable = true) c = nil loop {break if (c = STDIN.getch)} STDIN.cooked! raise Interrupt if breakable and c == "\cC" c end
m3u8(url, fname)
click to toggle source
m3u8(HLS)ダウンロード
# File lib/kaki/utils.rb, line 161 def m3u8(url, fname) %x(ffmpeg -i #{url} -movflags faststart -c copy -bsf:a aac_adtstoasc #{fname}) end
modify_serial_numbers(dir = Dir.pwd)
click to toggle source
連番ファイル名を sort 順になるように変更する
# File lib/kaki/utils.rb, line 168 def modify_serial_numbers(dir = Dir.pwd) Dir.chdir(dir) files = Dir.glob("*").select {|fn| File.file?(fn)} data1 = [] data = files.map do |fn| m = /^(.*?\D*?)(\d+)(\..+)$/.match(fn) if m [fn, m[1], m[2].to_i, m[3]] else data1 << [fn, fn] nil end end.compact num_length = data.map {|d| d[2].to_s.length}.max data1 += data.map {|d| [d[0], d[1] + ("%0#{num_length}d" % d[2]) + d[3]]} Dir.mkdir("new_folder") data1.each {|be, af| FileUtils.cp(be, "new_folder/#{af}")} end
permutation(a, b)
click to toggle source
a個の中からb個選ぶ順列の数
@return [Integer]
# File lib/kaki/utils.rb, line 92 def permutation(a, b) [*1..a].last(b).inject(1, &:*) end
repeated_combination(a, b)
click to toggle source
a個の中から重複を許してb個選ぶ組み合わせの数
@return [Integer]
# File lib/kaki/utils.rb, line 110 def repeated_combination(a, b) Utils.combination(a + b - 1, b) end
repeated_permutation(a, b)
click to toggle source
a個の中から重複を許してb個選ぶ順列の数
@return [Integer]
# File lib/kaki/utils.rb, line 104 def repeated_permutation(a, b) a ** b end
timer(minutes)
click to toggle source
簡易タイマー
# File lib/kaki/utils.rb, line 191 def timer(minutes) end_time = Time.now + (minutes * 60).to_i while Time.now < end_time print "\e[2K\e[1Gleft: #{(end_time - Time.now).to_i} sec." sleep(1) end Utils.bell puts end
Private Instance Methods
bell()
click to toggle source
ベルを鳴らす(Ubuntuのみ)
# File lib/kaki/utils.rb, line 74 def bell `paplay /usr/share/sounds/freedesktop/stereo/complete.oga` end
combination(a, b)
click to toggle source
aの中からb個選ぶ組み合わせの数
@return [Integer]
# File lib/kaki/utils.rb, line 98 def combination(a, b) Utils.permutation(a, b) / Utils.factorial(b) end
delete_empty_folder(fname = './')
click to toggle source
指定したディレクトリの空フォルダを削除
# File lib/kaki/utils.rb, line 63 def delete_empty_folder(fname = './') Dir.chdir(fname) Dir.glob("*").each do |name| next unless File.directory?(name) Dir.rmdir(p name) if Dir.glob("./#{name}/*").empty? rescue next end end
delete_non_img(fname)
click to toggle source
画像ファイルでなければ削除
@return [Boolean]
# File lib/kaki/utils.rb, line 51 def delete_non_img(fname) if Utils.imgexist?(fname) false else FileUtils.rm(fname) true end end
factorial(n)
click to toggle source
階乗
@return [Integer]
# File lib/kaki/utils.rb, line 82 def factorial(n) result = 1 2.upto(n) {|i| result *= i} result end
generate_random_strings(num, string_length = nil)
click to toggle source
アルファベット小文字を使った長さ string_length の文字列を、重複しないようにランダムに num 個生成する。string_length が指定されないときは最短の文字列を使う
@return [Array]
# File lib/kaki/utils.rb, line 118 def generate_random_strings(num, string_length = nil) table = [*"a".."z"] limit = [0, 26, 702, 18278, 475254, 12356630] result = [] generate_string1 = ->(n, l) { st = "" l.times do a, n = n % 26, n / 26 st = table[a] + st end st } generate_string2 = ->(n) { idx = limit.find_index {|i| i > n} generate_string1.(n - limit[idx - 1], idx) } if string_length and 26 < string_length raise "Given length of strings too big." end num_table = Set.new if string_length n = Utils.repeated_permutation(26, string_length) raise "Given length of strings too small." if n < num while num_table.size < num num_table << rand(n) end num_table.each {|i| result << generate_string1.(i, string_length)} else idx = limit.find_index {|i| i >= num} raise "Result Array too big." unless idx while num_table.size < num num_table << rand(limit[idx]) end num_table.each {|i| result << generate_string2.(i)} end result end
getfile(url, filename, max = 0, ua = nil)
click to toggle source
Web上のバイナリファイルの保存
@return [Boolean]
# File lib/kaki/utils.rb, line 17 def getfile(url, filename, max = 0, ua = nil) count = 0 opt = ua ? {'User-Agent' => ua} : {} begin open(filename, 'wb') do |file| URI.open(url, opt) {|data| file.write(data.read)} end true rescue puts "ファイル入出力エラー: " + $!.message.encode("UTF-8") count += 1 return false if count > max #max回までリトライする puts "リトライ: #{count}" sleep(1) retry end end
imgexist?(url)
click to toggle source
画像か?
# File lib/kaki/utils.rb, line 9 def imgexist?(url) s = FastImage.size(url) s ? s != [0, 0] : s end
key_wait(breakable = true)
click to toggle source
一文字入力
@return [String]
# File lib/kaki/utils.rb, line 39 def key_wait(breakable = true) c = nil loop {break if (c = STDIN.getch)} STDIN.cooked! raise Interrupt if breakable and c == "\cC" c end
m3u8(url, fname)
click to toggle source
m3u8(HLS)ダウンロード
# File lib/kaki/utils.rb, line 161 def m3u8(url, fname) %x(ffmpeg -i #{url} -movflags faststart -c copy -bsf:a aac_adtstoasc #{fname}) end
modify_serial_numbers(dir = Dir.pwd)
click to toggle source
連番ファイル名を sort 順になるように変更する
# File lib/kaki/utils.rb, line 168 def modify_serial_numbers(dir = Dir.pwd) Dir.chdir(dir) files = Dir.glob("*").select {|fn| File.file?(fn)} data1 = [] data = files.map do |fn| m = /^(.*?\D*?)(\d+)(\..+)$/.match(fn) if m [fn, m[1], m[2].to_i, m[3]] else data1 << [fn, fn] nil end end.compact num_length = data.map {|d| d[2].to_s.length}.max data1 += data.map {|d| [d[0], d[1] + ("%0#{num_length}d" % d[2]) + d[3]]} Dir.mkdir("new_folder") data1.each {|be, af| FileUtils.cp(be, "new_folder/#{af}")} end
permutation(a, b)
click to toggle source
a個の中からb個選ぶ順列の数
@return [Integer]
# File lib/kaki/utils.rb, line 92 def permutation(a, b) [*1..a].last(b).inject(1, &:*) end
repeated_combination(a, b)
click to toggle source
a個の中から重複を許してb個選ぶ組み合わせの数
@return [Integer]
# File lib/kaki/utils.rb, line 110 def repeated_combination(a, b) Utils.combination(a + b - 1, b) end
repeated_permutation(a, b)
click to toggle source
a個の中から重複を許してb個選ぶ順列の数
@return [Integer]
# File lib/kaki/utils.rb, line 104 def repeated_permutation(a, b) a ** b end
timer(minutes)
click to toggle source
簡易タイマー
# File lib/kaki/utils.rb, line 191 def timer(minutes) end_time = Time.now + (minutes * 60).to_i while Time.now < end_time print "\e[2K\e[1Gleft: #{(end_time - Time.now).to_i} sec." sleep(1) end Utils.bell puts end