« [Edge]URLを開く | トップページ | [YoutubeAPI] チャンネルIDからチャンネルのアップロードビデオのリストIDを取得する »

[YoutubeAPI] チャンネルIDからカスタムURL=ユーザー名(アカウント名)を取得


【スクリプトエディタで開く】|

#!/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 refMe : a reference to current application

############設定項目 
####API key
property strAPIkey : "XXXXXXXXXXXXXXXXXX" as text


####################################
property numMaxResults : 10 as number
####################################


set strURL to ("https://www.youtube.com/channel/UCQ6Br7m6vP61FZvjv4lwR5w") as text

######

set ocidURLString to refMe's NSString's stringWithString:(strURL)
set ocidURL to refMe's NSURL's alloc()'s initWithString:(ocidURLString)
set strChannelID to ocidURL's lastPathComponent() as text
##################################################
#チャンネルIDからcustomUrl=forUsernameを取得
set ocidUserName to doChannelId2customUrl(strChannelID)
#customUrlから@取ってforUsernameにする場合
#set ocidForUsername to (strUserName's stringByReplacingOccurrencesOfString:("@") withString:(""))

#基本となるURL
set strBaseURL to ("https://www.youtube.com/") as text
#NSURLにして
set ocidBaseURL to refMe's NSURL's alloc()'s initWithString:(strBaseURL)
#コンポーネントに分割
set ocidURLomponents to refMe's NSURLComponents's componentsWithURL:(ocidBaseURL) resolvingAgainstBaseURL:(false)
#カスタムURLを付与
set strSetValue to ("/" & ocidUserName & "/videos") as text
ocidURLomponents's setPath:(strSetValue)
#URLを再定義
set ocidCustomURL to ocidURLomponents's |URL|
set strCustomURL to ocidCustomURL's absoluteString() as text
#
tell application "Finder"
open location strCustomURL
end tell

###############################################################
to doChannelId2customUrl(toChannelId)
  set jsonChannelsURL to "https://www.googleapis.com/youtube/v3/channels?&part=id,snippet,brandingSettings,contentDetails,statistics,topicDetails&id=" & toChannelId & "&key=" & strAPIkey & ""
  ############コマンド整形
  set strCommandText to "/usr/bin/curl -X GET -H 'Content-Type: application/json;charset=UTF-8' \"" & jsonChannelsURL & "\" --connect-timeout 20"
  ####コマンド実行
  set strResponse to (do shell script strCommandText) as text
  ####戻り値を格納
  set ocidResponse to refMe's NSString's stringWithString:(strResponse)
  ###NSDATA化
  set ocidJsonData to (ocidResponse's dataUsingEncoding:(refMe's NSUTF8StringEncoding))
  ##JSONSiriアライズ
  set listJson to (refMe's NSJSONSerialization's JSONObjectWithData:(ocidJsonData) options:0 |error|:(reference))
  set ocidJson to (item 1 of listJson)
  #DICTに格納
  set ocidJsonDict to (refMe's NSDictionary's alloc()'s initWithDictionary:(ocidJson))
  ####チャンネルの名前を取得
  set ocidItemsArray to ocidJsonDict's objectForKey:("items")
  set ocidItemDict to ocidItemsArray's firstObject()
  set ocidItemSnipet to ocidItemDict's objectForKey:("snippet")
  set ocidCustomURL to ocidItemSnipet's valueForKey:("customUrl")
return ocidCustomURL
end doChannelId2customUrl



|

« [Edge]URLを開く | トップページ | [YoutubeAPI] チャンネルIDからチャンネルのアップロードビデオのリストIDを取得する »

YouTube」カテゴリの記事