[YouTube Api V3] DataAPI JSONを利用して各種値を取得する
Data API Guides videos
https://developers.google.com/youtube/v3/guides/implementation/videos
APIダッシュボード
https://console.cloud.google.com/apis/dashboard
ブラウザやコマンドからデータを取得する『だけ』なので認証関係は不要です
#!/usr/bin/env osascript
----+----1----+----2----+-----3----+----4----+----5----+----6----+----7
#
#
#
#
# com.cocolog-nifty.quicktimer.icefloe
----+----1----+----2----+-----3----+----4----+----5----+----6----+----7
use framework "Foundation"
use AppleScript version "2.4"
use scripting additions
property objMe : a reference to current application
property objNSString : a reference to objMe's NSString
property objNSURL : a reference to objMe's NSURL
property objNSDictionary : a reference to objMe's NSDictionary
property objNSMutableArray : a reference to objMe's NSMutableArray
property objNSJSONSerialization : a reference to objMe's NSJSONSerialization
############設定項目 API ID
set strAPIkey to "APIIDをここに入力" as text
###URL取得
tell application "Safari"
activate
set the strURL to the URL of document 1 as text
end tell
#####URLからVideoIDを取得する
set AppleScript's text item delimiters to "watch?"
set listURL to every text item of strURL as list
set AppleScript's text item delimiters to ""
set strQuery to text item 2 of listURL as text
set AppleScript's text item delimiters to "&"
set listQuery to every text item of strQuery as list
set AppleScript's text item delimiters to ""
#####Video id を取得
repeat with objQuery in listQuery
set AppleScript's text item delimiters to "="
set listURLQuery to every text item of objQuery as list
set AppleScript's text item delimiters to ""
set strParams to text item 1 of listURLQuery as text
if strParams is "v" then
set strVideoID to (text item 2 of listURLQuery) as text
end if
end repeat
####URL整形
set jsonURL to "https://www.googleapis.com/youtube/v3/videos?id=" & strVideoID & "&key=" & strAPIkey & "&part=snippet,contentDetails,statistics,status"
############コマンド整形
set strCommandText to "/usr/bin/curl -X GET -H 'Content-Type: application/json;charset=UTF-8' \"" & jsonURL & "\""
####コマンド実行
set jsonResponse to (do shell script strCommandText) as text
####戻り値を格納
set strResJson to (objNSJSONSerialization's JSONObjectWithData:((objNSString's stringWithString:jsonResponse)'s dataUsingEncoding:(objMe's NSUTF8StringEncoding)) options:0 |error|:(missing value))
################################
####:items
set jsonItems to |items| of strResJson
####:snippet
set jsonSnippet to snippet of jsonItems
####: title
set strTitle to title of jsonSnippet as text
set strChannelId to channelId of jsonSnippet as text
####:thumbnails
set jsonThumbnails to thumbnails of jsonSnippet
####:medium
set jsonMedium to medium of jsonThumbnails
####
set strThumbnailsURL to |url| of jsonMedium as text
####オリジナルの幅と高さを取得する場合はこちら
##set strThumbnailsWidth to width of jsonMedium as text
##set strThumbnailsHeight to height of jsonMedium as text
################################
###リンク用のURLを作成
set strLinkURL to ("https://youtu.be/" & strVideoID & "") as text
####画像の幅と高さを指定
set strThumbnailsWidth to "512" as text
set strThumbnailsHeight to "288" as text
####HTML整形
set strHTML to "<script src=\"https://apis.google.com/js/platform.js\"></script><a href=\"" & strLinkURL & "\" target=\"_blank\" rel=\"noindex\" title=\"" & strTitle & "\"><img src=\"" & strThumbnailsURL & "\" alt=\"" & strTitle & "\" width=\"" & strThumbnailsWidth & "\" height=\"" & strThumbnailsHeight & "\"></a><br clear=\"all\"><div class=\"g-ytsubscribe\" data-channelid=\"" & strChannelId & "\" data-layout=\"default\" data-count=\"default\"></div>" as text
log strHTML
################################
####ペーストボードに格納
####テキストをNSStringに
set ocidText to objNSString's stringWithString:(strHTML)
####ペーストボードを定義
set objPasteboard to objMe's NSPasteboard's generalPasteboard()
####ペーストボード初期化
objPasteboard's clearContents()
####テキストを格納
objPasteboard's writeObjects:{objMe's NSAttributedString's alloc()'s initWithString:ocidText}
####ペーストボードの内容をテキストで確定
set ocidPbData to objPasteboard's dataForType:(objMe's NSPasteboardTypeString)
| 固定リンク
「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)