Skip to content

Instantly share code, notes, and snippets.

@dfinke
Created March 4, 2016 14:40
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 dfinke/4ab9927eab082a8ef4b1 to your computer and use it in GitHub Desktop.
Save dfinke/4ab9927eab082a8ef4b1 to your computer and use it in GitHub Desktop.
PowerShell script for searching and downloading images from the Bing API
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