Created
March 4, 2016 14:40
-
-
Save dfinke/4ab9927eab082a8ef4b1 to your computer and use it in GitHub Desktop.
PowerShell script for searching and downloading images from the Bing API
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-BingPics { | |
param( | |
[Parameter(ValueFromPipeline=$true)] | |
$q | |
) | |
Process { | |
$Key=$env:BingPicKey | |
if(!$env:BingPicKey) { | |
Throw '$env:BingPicKey needs to be set' | |
} | |
$Base64KeyBytes = [Text.Encoding]::ASCII.GetBytes("ignored:$Key") | |
$Base64Key = [Convert]::ToBase64String($Base64KeyBytes) | |
$url = "https://api.datamarket.azure.com/Bing/Search/Image?`$format=json&Query='$q'" | |
$r=Invoke-RestMethod $url -Headers @{ 'Authorization' = "Basic $Base64Key" } | |
$links = $r.d.results| % { | |
'<a href="{1}"><img src="{0}" alt="{2}", title="{2}" /></a>' -f ($_.Thumbnail.MediaUrl),($_.MediaUrl),($_.Title) | |
} | |
@" | |
<html> | |
<body> | |
<h1>"{0}" pictures</h1> | |
{1} | |
</body> | |
</html> | |
"@ -f $q, ($links -join ' ') | Set-Content -Encoding Ascii "c:\temp\pics.html" | |
Invoke-Item "c:\temp\pics.html" | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment