« [YoutubeAPI] チャンネルIDからカスタムURL=ユーザー名(アカウント名)を取得 | トップページ | [XML]CDATA(HTML化済みのテキスト)を追加する »

[YoutubeAPI] チャンネルIDからチャンネルのアップロードビデオのリストIDを取得する


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

#!/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 ID
property strAPIkey : "XXXXXXXXXXXXXXXXXXXXXXXXXXXXX" 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からアップロードビデオのリストID
set ocidUploadsListID to doChannelId2UploadsListID(strChannelID)
log ocidUploadsListID as text
#
#基本となるURL
set strPlistURL to ("https://www.youtube.com/playlist") as text
#NSURLにして
set ocidPlistURL to refMe's NSURL's alloc()'s initWithString:(strPlistURL)
#コンポーネントに分割
set ocidURLomponents to refMe's NSURLComponents's componentsWithURL:(ocidPlistURL) resolvingAgainstBaseURL:(false)
#クエリーアイテム
set ocidQueryItems to refMe's NSMutableArray's alloc()'s initWithCapacity:(0)
#クエリー定義
set ocidQueryItem to refMe's NSURLQueryItem's alloc()'s initWithName:("list") value:(ocidUploadsListID)
#クエリーをセット
ocidQueryItems's addObject:(ocidQueryItem)
#コンポーネントに追加
ocidURLomponents's setQueryItems:(ocidQueryItems)
#URLを再定義
set ocidPlaylistURL to ocidURLomponents's |URL|


set strPlaylistURL to ocidPlaylistURL's absoluteString() as text
#
tell application "Finder"
open location strPlaylistURL
end tell



###############################################################
to doChannelId2UploadsListID(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 ocidItemContentDetails to ocidItemDict's objectForKey:("contentDetails")
  set ocidrelatedPlaylists to ocidItemContentDetails's objectForKey:("relatedPlaylists")
  set ocidUploadsListID to ocidrelatedPlaylists's valueForKey:("uploads")
return ocidUploadsListID
end doChannelId2UploadsListID

|

« [YoutubeAPI] チャンネルIDからカスタムURL=ユーザー名(アカウント名)を取得 | トップページ | [XML]CDATA(HTML化済みのテキスト)を追加する »

YouTube」カテゴリの記事