« [YouTube Api V3] DataAPI JSONを利用して各種値を取得する | トップページ | [YouTube Api V3] DataAPI User IDからChannel ID を取得(その2) »

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

forUsername を使います
https://developers.google.com/youtube/v3/docs/channels/


#!/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 strResJson to (objNSJSONSerialization's JSONObjectWithData:((objNSString's stringWithString:jsonResponse)'s dataUsingEncoding:(objMe's NSUTF8StringEncoding)) options:0 |error|:(missing value))
################################
set jsonItems to |items| of strResJson
set strSnippet to snippet of jsonItems
set strChannelID to channelId of strSnippet as text
on error
#####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 strResJson to (objNSJSONSerialization's JSONObjectWithData:((objNSString's stringWithString:jsonResponse)'s dataUsingEncoding:(objMe's NSUTF8StringEncoding)) options:0 |error|:(missing value))
################################
set jsonItems to |items| of strResJson
set strChannelID to |id| of jsonItems as text
end try


log strChannelID

|

« [YouTube Api V3] DataAPI JSONを利用して各種値を取得する | トップページ | [YouTube Api V3] DataAPI User IDからChannel ID を取得(その2) »

YouTube」カテゴリの記事