Created
October 19, 2019 07:22
-
-
Save ochaochaocha3/c1b60246b265b529af1543c0c706986a to your computer and use it in GitHub Desktop.
どどんとふのチャットログからダイスロール部分を抽出するスクリプト
This file contains 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
require 'yaml' | |
require 'json' | |
lines = ARGF.readlines(chomp: true) | |
json_lines = lines.map { |line| | |
left_bracket_index = line.index('[') | |
line[left_bracket_index..-1] | |
} | |
hashes = json_lines.map { |line| | |
JSON.parse(line) | |
} | |
messages = hashes.map{ |hash| | |
hash[1]['message'] | |
} | |
sharps = messages.grep(/\A###/) | |
inner_hashes = sharps.map { |message| | |
JSON.parse(message.sub(/\A###.+?###/, '')) | |
} | |
chat_messages = inner_hashes.map{ |hash| | |
hash['chatMessage'] | |
}.compact | |
multi_lines = chat_messages.select { |m| | |
m.chomp.lines.length > 1 | |
} | |
dice_commands = multi_lines. | |
map { |m| | |
second_line = m.lines[1] | |
matches = second_line.match(/\A(.+?):/) | |
game_id = matches ? matches[1].strip : nil | |
[game_id, m.lines[0].strip] | |
}. | |
reject { |game_id, _| game_id.nil? }. | |
sort_by { |game_id, _| game_id } | |
game_id_messages = dice_commands.reduce({}) { |acc, (game_id, message)| | |
acc[game_id] ||= [] | |
acc[game_id] << message | |
acc | |
} | |
YAML.dump(game_id_messages, $stdout) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment