Skip to content

Instantly share code, notes, and snippets.

@ochaochaocha3
Last active July 2, 2016 09:27
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ochaochaocha3/647b81a8cff20d259f7dd020e3cb24f0 to your computer and use it in GitHub Desktop.
Save ochaochaocha3/647b81a8cff20d259f7dd020e3cb24f0 to your computer and use it in GitHub Desktop.
どどんとふの「削除可能」な部屋の一覧を表示するスクリプト
#!/usr/bin/env ruby
require 'optparse'
require 'json'
PlayRoom = Struct.new(:num, :name, :updated)
def pad(s, width)
char_lengths = s.each_char.map { |c| c.ord >= 8192 ? 2 : 1 }
length = char_lengths.reduce(0, &:+)
length < width ? "#{s}#{' ' * (width - length)}" : s
end
options = {}
opts = OptionParser.new
opts.on('-d', '--days DAYS', OptionParser::DecimalInteger,
'経過日数。無指定の場合はすべてを表示する。') do |d|
options[:days] = d
end
opts.parse!(ARGV)
dirs = Dir.glob('data_*/')
rooms = dirs.map do |dir|
num = dir.match(/\d+/) { |m| m[0].to_i }
updated = File.mtime(File.join(dir, 'chatLongLines.txt'))
json = File.read(File.join(dir, 'playRoomInfo.json'))
states = JSON.parse(json)
name = states['playRoomName']
PlayRoom.new(num, name, updated)
end
abandoned_rooms = rooms.
select { |r| r.name.include?('削除可能') }.
sort_by(&:num)
old_abandoned_rooms =
if d = options[:days]
sec = 24 * 60 * 60 * d
now = Time.now
abandoned_rooms.select { |r| (now - r.updated) >= sec }
else
abandoned_rooms
end
old_abandoned_rooms.each do |r|
puts('%4d %s %s' % [r.num, pad(r.name, 36), r.updated.strftime('%F %T')])
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment