Last active
August 29, 2015 14:23
-
-
Save sqlchow/a16cc20645666bb052ae to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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