MetaCompN04.app
- http://iphlox.web.fc2.com/
- omtoolではなくて最新のopenmetaを同じフォルダに入れてください リンク先は上に張ってあります
- 「‐ニコニコ動画(」以降をカットして検索をかけますのでNNDDやsafari standでDLしたファイルをリネームすることなく処理できるようにしました
- 検索結果が0件のエラー処理を追記しました
- 検索のタイムアウト処理を追記しました
- 「短時間の連続アクセス」にならないように1ファイルに5秒のディレイをかけてます。
- tmpファイルをrmコマンドで消すことにしました finder制御で消すとゴミ箱に入れる音がするので
- 半角スペースを含むファイルを処理できるようになりました
- 処理できなかったファイルをお知らせします。
未決
- 混雑時のソースが手に入らなかったので、英語のエラーが出て止まります
- ライセンスとか表記しないとなぁ
以下ソース
--MetaCompN0.4
--ファイルをドロップするとニコニコ動画の情報を取得し、タグ付けします
---ファイル名をニコニコ動画で検索、html解析し、再生の多い動画IDを取得 要safariでログイン
---ニコニコ動画apiを呼んで情報取得
---コマンドラインopenmetaで書き込み
on open dropItems
set textMessage to ""
repeat with dropItem in dropItems
set resultCode to setTags(dropItem)
if resultCode is not 0 then
set textMessage to textMessage & resultCode & "は処理できませんでした" & (ASCII character 10)
end if
end repeat
beep 1
display dialog textMessage & "終了しました" buttons "OK"
end open
on setTags(dropItem)
tell application "Finder"
set tmpFolder to POSIX path of ((container of (path to me)) as alias)
set openmeta to tmpFolder & "openmeta" --openmetaパス
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
--「‐ニコニコ動画(」以降をカット
--NNDDやsafari standでDLしたファイルをリネームすることなく処理できる
--ちなみに私はrucaDownloaderを使ってます
set filename to deleteSuffix(filename) of me
end tell
--ファイル名から動画idを調べる
set targetURL to "http://www.nicovideo.jp/search/" & filename & "?sort=v"
set theSource to ""
tell application "Safari"
open location targetURL
delay 5
repeat with timer from 1 to 20
try
set theSource to (the source of document 1)
on error
delay 1
end try
end repeat
do JavaScript "window.close();" in document 1
end tell
--タイムアウトはぬける
if theSource is "" then return filename
--検索エラーになったときの文章をくれYO
--if busyResult is "混雑中" then return
--結果が0件ならぬける
set numResult to simpleTagParse(theSource, "検索結果:<strong style=\"color:#357;\">", "</strong> 件")
if numResult is "0" then return filename
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 openmeta & " -a " & dataTags & " -p \"" & targetFilePath & "\""
do shell script "rm \"" & tmp3 & "\""
do shell script "rm \"" & tmp4 & "\""
return 0
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
--引用元
--http://bowz.info/1244
--ちょっと改造
on deleteSuffix(this_text)
set y to 0
set z to length of this_text
repeat with thisChar in this_text
set y to y + 1
set x to the offset of thisChar in "‐ニコニコ動画("
if x is not 0 then
set z to y - 1
exit repeat
end if
end repeat
return (text from character 1 to z of this_text)
end deleteSuffix