Skip to content

Instantly share code, notes, and snippets.

@sqlchow
Last active August 29, 2015 14:23
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save sqlchow/a16cc20645666bb052ae to your computer and use it in GitHub Desktop.
Save sqlchow/a16cc20645666bb052ae to your computer and use it in GitHub Desktop.
function Get-YoutubeSearchResults{
param(
[string]
$Query
)
begin{
$ApiKey = 'addyourAPIkey here'
$requestUri = "https://www.googleapis.com/youtube/v3/search?part=id%2C+snippet&q=$Query&key=$ApiKey"
$watchUrl = "https://www.youtube.com/watch?v="
}
process{
try{
$searchResults = Invoke-RestMethod -Uri $requestUri
#Get-only videos Where-Object {$_.id.kind -eq "youtube#video"}
$searchResults.items |
Select-Object @{Name = "VideoType" ; Expression = {$_.id.kind.Replace('youtube#', '')}},
@{Name = "PublishedOn" ; Expression = {$_.snippet.publishedAt}},
@{Name = "Title" ; Expression = {$_.snippet.title}},
@{Name = "VideoUrl" ; Expression = { if($_.id.kind -eq "youtube#video"){
$watchUrl + $_.id.videoId
}elseif($_.id.kind -eq "youtube#channel"){
$watchUrl + $_.id.channelId
}else{
$watchUrl + $_.id.playlistId
}
}}
}
catch{
throw $Error[0].Exception.Message
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment