« [clipboard] NSPasteboard | トップページ | [YouTube Api] oEmbed XMLを利用して各種値を取得する »

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

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

デメリットは日本語がUNICODEエスケープされる



json形式


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


###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=json") as text
###コマンド整形
set strCommandText to "/usr/bin/curl -X GET -H 'Content-Type: application/json;charset=UTF-8' \"" & strJsonUrl & "\" --connect-timeout 20"
###コマンド実行
set jsonResponse to (do shell script strCommandText) as text
####戻り値=JSONを格納
set strResJson to (objNSJSONSerialization's JSONObjectWithData:((objNSString's stringWithString:jsonResponse)'s dataUsingEncoding:(objMe's NSUTF8StringEncoding)) options:0 |error|:(missing value))
####JSON分解して各種値を取得
set strThumbnailUrl to thumbnail_url of strResJson as text
set strThumbnailWidth to thumbnail_width of strResJson as text
set strThumbnailHeight to thumbnail_height of strResJson as text
####
try
set strAuthorUrl to author_url of strResJson as text
set AppleScript's text item delimiters to "https://www.youtube.com/channel/"
set listAuthorUrl to every text item of strAuthorUrl as list
set strAuthorID to text item 2 of listAuthorUrl as text
set AppleScript's text item delimiters to ""
set strDataCh to "channelid"
on error
set strAuthorUrl to author_url of strResJson as text
set AppleScript's text item delimiters to "https://www.youtube.com/user/"
set listAuthorUrl to every text item of strAuthorUrl as list
set strAuthorID to text item 2 of listAuthorUrl as text
set AppleScript's text item delimiters to ""
set strDataCh to "channel"
end try


################
####タイトル部はUNICODEエスケープしているのでデコードする
set strUnicodeTitle to title of strResJson as text
set strTitle to ((objNSString's stringWithString:strUnicodeTitle)'s stringByApplyingTransform:"Hex-Any" |reverse|:false) 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-" & strDataCh & "=\"" & 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)


|

« [clipboard] NSPasteboard | トップページ | [YouTube Api] oEmbed XMLを利用して各種値を取得する »

YouTube」カテゴリの記事

NSString stringByApplyingTransform」カテゴリの記事