youtubeのサムネイル画像をダウンロードする
#!/usr/bin/env osascript
----+----1----+----2----+-----3----+----4----+----5----+----6----+----7
#
# com.cocolog-nifty.quicktimer.icefloe
----+----1----+----2----+-----3----+----4----+----5----+----6----+----7
use AppleScript version "2.8"
use framework "Foundation"
use framework "AppKit"
use scripting additions
property refMe : a reference to current application
property refNSNotFound : a reference to 9.22337203685477E+18 + 5807
########################
## クリップボードの中身取り出し
###初期化
set appPasteboard to refMe's NSPasteboard's generalPasteboard()
set ocidPastBoardTypeArray to appPasteboard's types
###テキストがあれば
set boolContain to ocidPastBoardTypeArray's containsObject:"public.utf8-plain-text"
if boolContain = true then
###値を格納する
tell application "Finder"
set strReadString to (the clipboard as text) as text
end tell
###Finderでエラーしたら
else
set boolContain to ocidPastBoardTypeArray's containsObject:"NSStringPboardType"
if boolContain = true then
set ocidReadString to ocidPasteboard's readObjectsForClasses:({refMe's NSString}) options:(missing value)
set strReadString to ocidReadString as text
else
log "テキストなし"
set strReadString to "YouTubeのビデオURLを入力" as text
end if
end if
########################
##ダイアログを前面に出す
set strName to (name of current application) as text
####スクリプトメニューから実行したら
if strName is "osascript" then
tell application "Finder" to activate
else
tell current application to activate
end if
########################
##ダイアログ
set aliasIconPath to (POSIX file "/System/Library/CoreServices/Tips.app/Contents/Resources/AppIcon.icns") as alias
set strTitle to ("入力してください") as text
set strMes to ("YouTubeURLを入力してください\r") as text
set recordResult to (display dialog strMes with title strTitle default answer strReadString buttons {"キャンセル", "OK"} default button "OK" cancel button "キャンセル" giving up after 30 with icon aliasIconPath without hidden answer)
if (gave up of recordResult) is true then
return "時間切れです"
else if (button returned of recordResult) is "キャンセル" then
return "キャンセルです"
else
set strReturnedText to (text returned of recordResult) as text
end if
set ocidReturnedText to refMe's NSString's stringWithString:(strReturnedText)
########################
##改行を取る
set ocidURLStr to ocidReturnedText's stringByReplacingOccurrencesOfString:("\r") withString:("")
set ocidURLStr to ocidURLStr's stringByReplacingOccurrencesOfString:("\n") withString:("")
########################
##NSURLに
set ocidURL to refMe's NSURL's alloc()'s initWithString:(ocidURLStr)
##NSURLComponentsに分解
set ocidURLComponents to refMe's NSURLComponents's componentsWithURL:(ocidURL) resolvingAgainstBaseURL:(true)
########################
##以下はチャンネルと関係ない
##ホスト判定
set strHost to ocidURL's |host|() as text
if strHost is not "www.youtube.com" then
return "Youtube URL 専用"
end if
##パス判定
set strPath to ocidURL's |path|() as text
if strPath starts with "/watch" then
log "ビデオぺージのみ処理する"
else if strPath starts with "/feed" then
return "feedぺージは処理しない"
else if strPath starts with "/results" then
return "resultsぺージは処理しない"
else if strPath starts with "/hashtag" then
return "hashtagぺージは処理しない"
else if strPath is "" then
return "トップぺージは処理しない"
else if strPath is "/" then
return "トップぺージは処理しない"
end if
########################
##クエリーの有無をチェック
set recordRange to ocidURLComponents's rangeOfQuery()
set ocidLocation to location of recordRange
##
if ocidLocation = refNSNotFound then
if strPath starts with "/shorts" then
set strVideoID to ocidURL's lastPathComponent() as text
else if strPath starts with "/embed" then
set strVideoID to ocidURL's lastPathComponent() as text
else
return "クエリーの無いURLは処理しない"
end if
else
########################
##【Aクエリーあり】
set ocidQueryArray to ocidURLComponents's queryItems
##【Aー1】ラストパスで判定
set strLastPath to ocidURL's lastPathComponent() as text
##【Aー1−1】ビデオ再生のwatchの場合
if strLastPath is "watch" then
##queryItemsから各種ID
repeat with itemQuery in ocidQueryArray
set strQueryName to itemQuery's |name| as text
if strQueryName is "v" then
#再生中ビデオID
set strVideoID to itemQuery's value as text
else if strQueryName is "list" then
#プレイリストのID
set strPlayListID to itemQuery's value as text
else if strQueryName is "index" then
#プレイリストの曲順番号
set strPlayListIndex to itemQuery's value as text
end if
end repeat
##【Aー1−2】プレイリスト画面の場合
else if strLastPath is "playlist" then
repeat with itemQuery in ocidQueryArray
set strQueryName to itemQuery's |name| as text
if strQueryName is "list" then
#プレイリストのID
set strPlayListID to itemQuery's value as text
end if
end repeat
end if
end if
###保存先 ダウンロード
set appFileManager to refMe's NSFileManager's defaultManager()
set ocidURLsArray to (appFileManager's URLsForDirectory:(refMe's NSDownloadsDirectory) inDomains:(refMe's NSUserDomainMask))
set ocidDownloadsDirPathURL to ocidURLsArray's firstObject()
set strMakeDirName to ("www.youtube.com/" & strVideoID) as text
set ocidSaveDirPathURL to ocidDownloadsDirPathURL's URLByAppendingPathComponent:(strMakeDirName)
###フォルダ作成
set ocidAttrDict to refMe's NSMutableDictionary's alloc()'s initWithCapacity:0
ocidAttrDict's setValue:(448) forKey:(refMe's NSFilePosixPermissions)
set listBoolMakeDir to appFileManager's createDirectoryAtURL:(ocidSaveDirPathURL) withIntermediateDirectories:true attributes:(ocidAttrDict) |error|:(reference)
##パスとエイリアス
set strSaveDirPath to (ocidSaveDirPathURL's |path|()) as text
set aliasSaveDirPath to (ocidSaveDirPathURL's absoluteURL()) as alias
###ファイル名
set listFileName to {"frame0", "default", "mqdefault", "hqdefault", "sddefault", "maxresdefault", "0", "1", "2", "3", "hq720", "hq720_1", "hq720_2", "hq720_3", "maxres3", "maxres2", "maxres1", "mq1", "mq2", "mq3", "oar1", "oar2", "oar3", "mqdefault_6s", "oardefault"} as list
##ファイル名の数だけ繰り返し
repeat with itemFileName in listFileName
###JPEGサムネイル
set strFileNameJpeg to (itemFileName & ".jpg") as text
set strSaveFilePathJpeg to (strSaveDirPath & "/" & strFileNameJpeg) as text
set numServerNo to ("") as text
set strBaseUrlJpeg to ("https://i" & numServerNo & ".ytimg.com/vi/") as text
set strURLjpeg to (strBaseUrlJpeg & strVideoID & "/" & strFileNameJpeg) as text
set strCommandText to ("/usr/bin/curl -L \"" & strURLjpeg & "\" -o \"" & strSaveFilePathJpeg & "\"") as text
do shell script strCommandText
####WEBPサムネイル
set numServerNo to ("") as text
set strBaseUrlWebp to ("https://i" & numServerNo & ".ytimg.com/vi_webp/") as text
set strFileNameWebp to (itemFileName & ".webp") as text
set strURLwebp to (strBaseUrlWebp & strVideoID & "/" & strFileNameWebp) as text
set strSaveFilePathWebp to (strSaveDirPath & "/" & strFileNameWebp) as text
set strCommandText to ("/usr/bin/curl -L \"" & strURLwebp & "\" -o \"" & strSaveFilePathWebp & "\"") as text
do shell script strCommandText
end repeat
##保存先を開く
set appShardWorkspace to refMe's NSWorkspace's sharedWorkspace()
set boolResults to appShardWorkspace's openURL:(ocidSaveDirPathURL)
if boolResults is false then
tell application "Finder"
make new Finder window to aliasSaveDirPath
end tell
end if
| 固定リンク
「YouTube」カテゴリの記事
- [DaVinci Resolve]DaVinci Resolveから書き出したVTTにLineポジションを付与する(2024.07.14)
- [Youtube API]チャンネルIDからチャンネルのアップロードビデオを取得する(2024.03.15)
- [YoutubeAPI] チャンネルIDからチャンネルのアップロードビデオのリストIDを取得する(2024.03.14)
- [YoutubeAPI] チャンネルIDからカスタムURL=ユーザー名(アカウント名)を取得(2024.03.14)
- [Youtube]ビデオIDから主要な情報を取得(2024.01.13)