From 4c0cf1a8d03a4e288eba72ba42aba5546ef90282 Mon Sep 17 00:00:00 2001 From: Trevor Sullivan Date: Mon, 24 Jan 2022 11:08:07 -0700 Subject: [PATCH] =?UTF-8?q?=F0=9F=93=BD=EF=B8=8F=20Add=20YouTube=20Activit?= =?UTF-8?q?y=20and=20Subscription=20support=20=F0=9F=93=BA?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- formats/YouTube.Activity.ps1xml | 51 +++++++++++++++++++++ formats/YouTube.Search.ChannelResult.ps1xml | 39 ++++++++++++++++ formats/YouTube.Subscription.ps1xml | 51 +++++++++++++++++++++ functions/Find-YouTubeChannel.ps1 | 24 ++++++++++ functions/Get-YouTubeActivity.ps1 | 35 ++++++++++++++ functions/Get-YouTubeSubscription.ps1 | 20 ++++++++ functions/Remove-YouTubeSubscription.ps1 | 9 ++++ tests/test-activity-list.ps1 | 7 +++ tests/test-search-channels.ps1 | 7 +++ tests/test-subscription-delete.ps1 | 5 ++ tests/test-subscription-list.ps1 | 18 ++++++++ youtube.psd1 | 6 ++- 12 files changed, 271 insertions(+), 1 deletion(-) create mode 100644 formats/YouTube.Activity.ps1xml create mode 100644 formats/YouTube.Search.ChannelResult.ps1xml create mode 100644 formats/YouTube.Subscription.ps1xml create mode 100644 functions/Find-YouTubeChannel.ps1 create mode 100644 functions/Get-YouTubeActivity.ps1 create mode 100644 functions/Get-YouTubeSubscription.ps1 create mode 100644 functions/Remove-YouTubeSubscription.ps1 create mode 100644 tests/test-activity-list.ps1 create mode 100644 tests/test-search-channels.ps1 create mode 100644 tests/test-subscription-delete.ps1 create mode 100644 tests/test-subscription-list.ps1 diff --git a/formats/YouTube.Activity.ps1xml b/formats/YouTube.Activity.ps1xml new file mode 100644 index 0000000..d3367ea --- /dev/null +++ b/formats/YouTube.Activity.ps1xml @@ -0,0 +1,51 @@ + + + + Table + + YouTube.Activity + + + + + + + + + + + + + + + + + + + + + + + + + $_.snippet.title + + + $_.snippet.type + + + $_.snippet.publishedAt + + + $_.id + + + $_.snippet.channelId + + + + + + + + diff --git a/formats/YouTube.Search.ChannelResult.ps1xml b/formats/YouTube.Search.ChannelResult.ps1xml new file mode 100644 index 0000000..93832e5 --- /dev/null +++ b/formats/YouTube.Search.ChannelResult.ps1xml @@ -0,0 +1,39 @@ + + + + Table + + YouTube.Search.ChannelResult + + + + + + + + + + + + + + + + + + + $_.snippet.channelTitle + + + $_.snippet.publishedAt + + + $_.id.channelId + + + + + + + + diff --git a/formats/YouTube.Subscription.ps1xml b/formats/YouTube.Subscription.ps1xml new file mode 100644 index 0000000..c0f4b63 --- /dev/null +++ b/formats/YouTube.Subscription.ps1xml @@ -0,0 +1,51 @@ + + + + Table + + YouTube.Subscription + + + + + + + + + + + + + + + + + + + + + + + + + $_.snippet.title + + + $_.contentDetails.totalItemCount + + + $_.snippet.publishedAt + + + $_.id + + + $_.snippet.channelId + + + + + + + + diff --git a/functions/Find-YouTubeChannel.ps1 b/functions/Find-YouTubeChannel.ps1 new file mode 100644 index 0000000..a94e8d3 --- /dev/null +++ b/functions/Find-YouTubeChannel.ps1 @@ -0,0 +1,24 @@ +function Find-YouTubeChannel { + <# + .SYNOPSIS + Search for YouTube channels based on a query term. + #> + [CmdletBinding()] + param ( + [Parameter(Mandatory = $true)] + [string] $Query, + [string] $PageToken, + [switch] $Raw + ) + $Uri = 'https://www.googleapis.com/youtube/v3/search?part=snippet&type=channel&maxResults=50&q={0}' -f $Query + if ($PageToken) { + $Uri += '&pageToken={0}' -f $PageToken + } + + $Result = Invoke-RestMethod -Uri $Uri -Headers (Get-AccessToken) + + if ($PSBoundParameters.ContainsKey('Raw')) { return $Result } + + $Result.Items | ForEach-Object -Process { $PSItem.PSTypeNames.Add('YouTube.Search.ChannelResult') } + $Result.Items +} \ No newline at end of file diff --git a/functions/Get-YouTubeActivity.ps1 b/functions/Get-YouTubeActivity.ps1 new file mode 100644 index 0000000..0a1fd0d --- /dev/null +++ b/functions/Get-YouTubeActivity.ps1 @@ -0,0 +1,35 @@ +function Get-YouTubeActivity { + <# + .SYNOPSIS + Obtain activities performed by yourself or a specific channel ID. + #> + [CmdletBinding()] + param ( + [Parameter(ParameterSetName = 'ChannelId')] + [string] $ChannelId, + [Parameter(ParameterSetName = 'Mine')] + [switch] $Mine, + [Parameter(ParameterSetName = 'ChannelId')] + [Parameter(ParameterSetName = 'Mine')] + [switch] $Raw + ) + + $Uri = 'https://www.googleapis.com/youtube/v3/activities?part=id,snippet,contentDetails&maxResults=50' + + switch ($PSCmdlet.ParameterSetName) { + 'Mine' { + $Uri += '&mine=true' + break + } + 'ChannelId' { + $Uri += '&channelId={0}' -f $ChannelId + } + } + $Result = Invoke-RestMethod -Uri $Uri -Headers (Get-AccessToken) + + $Result.items | ForEach-Object -Process { $PSItem.PSTypeNames.Add('YouTube.Activity') } + + if ($PSBoundParameters.ContainsKey('Raw')) { $Result } + + $Result.items +} \ No newline at end of file diff --git a/functions/Get-YouTubeSubscription.ps1 b/functions/Get-YouTubeSubscription.ps1 new file mode 100644 index 0000000..22333be --- /dev/null +++ b/functions/Get-YouTubeSubscription.ps1 @@ -0,0 +1,20 @@ +function Get-YouTubeSubscription { + [CmdletBinding()] + param ( + [string] $NextPageToken, + [switch] $Raw + ) + + $Uri = 'https://www.googleapis.com/youtube/v3/subscriptions?part=id,contentDetails,snippet,subscriberSnippet&mine=true&maxResults=50' + if ($PSBoundParameters.ContainsKey('NextPageToken')) { + $Uri += '&pageToken={0}' -f $NextPageToken + Write-Verbose -Message 'Added next page token' + } + $Result = Invoke-RestMethod -Uri $Uri -Headers (Get-AccessToken) + + $Result.items | ForEach-Object -Process { $PSItem.PSTypeNames.Add('YouTube.Subscription') } + + if ($PSBoundParameters.ContainsKey('Raw')) { return $Result } + + $Result.items +} \ No newline at end of file diff --git a/functions/Remove-YouTubeSubscription.ps1 b/functions/Remove-YouTubeSubscription.ps1 new file mode 100644 index 0000000..705294b --- /dev/null +++ b/functions/Remove-YouTubeSubscription.ps1 @@ -0,0 +1,9 @@ +function Remove-YouTubeSubscription { + [CmdletBinding()] + param ( + [Parameter(Mandatory = $true)] + [string] $Id + ) + $Uri = 'https://www.googleapis.com/youtube/v3/subscriptions?id={0}' -f $Id + Invoke-RestMethod -Uri $Uri -Method Delete -Headers (Get-AccessToken) +} \ No newline at end of file diff --git a/tests/test-activity-list.ps1 b/tests/test-activity-list.ps1 new file mode 100644 index 0000000..a04e9c0 --- /dev/null +++ b/tests/test-activity-list.ps1 @@ -0,0 +1,7 @@ +Import-Module -Name $PSScriptRoot/../ -Force + +$Activities = Get-YouTubeActivity -Mine + +$Activities + +Get-YouTubeActivity -ChannelId \ No newline at end of file diff --git a/tests/test-search-channels.ps1 b/tests/test-search-channels.ps1 new file mode 100644 index 0000000..85b574c --- /dev/null +++ b/tests/test-search-channels.ps1 @@ -0,0 +1,7 @@ +Import-Module -Name $PSScriptRoot/../ -Force + +$Result = Find-YouTubeChannel -Query 'trevor sullivan' + +#$Result | ConvertTo-Json -Depth 10 | Set-Content -Path ("$HOME/{0}.json" -f (New-Guid).Guid) + +$Result.snippet \ No newline at end of file diff --git a/tests/test-subscription-delete.ps1 b/tests/test-subscription-delete.ps1 new file mode 100644 index 0000000..02d3459 --- /dev/null +++ b/tests/test-subscription-delete.ps1 @@ -0,0 +1,5 @@ +Import-Module -Name $PSScriptRoot/../ -Force + +$Subscriptions = Get-YouTubeSubscription + +Remove-YouTubeSubscription -Id RsdxlJShC6cmvtxrXi97D2DP2WSbPO0-RJocte_4xbE \ No newline at end of file diff --git a/tests/test-subscription-list.ps1 b/tests/test-subscription-list.ps1 new file mode 100644 index 0000000..b9142fb --- /dev/null +++ b/tests/test-subscription-list.ps1 @@ -0,0 +1,18 @@ +Import-Module -Name $PSScriptRoot/../ -Force + +$Subscriptions = Get-YouTubeSubscription + +$Subscriptions.items[0].snippet.title +$Subscriptions.items[0].contentDetails + +$AllResults = @() +$RawResult = Get-YouTubeSubscription -Raw +$AllResults += $RawResult +$RawResult = Get-YouTubeSubscription -Raw -NextPageToken $RawResult.nextPageToken +$AllResults += $RawResult +$RawResult = Get-YouTubeSubscription -Raw -NextPageToken $RawResult.nextPageToken +$AllResults += $RawResult +$RawResult = Get-YouTubeSubscription -Raw -NextPageToken $RawResult.nextPageToken +$AllResults += $RawResult + +$AllResults.items \ No newline at end of file diff --git a/youtube.psd1 b/youtube.psd1 index 311ee02..4a30c3e 100644 --- a/youtube.psd1 +++ b/youtube.psd1 @@ -2,7 +2,7 @@ RootModule = 'youtube.psm1' Author = 'Trevor Sullivan ' CompanyName = 'Trevor Sullivan' - ModuleVersion = '0.1' + ModuleVersion = '0.2' GUID = '4f1448cd-300f-444c-afdf-8ed678504ffd' Copyright = '2022 Trevor Sullivan' Description = 'Manage YouTube from the command line with PowerShell.' @@ -12,12 +12,16 @@ 'Grant-YouTube' 'Grant-YouTubeOauth' 'Find-YouTubeVideo' + 'Find-YouTubeChannel' 'Get-YouTubeVideo' 'Set-YouTubeConfiguration' 'Get-YouTubeCommentThread' 'Get-YouTubeComment' 'New-YouTubeComment' 'Remove-YouTubeComment' + 'Get-YouTubeSubscription' + 'Remove-YouTubeSubscription' + 'Get-YouTubeActivity' ) AliasesToExport = @('') VariablesToExport = @('')