Skip to content

Instantly share code, notes, and snippets.

@kaosf
Created May 26, 2023 11:21
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 kaosf/99908f4e4f38156a16766c8384d4958b to your computer and use it in GitHub Desktop.
Save kaosf/99908f4e4f38156a16766c8384d4958b to your computer and use it in GitHub Desktop.
AikatsUP search core by Ruby
require "digest"
require "open-uri"
FILEPATH = "./aikatsup20230422.csv"
EXPECTED_SHA256 = "14648f557a2ad394b513c0800afd3b1f4d1ee23964f501820a392cb6779a9a19"
def download_from_github_and_succeeded
URI.open("https://raw.githubusercontent.com/kaosf/AikatsuDOWN/master/data/aikatsup20230422.csv") do |file|
File.open(FILEPATH, 'wb') do |output|
output.write(file.read)
end
end
File.exist?(FILEPATH) && Digest::SHA256.file(FILEPATH).hexdigest == EXPECTED_SHA256
rescue
false
end
def download_from_source_and_succeeded
URI.open("http://aikatsup.com/static/aikatsup20230422.csv") do |file|
File.open(FILEPATH, 'wb') do |output|
output.write(file.read)
end
end
File.exist?(FILEPATH) && Digest::SHA256.file(FILEPATH).hexdigest == EXPECTED_SHA256
rescue
false
end
def prepare
return if File.exist?(FILEPATH) && Digest::SHA256.file(FILEPATH).hexdigest == EXPECTED_SHA256
return if download_from_github_and_succeeded
return if download_from_source_and_succeeded
$stderr.puts "CSV file could not be prepared."
exit 1
end
prepare
if ARGV.size < 2
$stderr.puts "Usage: ruby aikatsup.rb 検索ワード Type=(キャラ|セリフ)"
$stderr.puts "Example 1: ruby aikatsup.rb 大空あかり キャラ"
$stderr.puts "Example 2: ruby aikatsup.rb 穏やかじゃない セリフ"
exit 1
end
keyword = ARGV[0]
type = ARGV[1]
require "csv"
table = CSV.read(FILEPATH, headers: true)
ids =
case type
when "キャラ"
table.filter { _1[4..].include?(keyword) }.map { _1["ID"] }
when "セリフ"
table.filter { _1["Words"] =~ /#{keyword}/ }.map { _1["ID"] }
else
$stderr.puts "Invalid type."
exit 1
end
p ids
ruby aikatsup.rb 大空あかり キャラ
["19", "20", "23", "36", "37", "38", "41", ... "4315", "4318", "4324"]
ruby aikatsup.rb 穏やかじゃない セリフ
["35", "79", "167", "268", "368", "377", "383", ... "3586", "3784", "4202"]
@kaosf
Copy link
Author

kaosf commented May 26, 2023

@kaosf
Copy link
Author

kaosf commented May 26, 2023

ruby -v
ruby 3.2.2 (2023-03-30 revision e51014f9c0) [x86_64-linux]

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment