Created
September 25, 2008 08:02
-
-
Save hotchpotch/12782 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
#!/usr/bin/env ruby | |
$LOAD_PATH << '/opt/local/lib/ruby/gems/1.8/gems/rubyosa-0.4.0/lib' | |
require 'rbosa' | |
require 'pp' | |
require 'nkf' | |
$KCODE = 'u' | |
OSA.utf8_strings = true | |
class ArtistRubyList | |
attr_accessor :list | |
def initialize(list) | |
@list = list | |
end | |
def match(name) | |
res = @list[name] | |
if !res && s = name.split(/\s/)[0] | |
# 適当なあいまいマッチ | |
# 天海春香 (中村繪理子) & 高槻やよい (仁後真耶子) => あまみはるか | |
# とかになるようにする | |
if s.scan(/./).length >= 3 | |
res = @list[s] | |
@list[name] = res if res | |
end | |
end | |
res | |
end | |
def self.hatena_keywords(csv) | |
list = {} | |
csv.split("\n").each do |line| | |
val, key = NKF.nkf('-m0 -Ew', line).split("\t", 2) | |
list[key] = val | |
end | |
new list | |
end | |
end | |
OSA::ObjectSpecifierList.extend Enumerable | |
itunes = OSA.app('iTunes') | |
utf8 = /\A[\xc0-\xdf][\x80-\xbf]|[\xe0-\xef][\x80-\xbf]{2}|[\xf0-\xf7][\x80-\xbf]{3}|[\xf8-\xfb][\x80-\xbf]{4}|[\xfc-\xfd][\x80-\xbf]{5}/xn | |
ruby_set = [] | |
ruby_not_set = [] | |
k = ArtistRubyList.hatena_keywords(open('keywordlist.csv').read) | |
# | |
# 範囲選択してたらそちら(1つだけ選択してるのは除く)を、選択してなかったら現在のプレイリストを対象とする | |
(itunes.selection.length > 1 ? itunes.selection : itunes.current_playlist.tracks). | |
each do |track| | |
artist = track.artist | |
if artist.match(utf8) && track.sort_artist.to_s.length == 0 | |
yomi = k.match(artist) || k.match(artist.gsub(/\s/, '')) | |
if yomi | |
puts "#{artist} => #{yomi}" | |
begin | |
track.sort_artist = yomi | |
ruby_set << yomi | |
rescue RuntimeError => e | |
puts e.inspect | |
end | |
else | |
puts "!#{artist}" | |
ruby_not_set << yomi | |
end | |
end | |
end | |
puts "ふり仮名を設定したアーティスト #{ruby_set.length}" | |
puts "ふり仮名が解らなかったアーティスト #{ruby_not_set.length}" | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment