« [YouTube Api V3] DataAPI User IDからChannel ID を取得(その2) | トップページ | [AppleScript]小数点以下の切り上げと切り捨て »

[YouTube Api V3] DataAPI Channel ID からPlayList

API
https://developers.google.com/youtube/v3/docs/channelSections/list?apix=true
ChannelSections
https://developers.google.com/youtube/v3/docs/channelSections/list

https://youtube.googleapis.com/youtube/v3/channelSections?part=snippet%2CcontentDetails&channelId=[ strChannelID ]&key=[ strAPIkey ]



#!/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 "ここにAPIキー" as text

###URL取得
tell application "Safari"
activate
set the strURL to the URL of document 1 as text
end tell
try
#####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 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 strSnippet to snippet of jsonItems
set strChannelID to channelId of strSnippet as text
on error
try
#####URLからVideoIDを取得する
set AppleScript's text item delimiters to "https://www.youtube.com/user/"
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 ""
set strUserID to text item 1 of listQuery as text
####URL整形
set jsonURL to "https://www.googleapis.com/youtube/v3/channels?key=" & strAPIkey & "&forUsername=" & strUserID & "&part=id"
############コマンド整形
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 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
on error
try
#####URLからチャンネル名を取得する
set AppleScript's text item delimiters to "https://www.youtube.com/c/"
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 ""
set strUserID to text item 1 of listQuery as text
####URL整形
set jsonURL to "https://www.googleapis.com/youtube/v3/search?part=id%2Csnippet&maxResults=1&q=" & strUserID & "&type=channel&key=" & strAPIkey & ""
############コマンド整形
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 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 jsonIDs to |id| of jsonItems
set strChannelID to |channelId| of jsonIDs as text
on error
#####URLからチャンネル名を取得する
set AppleScript's text item delimiters to "https://www.youtube.com/channel/"
set listURL to every text item of strURL as list
set AppleScript's text item delimiters to ""
set strChannelID to text item 2 of listURL as text
end try
end try
end try

log strChannelID
########################
####チャンネルのプレイリスト一覧を取得する
####URL整形
set jsonURL to "https://youtube.googleapis.com/youtube/v3/channelSections?part=snippet%2CcontentDetails&channelId=" & strChannelID & "&key=" & strAPIkey & ""
####コマンド整形
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 ocidResJson to (objNSJSONSerialization's JSONObjectWithData:((objNSString's stringWithString:jsonResponse)'s dataUsingEncoding:(objMe's NSUTF8StringEncoding)) options:0 |error|:(missing value))
####
#####値を取り出す
set ocidResDictionary to objNSDictionary's dictionaryWithDictionary:ocidResJson
####キー項目を保存
set ocidResDictionaryKey to ocidResDictionary's allKeys()
####キー項目のログ用 ROOTキーが何個あって何なのか?を調べる用
repeat with ocidKey in ocidResDictionaryKey
log ocidKey as text
end repeat
########################
####itemのレコードを格納
set ocidItemsDictionary to ocidResDictionary's valueForKey:"items"
####リストの数
set numCntItems to count of ocidItemsDictionary
####シングルプレイリストの初期化
set ocidSingleplaylistID to objNSMutableArray's alloc()'s init()
####
repeat with objItemsDictionary in ocidItemsDictionary
###JSONのタイプを格納
set strSipType to type of snippet of objItemsDictionary as text
## log objItemsDictionary as list
###タイプ別の分岐
if strSipType is "recentuploads" then
set strRecentuploadsPlaylistsID to |id| of objItemsDictionary
## log strRecentuploadsPlaylistsID as text

else if strSipType is "channelsectiontypeundefined" then
set strUndefinedPlaylistsID to |id| of objItemsDictionary
## log strUndefinedPlaylistsID as text

else if strSipType is "popularuploads" then
set strPopularuploadsPlaylistsID to |id| of objItemsDictionary
## log strPopularuploadsPlaylistsID as text

else if strSipType is "multiplechannels" then
set strPopularuploadsPlaylistsID to channels of |contentDetails| of objItemsDictionary
## log strPopularuploadsPlaylistsID as list

else if strSipType is "multipleplaylists" then
set strPopularuploadsPlaylistsID to channels of |contentDetails| of objItemsDictionary
## log strPopularuploadsPlaylistsID as list

else if strSipType is "singleplaylist" then
set strSingleplaylistID to playlists of |contentDetails| of objItemsDictionary
(ocidSingleplaylistID's addObject:strSingleplaylistID)
end if

end repeat

log "strChannelID:" & strChannelID

log "#############対象ビデオの所有者がこのビデオを"
log "#############登録しているチャンネル"

repeat with objSingleplaylistID in ocidSingleplaylistID

set strPlistUrl to ("https://www.youtube.com/playlist?list=" & (objSingleplaylistID as text) & "") as text
log strPlistUrl

end repeat

|

« [YouTube Api V3] DataAPI User IDからChannel ID を取得(その2) | トップページ | [AppleScript]小数点以下の切り上げと切り捨て »

YouTube」カテゴリの記事