LoginSignup
10
6

More than 5 years have passed since last update.

アイマス楽曲のタイトル検索API(もどき)を作った

Last updated at Posted at 2016-02-26

動機

LTHの高山紗代子ソロ曲が vivid color か Vivid Color か VIVID COLOR だったか分からなくなり、コマンドラインからアイマス楽曲の正式名称を検索できるようにしたかった。

実装方法

つくったもの

gomao9/imas-songs

サーバ

app.rb

require 'sinatra'
require 'nokogiri'
require 'open-uri'
require 'json'
require 'unicode'

URL ='http://imas-db.jp/song/detail/'

get '/search' do
  title(params['keyword']).to_json
end

helpers do
  def title keyword
    html = open(URL).read
    doc = Nokogiri::HTML.parse(html)
    songs = doc.css('div.section > ul > li').map(&:text).to_a
    songs.select do |song|
      Unicode::nfkc(song).include?  Unicode::nfkc(keyword)
    end
  end
end

ローカルスクリプト

imas_songs.rb
#! /usr/bin/env ruby
require 'json'
require 'uri'
require 'open-uri'

keyword = ARGV.first
url = "https://imas-song-title.herokuapp.com/search?keyword=#{keyword}"
songs = JSON.parse(open(URI.escape url).read)
puts songs
$ ./imas_songs.rb vivid
vivid color
10
6
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
10
6