« [stringByAppendingString]文字列の結合 | トップページ | [sortDescriptorWithKey]並び替え(その2) »

[YouTube Api V3] DataAPI URLからChannel ID を取得(その1)

ビデオID
https://www.youtube.com/watch?v=XXXXXXX
Shorts
https://www.youtube.com/shorts/XXXXXXX
チャンネルID
https://www.youtube.com/channel/XXXXXXX
カスタムURL
https://www.youtube.com/c/XXXXXXX
ユーザーネーム
https://www.youtube.com/user/XXXXXXX



#!/usr/bin/env osascript
----+----1----+----2----+-----3----+----4----+----5----+----6----+----7
#
#
(*
API
https://developers.google.com/youtube/v3

コンソール
https://console.cloud.google.com/apis/dashboard?pli=1

*)
#
#
# com.cocolog-nifty.quicktimer.icefloe
----+----1----+----2----+-----3----+----4----+----5----+----6----+----7
use framework "Foundation"
use AppleScript version "2.8"
use scripting additions

property objMe : a reference to current application
property objNSString : a reference to objMe's NSString
property objNSJSONSerialization : a reference to objMe's NSJSONSerialization

############設定項目 
####API ID
property strAPIkey : "APIIDをここにペースト" as text


####################################
property numMaxResults : 50 as number
####################################
property strVideoID : ""
property strPlayListID : ""
property strChannelID : ""
property strUserID : ""
property strUserName : ""



###URL取得 Safari
try
tell application "Safari"
activate
set the strURL to the URL of document 1 as text
end tell
if strURL does not contain "www.youtube.com" then
error "" number -128
end if
on error
###URL取得Chrome
tell application "Google Chrome"
activate
tell window 1
tell active tab
set strURL to URL as text
end tell
end tell
end tell
if strURL does not contain "www.youtube.com" then
error "" number -128
end if
end try

log strURL as text

######
set AppleScript's text item delimiters to "/"
set listURL to every text item of strURL as list
set AppleScript's text item delimiters to ""
set strQuery to last text item of listURL as text
###############################################################
### チャンネルIDを取得する
###############################################################
if (text item 4 of listURL) starts with "watch" then
set strVideoID to text item 1 of doQuery2VideoID(strQuery) as text
set strChannelID to doGetChannelID(strVideoID) as text
####ブランドチャンネルにカスタムURLがあるか?確認
set strCustomUrl to doChkChanneCustomUrl(strChannelID) as text

#############チャンネルIDで呼び出している場合
else if (text item 4 of listURL) is "channel" then
set strChannelID to (text item 5 of listURL as text) as text
####ブランドチャンネルにカスタムURLがあるか?確認
set strCustomUrl to doChkChanneCustomUrl(strChannelID) as text

#############カスタムURLuserName)が設定されている場合
####カスタムURLはチャンネル登録100から
else if (text item 4 of listURL) is "c" then
set strCustomUrl to (text item 5 of listURL as text) as text
set strChannelID to doUserID2ChannelID(strCustomUrl) as text

#############ユーザーIDで呼び出している場合
####Legacy現在は新規では使われない=userある人は昔からの人
else if (text item 4 of listURL) is "user" then
set strUserName to (text item 5 of listURL as text) as text
set strChannelID to doUserName2ChannelID(strUserName) as text
####ブランドチャンネルにカスタムURLがあるか?確認
set strCustomUrl to doChkChanneCustomUrl(strChannelID) as text

#############ショート画面から呼び出し
else if (text item 4 of listURL) is "shorts" then
set strVideoID to text item 1 of doQuery2VideoID(strQuery) as text
set strChannelID to doGetChannelID(strVideoID) as text
####ブランドチャンネルにカスタムURLがあるか?確認
set strCustomUrl to doChkChanneCustomUrl(strChannelID) as text


####################################################
#############以下はチャンネルと関係ない
#############検索結果ページ
else if (text item 4 of listURL) starts with "results" then
return "検索結果からは取得できません"
else if (text item 4 of listURL) is "hashtag" then
return "hashtagページからは取得できません"
end if


###############################################################
###############################################################

log strChannelID as text

if (strCustomUrl as text) is "msng" then
set strCustomUrl to "未設定です" as text
end if


log "カスタムURL" & strCustomUrl
log "処理終了チャンネルID" & strChannelID

return

###############################################################
###############################################################


