« [ffmpeg] タイムライン用サムネイル作成 | トップページ | YouTubeの『チャンネル』についてのメモ »

[YouTube Api V3] DataAPI PlayList から登録されている VideoID

注意点
プレイリストには『非公開』や『限定公開』のビデオも含まれているので
取得出来ない物が『ある』→エラー制御が必要



#!/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 objNSArray : a reference to objMe's NSArray
property objNSMutableArray : a reference to objMe's NSMutableArray

property objNSJSONSerialization : a reference to objMe's NSJSONSerialization

############設定項目 
####API ID
property strAPIkey : "ここにAPIキー " as text


####################################
property numMaxResults : 50 as number
####################################
property strVideoID : "" as text
property strPlayListID : "" as text
property strChannelID : "" as text
property strUserID : "" as text
property strUserName : "" as text
property strNextPageToken : "〓〓〓〓〓" as text
property listVideoID : {} as list


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

######
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 toQuery to strQuery 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)
end if
if (text item 1 of listObjQuery as text) is "list" then
####プレイリスト再生の場合のプレイリストID
set strPlayListID to (text item 2 of listObjQuery as text)
end if
end repeat
#############チャンネルIDで呼び出している場合
else if (text item 4 of listURL) starts with "playlist" then
set toQuery to strQuery 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 "list" then
####プレイリスト再生の場合のプレイリストID
set strPlayListID to (text item 2 of listObjQuery as text)
else if (text item 1 of listObjQuery as text) is "playlist?list" then
set strPlayListID to (text item 2 of listObjQuery as text)
end if
end repeat

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

###############################################################
###############################################################
if (strPlayListID as text) is "msng" then
set strPlayListID to "未設定です" as text
end if
log "カスタムURL" & strPlayListID
log "チャンネルID" & strPlayListID
log "処理終了"
###############################################################
###############################################################

set ocidPlayListVideoList to doGetPlayList2VideoID(strPlayListID, strNextPageToken)

set numTotalResults to item 1 of ocidPlayListVideoList as number
set strNextPageToken to item 2 of ocidPlayListVideoList as text

repeat
if strNextPageToken is "〓〓〓〓〓" then
log "EXIT:〓〓〓〓〓"
exit repeat
else
set ocidPlayListVideoList to doGetPlayList2VideoID(strPlayListID, strNextPageToken)
set strNextPageToken to item 2 of ocidPlayListVideoList as text
end if
end repeat
log listVideoID as list
### listVideoID に プレイリストのビデオIDがリストで入っている
###############################################################
###############################################################
### listVideoID のリスト数だけ処理を繰り返す
set strCnt4Log to 1
repeat with strVideoID in listVideoID

log strCnt4Log & ":" & strVideoID as text
set strCnt4Log to strCnt4Log + 1

end repeat

###ここまで
return
###############################################################
###############################################################
to doGetPlayList2VideoID(toPlayListID, toNextPageToken)
####NextPageTokenの有無でURLがかります
if toNextPageToken is "〓〓〓〓〓" then
####URL整形
set strPlaylistItemstUrl to ("https://www.googleapis.com/youtube/v3/playlistItems?part=snippet&playlistId=" & toPlayListID & "&key=" & strAPIkey & "&maxResults=" & numMaxResults & "") as text
else
####URL整形
set strPlaylistItemstUrl to ("https://www.googleapis.com/youtube/v3/playlistItems?part=snippet&playlistId=" & toPlayListID & "&key=" & strAPIkey & "&maxResults=" & numMaxResults & "&pageToken=" & toNextPageToken & "") as text
end if
####コマンド整形
set strCommandText to "/usr/bin/curl -X GET -H 'Content-Type: application/json;charset=UTF-8' \"" & strPlaylistItemstUrl & "\""
####コマンド実行
set jsonPlistResponse to (do shell script strCommandText) as text
####戻り値を格納
set ocidPlistResJson to (objNSJSONSerialization's JSONObjectWithData:((objNSString's stringWithString:jsonPlistResponse)'s dataUsingEncoding:(objMe's NSUTF8StringEncoding)) options:0 |error|:(missing value))
####プレイスとに含まれるビデオの本数
set strTotalResults to (totalResults of |pageInfo| of ocidPlistResJson) as text
set numTotalResults to strTotalResults as number
####50以上の場合次ページがあるのでnextPageToken
if numTotalResults > 50 then
###次のページ用のnextPageToken
try
set strNextPageToken to |nextPageToken| of ocidPlistResJson as text
on error
set strNextPageToken to "〓〓〓〓〓" as text
end try
else
set strNextPageToken to "〓〓〓〓〓" as text
end if
set ocidPlistItems to |items| of ocidPlistResJson


###Jsonitemsの数だけ繰り返す
repeat with objPlistItems in ocidPlistItems
set ocidVideoID to |videoId| of resourceId of snippet of objPlistItems
set strVideoID to ocidVideoID as text
copy strVideoID to end of listVideoID
end repeat
return {strTotalResults, (strNextPageToken as text)}
end doGetPlayList2VideoID

|

« [ffmpeg] タイムライン用サムネイル作成 | トップページ | YouTubeの『チャンネル』についてのメモ »

YouTube」カテゴリの記事