« [YouTube Api V3] DataAPI User IDからChannel ID を取得(その1) | トップページ | [YouTube Api V3] DataAPI Channel ID からPlayList »

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

YouTubeの主要3タイプのURL形式
https://www.youtube.com/watch
https://www.youtube.com/user
https://www.youtube.com/c
ここから、チャンネルIDを取得するところまで
チャンネルIDが取得すれば
https://www.youtube.com/channel でチャンネルIDを使って色々出来る



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

log jsonResponse
log strChannelID

return
end try
end try

log strChannelID

####URL整形
set jsonURL to "https://youtube.googleapis.com/youtube/v3/channelSections?part=snippet%2CcontentDetails&channelId=" & strChannelID & "&key=" & strAPIkey & ""


|

« [YouTube Api V3] DataAPI User IDからChannel ID を取得(その1) | トップページ | [YouTube Api V3] DataAPI Channel ID からPlayList »

YouTube」カテゴリの記事