Module: Utils
- Defined in:
- lib/kaki/utils.rb
Class Method Summary collapse
-
.bell ⇒ Object
ベルを鳴らす(Ubuntuのみ).
-
.combination(a, b) ⇒ Integer
aの中からb個選ぶ組み合わせの数.
-
.delete_empty_folder(fname = './') ⇒ Object
指定したディレクトリの空フォルダを削除.
-
.delete_non_img(fname) ⇒ Boolean
画像ファイルでなければ削除.
-
.factorial(n) ⇒ Integer
階乗.
-
.generate_random_strings(num, string_length = nil) ⇒ Array
アルファベット小文字を使った長さ string_length の文字列を、重複しないようにランダムに num 個生成する。string_length が指定されないときは最短の文字列を使う.
-
.getfile(url, filename, max = 0, ua = nil) ⇒ Boolean
Web上のバイナリファイルの保存.
-
.imgexist?(url) ⇒ Boolean
画像か?.
-
.key_wait(breakable = true) ⇒ String
一文字入力.
-
.m3u8(url, fname) ⇒ Object
m3u8(HLS)ダウンロード.
-
.modify_serial_numbers(dir = Dir.pwd) ⇒ Object
連番ファイル名を sort 順になるように変更する.
-
.permutation(a, b) ⇒ Integer
a個の中からb個選ぶ順列の数.
-
.repeated_combination(a, b) ⇒ Integer
a個の中から重複を許してb個選ぶ組み合わせの数.
-
.repeated_permutation(a, b) ⇒ Integer
a個の中から重複を許してb個選ぶ順列の数.
Class Method Details
.bell ⇒ Object
ベルを鳴らす(Ubuntuのみ)
74 75 76 |
# File 'lib/kaki/utils.rb', line 74 def bell `paplay /usr/share/sounds/freedesktop/stereo/complete.oga` end |
.combination(a, b) ⇒ Integer
aの中からb個選ぶ組み合わせの数
98 99 100 |
# File 'lib/kaki/utils.rb', line 98 def combination(a, b) Utils.permutation(a, b) / Utils.factorial(b) end |
.delete_empty_folder(fname = './') ⇒ Object
指定したディレクトリの空フォルダを削除
63 64 65 66 67 68 69 |
# 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) ⇒ Boolean
画像ファイルでなければ削除
51 52 53 54 55 56 57 58 |
# 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) ⇒ Integer
階乗
82 83 84 85 86 |
# 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) ⇒ Array
アルファベット小文字を使った長さ string_length の文字列を、重複しないようにランダムに num 個生成する。string_length が指定されないときは最短の文字列を使う
118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 |
# 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) ⇒ Boolean
Web上のバイナリファイルの保存
17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 |
# 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| open(url, opt) {|data| file.write(data.read)} end true rescue puts "ファイル入出力エラー: " + $!..encode("UTF-8") count += 1 return false if count > max #max回までリトライする puts "リトライ: #{count}" sleep(1) retry end end |
.imgexist?(url) ⇒ Boolean
画像か?
9 10 11 12 |
# File 'lib/kaki/utils.rb', line 9 def imgexist?(url) s = FastImage.size(url) s ? s != [0, 0] : s end |
.key_wait(breakable = true) ⇒ String
一文字入力
39 40 41 42 43 44 45 |
# 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) ⇒ Object
m3u8(HLS)ダウンロード
161 162 163 |
# 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) ⇒ Object
連番ファイル名を sort 順になるように変更する
168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 |
# 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) ⇒ Integer
a個の中からb個選ぶ順列の数
92 93 94 |
# File 'lib/kaki/utils.rb', line 92 def permutation(a, b) [*1..a].last(b).inject(1, &:*) end |
.repeated_combination(a, b) ⇒ Integer
a個の中から重複を許してb個選ぶ組み合わせの数
110 111 112 |
# File 'lib/kaki/utils.rb', line 110 def repeated_combination(a, b) Utils.combination(a + b - 1, b) end |
.repeated_permutation(a, b) ⇒ Integer
a個の中から重複を許してb個選ぶ順列の数
104 105 106 |
# File 'lib/kaki/utils.rb', line 104 def repeated_permutation(a, b) a ** b end |