to doChkChanneCustomUrl(toChannelID)
set jsonChannelSnippetURL to "https://www.googleapis.com/youtube/v3/channels?&part=snippet&id=" & strChannelID & "&key=" & strAPIkey & "&maxResults=1" as text
############コマンド整形
set strCommandText to "/usr/bin/curl -X GET -H 'Content-Type: application/json;charset=UTF-8' \"" & jsonChannelSnippetURL & "\" --connect-timeout 20"
####コマンド実行
set jsonResponse to (do shell script strCommandText) as text
####戻り値を格納
set ocidResJson to (objNSJSONSerialization's JSONObjectWithData:((objNSString's stringWithString:jsonResponse)'s dataUsingEncoding:(objMe's NSUTF8StringEncoding)) options:0 |error|:(missing value))
################################
set jsonItems to |items| of ocidResJson
set jsonSnippet to snippet of jsonItems
try
set jsonCustomUrl to customUrl of jsonSnippet
on error
set jsonCustomUrl to "" as text
end try
return jsonCustomUrl
end doChkChanneCustomUrl

###############################################################
to doQuery2VideoID(toQuery)
set toQuery to toQuery as text
####watchがある=閲覧画面のVideoID
set AppleScript's text item delimiters to "&"
set listQuery to every text item of toQuery as list
set AppleScript's text item delimiters to ""
repeat with objQuery in listQuery
set AppleScript's text item delimiters to "="
set listObjQuery to every text item of objQuery as list
set AppleScript's text item delimiters to ""
if (text item 1 of listObjQuery as text) is "watch?v" then
####VideoID
set strVideoID to (text item 2 of listObjQuery as text)
else if (text item 1 of listObjQuery as text) is "v" then
####VideoID
set strVideoID to (text item 2 of listObjQuery as text)
else if objQuery is "list" then
####プレイリスト再生の場合のプレイリストID
set strPlayListID to (text item 2 of listObjQuery as text)
end if
end repeat
return {strVideoID, strPlayListID}
end doQuery2VideoID


###############################################################
to doGetChannelID(toVideoID)
set jsonURL to "https://www.googleapis.com/youtube/v3/videos?part=snippet&id=" & toVideoID & "&key=" & strAPIkey & "&maxResults=50"
############コマンド整形
set strCommandText to "/usr/bin/curl -X GET -H 'Content-Type: application/json;charset=UTF-8' \"" & jsonURL & "\" --connect-timeout 20"
####コマンド実行
set jsonResponse to (do shell script strCommandText) as text
####戻り値を格納
set ocidVideoResJson to (objNSJSONSerialization's JSONObjectWithData:((objNSString's stringWithString:jsonResponse)'s dataUsingEncoding:(objMe's NSUTF8StringEncoding)) options:0 |error|:(missing value))
################################
set jsonItems to |items| of ocidVideoResJson
#####: snippet
set jsonSnippet to snippet of jsonItems
#####: channelId
set strChannelID to channelId of jsonSnippet as text
return strChannelID
end doGetChannelID


###############################################################
to doUserID2ChannelID(toCustomUrl)
set jsonChannelsURL to "https://www.googleapis.com/youtube/v3/search?part=snippet&q=" & toCustomUrl & "&type=channel&key=" & strAPIkey & "&maxResults=1"
############コマンド整形
set strCommandText to "/usr/bin/curl -X GET -H 'Content-Type: application/json;charset=UTF-8' \"" & jsonChannelsURL & "\""
####コマンド実行
set jsonResponse to (do shell script strCommandText) as text
####戻り値を格納
set ocidResJson to (objNSJSONSerialization's JSONObjectWithData:((objNSString's stringWithString:jsonResponse)'s dataUsingEncoding:(objMe's NSUTF8StringEncoding)) options:0 |error|:(missing value))
################################
set jsonItems to |items| of ocidResJson
set strChannelID to (channelId of |id| of jsonItems) as text
return strChannelID
end doUserID2ChannelID


###############################################################
to doUserName2ChannelID(toUserName)
set jsonURL to "https://www.googleapis.com/youtube/v3/channels?key=" & strAPIkey & "&forUsername=" & toUserName & "&part=id&maxResults=" & numMaxResults & ""
############コマンド整形
set strCommandText to "/usr/bin/curl -X GET -H 'Content-Type: application/json;charset=UTF-8' \"" & jsonURL & "\" --connect-timeout 20"
####コマンド実行
set jsonResponse to (do shell script strCommandText) as text
####戻り値を格納
set ocidResJson to (objNSJSONSerialization's JSONObjectWithData:((objNSString's stringWithString:jsonResponse)'s dataUsingEncoding:(objMe's NSUTF8StringEncoding)) options:0 |error|:(missing value))
################################
set jsonItems to |items| of ocidResJson
set strChannelID to |id| of jsonItems as text
return strChannelID
end doUserName2ChannelID

|

« [stringByAppendingString]文字列の結合 | トップページ | [sortDescriptorWithKey]並び替え(その2) »

YouTube」カテゴリの記事