ニコ動からローカルにDLしたファイルに親サイトと同じOpenMetaタグを付けるドロップレット

やっと動くようになったのでメモとして残しておくけど、ろくにテストしてない暫定版なのでわかる人以外は使わないでください

  • 使用方法と処理内容
    • たとえばローカルに「【鏡音リン炉心融解【オリジナル】.mp4」があったとします
    • このファイルをappにドロップするとOpenMetaタグに書き込んでくれます(複数対応)
    • まず、「【鏡音リン炉心融解【オリジナル】」をニコニコ動画のwebアクセスで検索、結果htmlを解析し、再生の多い動画IDを取得
    • ニコニコ動画apiを呼んでタグ情報を取得
    • omtoolを使ってOpenMetaタグに書き込み
  • 確認方法
    • spotlightで「tag:vo」で引っかかるでしょう(VOCALOID殿堂入りにヒット)
    • 同様に「音楽」「鏡音リン」「kuma」「なぎみそ。SYS」「iroha(sasaki)」(敬称略)等のタグが登録されているはずです
    • finderのサイドバーにスマート検索フォルダにして置いておくといいかも
    • Tagitで二回検索すれば出るようになるようです
  • 残念ながら非対応
    • omtoolはcom.openmeta.shared.plistに書き込んでくれないので、s-take氏のOMWizard1.1ではタグ履歴に出てきません
  • テストしてくださった方には感謝感謝です
    • バグレポート、動作レポート、感想等、コメントに残していただけるととてもうれしいです
  • 次はpixiv版を作る予定

MetaCompN03.app

--MetaCompN0.3

--ファイルをドロップするとニコニコ動画の情報を取得し、タグ付けします
---ファイル名をニコニコ動画で検索、html解析し、再生の多い動画IDを取得 要safariでログイン
---ニコニコ動画apiを呼んで情報取得
---omtoolで書き込み

on open dropItems
	repeat with dropItem in dropItems
		
		setTags(dropItem)
		
	end repeat
end open


on setTags(dropItem)
	
	tell application "Finder"
		set tmpFolder to POSIX path of ((container of (path to me)) as alias)
		set omtool to tmpFolder & "omtool" --omtoolパス
		set tmp3 to tmpFolder & "tmp3"
		set tmp4 to tmpFolder & "tmp4"
		set ref3 to tmp3 as POSIX file
		set ref4 to tmp4 as POSIX file
		
		set targetFilePath to POSIX path of dropItem
		set filename to name of dropItem
		set filename to retFileNameWithoutExt(filename) of me
		
	end tell
	
	--ファイル名から動画idを調べる
	set targetURL to "http://www.nicovideo.jp/search/" & filename & "?sort=v"
	tell application "Safari"
		open location targetURL
		--タイムアウトを考慮してない
		delay 5
		try
			set theSource to (the source of document 1)
		on error
			beep 2
			return
		end try
		do JavaScript "window.close();" in document 1
	end tell
	
	set theSource to simpleTagParse(theSource, "<div id=\"item1\"", "</div>")
	set movieId to simpleTagParse(theSource, "<a href=\"watch/", "\">")
	
	--ニコニコ動画api	
	set targetURL to "http://www.nicovideo.jp/api/getthumbinfo/" & movieId
	try
		tell application "URL Access Scripting"
			activate
			download targetURL to file tmp3 replacing yes with progress
			quit
		end tell
	on error errorNum
		display dialog errorNum
	end try
	
	try
		do shell script "iconv -c -f UTF-8 -t SHIFT_JIS " & tmp3 & " > " & tmp4
		-- -cはエラー継続 tryで囲えばnon-zeroエラーを抑制できた
	end try
	
	open for access ref4
	try
		read ref4
		set dataXML to result
	on error
		close access ref4
		return
	end try
	close access ref4
	
	set dataTags to simpleTagParse(dataXML, "<tags domain=\"jp\">", "</tags>")
	set dataTags to replaceText(dataTags, "<tag lock=\"1\">", "\"")
	set dataTags to replaceText(dataTags, "<tag>", "\"")
	set dataTags to replaceText(dataTags, "</tag>", "\"")
	set dataTags to replaceText(dataTags, (ASCII character 10), " ")
	
	do shell script omtool & " -a " & dataTags & " -p " & targetFilePath
	
	tell application "Finder"
		delete ref3
		delete ref4
	end tell
	
end setTags


--引用元
--http://homepage.mac.com/travellers/blog/C746134881/E441300314/index.html
--ちょっと改造
on simpleTagParse(tmpData, beginKey, endKey)
	set oldDelim to AppleScript's text item delimiters
	try
		set AppleScript's text item delimiters to beginKey
		set tmpData2 to second text item of tmpData
		set AppleScript's text item delimiters to endKey
		set tmpData3 to (first text item of tmpData2) as Unicode text
		set AppleScript's text item delimiters to oldDelim
		return tmpData3
	on error
		if tmpData does not contain beginKey then
			log "The key doesn't exist in the data"
		end if
		set AppleScript's text item delimiters to oldDelim
		return missing value
	end try
end simpleTagParse

--引用元
--http://www.tonbi.jp/AppleScript/tips/String/FindReplace.html
on replaceText(theText, serchStr, replaceStr)
	set tmp to AppleScript's text item delimiters
	set AppleScript's text item delimiters to serchStr
	set theList to every text item of theText
	set AppleScript's text item delimiters to replaceStr
	set theText to theList as string
	set AppleScript's text item delimiters to tmp
	return theText
end replaceText

--引用元
--http://piyocast.com/as/archives/397
on retFileNameWithoutExt(fileNameStr)
	set fLen to length of fileNameStr
	set revText to (reverse of (characters of fileNameStr)) as string
	set anOffset to offset of "." in revText
	set fRes to text 1 thru (fLen - anOffset) of fileNameStr
	return fRes
end retFileNameWithoutExt