« [YouTube Api] oEmbed JSONを利用して各種値を取得する | トップページ | [Audio]オーディオ投稿テスト »

[YouTube Api] oEmbed XMLを利用して各種値を取得する

oEmbed support
https://youtube-eng.googleblog.com/2009/10/oembed-support_9.html
oEmbed.com
https://oembed.com/

Getで取得出来るので便利
XML形式
https://www.youtube.com/oembed?url=https://youtube.com/watch?v=ビデオID&format=xml
json形式
https://www.youtube.com/oembed?url=https://youtube.com/watch?v=ビデオID&format=json

XML形式はそのまま日本語が通るので使いやすい


#!/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

###oEmbedURL
set strBaseUrl to "https://www.youtube.com/oembed?url=http://www.youtube.com/watch?v="
###クロームで開いているYouTubeページのURLを取得

tell application "Google Chrome"
activate
set strURL to URL of active tab of front window as Unicode 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 ""

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
############JSONデータをGETする
###URL整形
set strJsonUrl to ("" & strBaseUrl & strVideoID & "&format=xml") as text
###コマンド整形
set strCommandText to "/usr/bin/curl -X GET -H 'Content-Type: application/xml;charset=UTF-8' \"" & strJsonUrl & "\" --connect-timeout 20"
###コマンド実行
set xmlResponse to (do shell script strCommandText) as text
#####戻り値のXMLの処理
tell application "System Events"
set strXMLData to make new XML data with properties {text:xmlResponse}
tell strXMLData
tell XML element "oembed"
set strTitle to value of XML element "title" as text
set strThumbnailUrl to value of XML element "thumbnail_url" as text
set strThumbnailHeight to value of XML element "thumbnail_height" as text
set strThumbnailWidth to value of XML element "thumbnail_width" as text
set strAuthorUrl to value of XML element "author_url" as text
end tell
end tell
end tell
#####URLを分解してチャンネルIDを取得
set AppleScript's text item delimiters to "/"
set listAuthorUrl to every text item of strAuthorUrl as list
set AppleScript's text item delimiters to ""
set strAuthorID to last text item of listAuthorUrl as text


############
###HTML整形
set strHTML to "<script src=\"https://apis.google.com/js/platform.js\"></script><a href=\"" & strURL & "\" target=\"_blank\" rel=\"noindex\" title=\"" & strTitle & "\"><img src=\"" & strThumbnailUrl & "\" alt=\"" & strTitle & "\" width=\"" & strThumbnailWidth & "\" height=\"" & strThumbnailHeight & "\"></a><br clear=\"all\"><div class=\"g-ytsubscribe\" data-channel=\"" & strAuthorID & "\" 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 Api] oEmbed JSONを利用して各種値を取得する | トップページ | [Audio]オーディオ投稿テスト »

YouTube」カテゴリの記事