Skip to content

Commit

Permalink
Adding YouTube channel and subscription functions
Browse files Browse the repository at this point in the history
  • Loading branch information
pcgeek86 committed Jan 25, 2022
1 parent 1e0af35 commit dd2dd75
Show file tree
Hide file tree
Showing 15 changed files with 207 additions and 14 deletions.
7 changes: 7 additions & 0 deletions LICENSE
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
Copyright 2022 Trevor Sullivan

Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
19 changes: 17 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,12 @@ The purpose of this PowerShell module is to enable you to manage YouTube data fr

### 🚀 Features

* Search for YouTube videos with queries
* Search for YouTube videos or channels
* Get details for YouTube videos (ie. duration, like count, comment count)
* Retrieve top-level comments from YouTube videos
* Delete a comment, from a YouTube video you own
* [Post a comment](#-🗨️-post-comment-on-youtube-video) on a video
* Subscribe or unsubscribe from a YouTube channel

### 📦 Installation

Expand Down Expand Up @@ -128,4 +130,17 @@ UgyURVdTOxDbbNi6JfJ4AaABAg 2/6/2020 5:20:42 AM 2/6/2020 5:20:42 AM 0
UgwMHBsGm_sL3cMMzA14AaABAg 2/3/2020 8:35:19 PM 2/3/2020 8:35:19 PM 0 1 Semtx552 Always nice to see handy things like how someone else uses VSCode…
UgwTUdL6EeyOSc-6-8p4AaABAg 2/2/2020 11:46:46 PM 2/2/2020 11:46:46 PM 1 2 Guido Oliveira This camera quality is amazing Trevor! awesome explanation as well!
UgxWz7fai70eXgLKLwV4AaABAg 2/2/2020 10:05:14 PM 2/2/2020 10:05:14 PM 0 1 Josh King That pop filter is majestic!
```
```

#### 🗨️ Remove a YouTube Comment

```
Remove-YouTubeComment -Id <commentId>
```

#### 🗨️ Post Comment on YouTube Video

```
New-YouTubeComment -ChannelId <channelId> -VideoId <videoId> -Text 'This is a great video! 📺'
```

69 changes: 69 additions & 0 deletions formats/YouTube.Channel.ps1xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
<Configuration>
<ViewDefinitions>
<View>
<Name>Table</Name>
<ViewSelectedBy>
<TypeName>YouTube.Channel</TypeName>
</ViewSelectedBy>
<TableControl>
<AutoSize></AutoSize>
<TableHeaders>
<TableColumnHeader>
<Label>ChannelId</Label>
</TableColumnHeader>
<TableColumnHeader>
<Label>Channel Title</Label>
</TableColumnHeader>
<TableColumnHeader>
<Label>URL</Label>
</TableColumnHeader>
<TableColumnHeader>
<Label>Published At</Label>
</TableColumnHeader>
<TableColumnHeader>
<Label>Kids</Label>
</TableColumnHeader>
<TableColumnHeader>
<Label>Views</Label>
</TableColumnHeader>
<TableColumnHeader>
<Label>Subscribers</Label>
</TableColumnHeader>
<TableColumnHeader>
<Label>Videos</Label>
</TableColumnHeader>
</TableHeaders>
<TableRowEntries>
<TableRowEntry>
<TableColumnItems>
<TableColumnItem>
<ScriptBlock>$_.id</ScriptBlock>
</TableColumnItem>
<TableColumnItem>
<ScriptBlock>$_.snippet.title</ScriptBlock>
</TableColumnItem>
<TableColumnItem>
<ScriptBlock>$_.snippet.customUrl</ScriptBlock>
</TableColumnItem>
<TableColumnItem>
<ScriptBlock>$_.snippet.publishedAt</ScriptBlock>
</TableColumnItem>
<TableColumnItem>
<ScriptBlock>$_.status.madeForKids ? 'Yes' : 'No'</ScriptBlock>
</TableColumnItem>
<TableColumnItem>
<ScriptBlock>$_.statistics.viewCount</ScriptBlock>
</TableColumnItem>
<TableColumnItem>
<ScriptBlock>$_.statistics.subscriberCount</ScriptBlock>
</TableColumnItem>
<TableColumnItem>
<ScriptBlock>$_.statistics.videoCount</ScriptBlock>
</TableColumnItem>
</TableColumnItems>
</TableRowEntry>
</TableRowEntries>
</TableControl>
</View>
</ViewDefinitions>
</Configuration>
29 changes: 29 additions & 0 deletions functions/Add-YouTubeSubscription.ps1
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
function Add-YouTubeSubscription {
<#
.SYNOPSIS
Subscribe to a YouTube channel, with the specified ChannelId.
.PARAMETER ChannelId
The YouTube channel ID that you want to subscribe to.
#>
[CmdletBinding()]
param (
[Parameter(Mandatory = $true)]
[string] $ChannelId
)
$Uri = 'https://www.googleapis.com/youtube/v3/subscriptions?part=contentDetails,id,snippet,subscriberSnippet'
$Headers = (Get-AccessToken) + @{
'Content-Type' = 'application/json'
}
$Body = @{
snippet = @{
resourceId = @{
channelId = $ChannelId
}
}
} | ConvertTo-Json -Depth 5
Write-Verbose -Message $Uri
Write-Verbose -Message $Body
Write-Verbose -Message ($Headers | ConvertTo-Json)
Invoke-RestMethod -Uri $Uri -Method Post -Headers $Headers -Body $Body
}
27 changes: 27 additions & 0 deletions functions/Get-YouTubeChannel.ps1
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
function Get-YouTubeChannel {
<#
.SYNOPSIS
Retrieves details for a YouTube channel.
.PARAMETER ChannelId
The array of channels that you want to get detailed information for.
#>
[CmdletBinding()]
param (
[Parameter(ParameterSetName = 'ChannelId')]
[string[]] $Id
)
$Uri = 'https://www.googleapis.com/youtube/v3/channels?part=brandingSettings,contentDetails,contentOwnerDetails,id,snippet,localizations,statistics,status,topicDetails'

switch ($PSCmdlet.ParameterSetName) {
'ChannelId' {
$Uri += '&id={0}' -f ($Id -join ',')
}
}
Write-Verbose -Message $Uri
$Result = Invoke-RestMethod -Uri $Uri -Method Get -Headers (Get-AccessToken)
$Result.items | ForEach-Object -Process { $PSItem.PSTypeNames.Add('YouTube.Channel') }

if ($Raw) { return $Result }
$Result.items
}
4 changes: 1 addition & 3 deletions functions/Get-YouTubeComment.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,5 @@ function Get-YouTubeComment {
param (

)



throw 'This command is not implemented'
}
19 changes: 16 additions & 3 deletions functions/Get-YouTubeCommentThread.ps1
Original file line number Diff line number Diff line change
@@ -1,15 +1,28 @@
function Get-YouTubeCommentThread {
<#
.SYNOPSIS
Retrieves top-level comments from a video, or comments related to a channel.
#>
[CmdletBinding()]
param (
[Parameter(Mandatory = $true, ParameterSetName = 'VideoId')]
[string] $VideoId,
[Parameter(Mandatory = $true, ParameterSetName = 'ChannelRelated')]
[string] $RelatedToChannelId,
[switch] $Raw
)

$Uri = 'https://www.googleapis.com/youtube/v3/commentThreads?part=id,replies,snippet'
if ($PSCmdlet.ParameterSetName -eq 'VideoId') {
$Uri += '&videoId={0}' -f $VideoId
}
switch ($PSCmdlet.ParameterSetName) {
'VideoId' {
$Uri += '&videoId={0}' -f $VideoId
break
}
'ChannelRelated' {
$Uri += '&allThreadsRelatedToChannelId={0}' -f $RelatedToChannelId
break
}
}

$Result = Invoke-RestMethod -Uri $Uri -Headers (Get-AccessToken)

Expand Down
5 changes: 4 additions & 1 deletion functions/New-YouTubeComment.ps1
Original file line number Diff line number Diff line change
@@ -1,7 +1,10 @@
function New-YouTubeComment {
<#
.SYNOPSIS
Creates a new top-level comment on a YouTube video. (BROKEN: Logged ticket with YouTube: https://issuetracker.google.com/issues/215967256)
Creates a new top-level comment on a YouTube video.
.EXAMPLE
New-YouTubeComment -ChannelId UCGpJOTbsdZH_sBxihz6p_wg -VideoId LFWxH-bexNk -Text 'Hello from PowerShell! 👨🏻‍💻'
#>
[CmdletBinding()]
param (
Expand Down
2 changes: 1 addition & 1 deletion tests/test-activity-list.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -4,4 +4,4 @@ $Activities = Get-YouTubeActivity -Mine

$Activities

Get-YouTubeActivity -ChannelId
Get-YouTubeActivity -ChannelId UCGpJOTbsdZH_sBxihz6p_wg
4 changes: 4 additions & 0 deletions tests/test-comment-threads.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,8 @@ Import-Module -Name $PSScriptRoot/../ -Force

$CommentList = Get-YouTubeCommentThread -VideoId LFWxH-bexNk

$CommentList

$CommentList = Get-YouTubeCommentThread -RelatedToChannelId UCGpJOTbsdZH_sBxihz6p_wg

$CommentList
8 changes: 8 additions & 0 deletions tests/test-get-channel.ps1
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
Import-Module -Name $PSScriptRoot/.. -Force
return
$ChannelList = Find-YouTubeChannel -Query bacon

$Details = Get-YouTubeChannel -Id $ChannelList.id.channelId -Verbose

$Details

5 changes: 5 additions & 0 deletions tests/test-subscription-add.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
2 changes: 1 addition & 1 deletion tests/test-youtube-oauth.ps1
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
# Clean up old Chromium processes
gps chrome | ? { $PSItem.MainModule.Filename -match 'chromium' } | spps
Get-Process chrome | Where-Object -FilterScript { $PSItem.MainModule.Filename -match 'chromium' } | Stop-Process

Import-Module -Name $PSScriptRoot/../ -Force

Expand Down
13 changes: 11 additions & 2 deletions 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.2'
ModuleVersion = '0.3'
GUID = '4f1448cd-300f-444c-afdf-8ed678504ffd'
Copyright = '2022 Trevor Sullivan'
Description = 'Manage YouTube from the command line with PowerShell.'
Expand All @@ -19,9 +19,11 @@
'Get-YouTubeComment'
'New-YouTubeComment'
'Remove-YouTubeComment'
'Add-YouTubeSubscription'
'Get-YouTubeSubscription'
'Remove-YouTubeSubscription'
'Get-YouTubeActivity'
'Get-YouTubeChannel'
)
AliasesToExport = @('')
VariablesToExport = @('')
Expand All @@ -32,7 +34,14 @@
ProjectUri = ''
IconUri = ''
ReleaseNotes = @'
0.1 - Initial realease
0.2
- Added New-YouTubeComment to create a top-level comment thread
0.1
- Initial realease
'@
}
}
Expand Down
8 changes: 7 additions & 1 deletion youtube.psm1
Original file line number Diff line number Diff line change
@@ -1,6 +1,12 @@
try {
Import-Module -Name Pode -ErrorAction Stop
}
catch {
throw 'The YouTube module for PowerShell requires the Pode web server for completing the oAuth process.'
}

$script:RedirectUri = 'http://localhost:8000/auth/complete'
$script:Scopes = 'https://www.googleapis.com/auth/youtube https://www.googleapis.com/auth/youtube.readonly'
$script:Scopes = 'https://www.googleapis.com/auth/youtube https://www.googleapis.com/auth/youtube.readonly https://www.googleapis.com/auth/youtubepartner-channel-audit'
$script:ConfigPath = "$HOME/.pwsh.youtube.config.json"

$FileList = Get-ChildItem -Path $PSScriptRoot/functions/*ps1
Expand Down

0 comments on commit dd2dd75

Please sign in to comment.