Skip to content

Commit

Permalink
📽️ Add YouTube Activity and Subscription support 📺
Browse files Browse the repository at this point in the history
  • Loading branch information
pcgeek86 committed Jan 24, 2022
1 parent d2e620f commit 4c0cf1a
Show file tree
Hide file tree
Showing 12 changed files with 271 additions and 1 deletion.
51 changes: 51 additions & 0 deletions formats/YouTube.Activity.ps1xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
<Configuration>
<ViewDefinitions>
<View>
<Name>Table</Name>
<ViewSelectedBy>
<TypeName>YouTube.Activity</TypeName>
</ViewSelectedBy>
<TableControl>
<AutoSize></AutoSize>
<TableHeaders>
<TableColumnHeader>
<Label>Title</Label>
</TableColumnHeader>
<TableColumnHeader>
<Label>Type</Label>
</TableColumnHeader>
<TableColumnHeader>
<Label>Published At</Label>
</TableColumnHeader>
<TableColumnHeader>
<Label>SubscriptionId</Label>
</TableColumnHeader>
<TableColumnHeader>
<Label>ChannelId</Label>
</TableColumnHeader>
</TableHeaders>
<TableRowEntries>
<TableRowEntry>
<TableColumnItems>
<TableColumnItem>
<ScriptBlock>$_.snippet.title</ScriptBlock>
</TableColumnItem>
<TableColumnItem>
<ScriptBlock>$_.snippet.type</ScriptBlock>
</TableColumnItem>
<TableColumnItem>
<ScriptBlock>$_.snippet.publishedAt</ScriptBlock>
</TableColumnItem>
<TableColumnItem>
<ScriptBlock>$_.id</ScriptBlock>
</TableColumnItem>
<TableColumnItem>
<ScriptBlock>$_.snippet.channelId</ScriptBlock>
</TableColumnItem>
</TableColumnItems>
</TableRowEntry>
</TableRowEntries>
</TableControl>
</View>
</ViewDefinitions>
</Configuration>
39 changes: 39 additions & 0 deletions formats/YouTube.Search.ChannelResult.ps1xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
<Configuration>
<ViewDefinitions>
<View>
<Name>Table</Name>
<ViewSelectedBy>
<TypeName>YouTube.Search.ChannelResult</TypeName>
</ViewSelectedBy>
<TableControl>
<AutoSize></AutoSize>
<TableHeaders>
<TableColumnHeader>
<Label>Channel Title</Label>
</TableColumnHeader>
<TableColumnHeader>
<Label>Published Date</Label>
</TableColumnHeader>
<TableColumnHeader>
<Label>ChannelId</Label>
</TableColumnHeader>
</TableHeaders>
<TableRowEntries>
<TableRowEntry>
<TableColumnItems>
<TableColumnItem>
<ScriptBlock>$_.snippet.channelTitle</ScriptBlock>
</TableColumnItem>
<TableColumnItem>
<ScriptBlock>$_.snippet.publishedAt</ScriptBlock>
</TableColumnItem>
<TableColumnItem>
<ScriptBlock>$_.id.channelId</ScriptBlock>
</TableColumnItem>
</TableColumnItems>
</TableRowEntry>
</TableRowEntries>
</TableControl>
</View>
</ViewDefinitions>
</Configuration>
51 changes: 51 additions & 0 deletions formats/YouTube.Subscription.ps1xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
<Configuration>
<ViewDefinitions>
<View>
<Name>Table</Name>
<ViewSelectedBy>
<TypeName>YouTube.Subscription</TypeName>
</ViewSelectedBy>
<TableControl>
<AutoSize></AutoSize>
<TableHeaders>
<TableColumnHeader>
<Label>Title</Label>
</TableColumnHeader>
<TableColumnHeader>
<Label>Items</Label>
</TableColumnHeader>
<TableColumnHeader>
<Label>Published At</Label>
</TableColumnHeader>
<TableColumnHeader>
<Label>SubscriptionId</Label>
</TableColumnHeader>
<TableColumnHeader>
<Label>ChannelId</Label>
</TableColumnHeader>
</TableHeaders>
<TableRowEntries>
<TableRowEntry>
<TableColumnItems>
<TableColumnItem>
<ScriptBlock>$_.snippet.title</ScriptBlock>
</TableColumnItem>
<TableColumnItem>
<ScriptBlock>$_.contentDetails.totalItemCount</ScriptBlock>
</TableColumnItem>
<TableColumnItem>
<ScriptBlock>$_.snippet.publishedAt</ScriptBlock>
</TableColumnItem>
<TableColumnItem>
<ScriptBlock>$_.id</ScriptBlock>
</TableColumnItem>
<TableColumnItem>
<ScriptBlock>$_.snippet.channelId</ScriptBlock>
</TableColumnItem>
</TableColumnItems>
</TableRowEntry>
</TableRowEntries>
</TableControl>
</View>
</ViewDefinitions>
</Configuration>
24 changes: 24 additions & 0 deletions functions/Find-YouTubeChannel.ps1
Original file line number Diff line number Diff line change
@@ -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
}
35 changes: 35 additions & 0 deletions functions/Get-YouTubeActivity.ps1
Original file line number Diff line number Diff line change
@@ -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
}
20 changes: 20 additions & 0 deletions functions/Get-YouTubeSubscription.ps1
Original file line number Diff line number Diff line change
@@ -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
}
9 changes: 9 additions & 0 deletions functions/Remove-YouTubeSubscription.ps1
Original file line number Diff line number Diff line change
@@ -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)
}
7 changes: 7 additions & 0 deletions tests/test-activity-list.ps1
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
Import-Module -Name $PSScriptRoot/../ -Force

$Activities = Get-YouTubeActivity -Mine

$Activities

Get-YouTubeActivity -ChannelId
7 changes: 7 additions & 0 deletions tests/test-search-channels.ps1
Original file line number Diff line number Diff line change
@@ -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
5 changes: 5 additions & 0 deletions tests/test-subscription-delete.ps1
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
Import-Module -Name $PSScriptRoot/../ -Force

$Subscriptions = Get-YouTubeSubscription

Remove-YouTubeSubscription -Id RsdxlJShC6cmvtxrXi97D2DP2WSbPO0-RJocte_4xbE
18 changes: 18 additions & 0 deletions tests/test-subscription-list.ps1
Original file line number Diff line number Diff line change
@@ -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
6 changes: 5 additions & 1 deletion youtube.psd1
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
RootModule = 'youtube.psm1'
Author = 'Trevor Sullivan <[email protected]>'
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.'
Expand All @@ -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 = @('')
Expand Down

0 comments on commit 4c0cf1a

Please sign in to comment.