SearchBingImagesDownload.ps1


<#PSScriptInfo
 
.VERSION 1.0.0
 
.GUID 06abc574-c34d-46a6-b30c-6eadc3931416
 
.AUTHOR mikko@lavento.com
 
.COMPANYNAME
 
.COPYRIGHT
 
.TAGS Bing, imagesearch, download image, image, search
 
.LICENSEURI
 
.PROJECTURI
 
.ICONURI
 
.EXTERNALMODULEDEPENDENCIES
 
.REQUIREDSCRIPTS
 
.EXTERNALSCRIPTDEPENDENCIES
 
.RELEASENOTES
 
 
#>
 



<#
 
.DESCRIPTION
Example how to download first image hit from Bing image search. This can be done without Azure APIs because it makes direct request mimicking browser request.
 
#>
 

Param()


#19.7.2019 M.Lavento
#Get first image from Bing based on Searchterm

#Example search "LPS #7" --> https://www.bing.com/images/search?q=LPS%20%237


Add-Type -AssemblyName System.Web

$SearchPlaintext = "LPS #1"
#Convert searchstring to HTTP
$SearchItem = [System.Web.HttpUtility]::UrlEncode($SearchPlaintext)

#Foledr to store pics
$TargetFolder = "C:\Skriptit\WebImageCrawler\Downloadedpics"
if ( (Test-Path -Path $TargetFolder) -eq $false) { md $TargetFolder }
Invoke-Item $TargetFolder

#Bing is "simpler"
$url = "https://www.bing.com/images/search?q=$SearchItem"

$browserAgent = 'Mozilla/5.0 (Windows NT 6.3; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/33.0.1750.146 Safari/537.36'
$page = Invoke-WebRequest -Uri $url -UserAgent $browserAgent -UseBasicParsing

#First picture resides in the link number 69 (Microsoft might change that over the time)
$firsthit = $page.Links | select -Skip 68 -First 1

#pick rawurl. The link we are looking begins with mediaurl=
$rawurl = $firsthit | ForEach-Object { ($_.href -split 'mediaurl=')[-1].Split('&')[0]}

#convert to http, basically makes this: $fineurl = $rawurl -replace "%2f","/" -replace "%3a",":"
$fineurl = [System.Web.HttpUtility]::UrlDecode($rawurl)

#Check if there is too much on the end of URL (some of the results have tails)
#For example http://vignette2.wikia.nocookie.net/littlestpetshoplps/images/b/b2/Littlest_Pet_Shop_-7.jpg/revision/latest?cb=20120124005013
$finalurl = ($fineurl -split '.jpg')[0]+".jpg"


#Save file
$file = Split-Path -Path $finalurl -Leaf
$finalfile = $SearchPlaintext + "_" + $file
#replace illegal chars if there is any
[System.IO.Path]::GetInvalidFileNameChars() | foreach {$finalfile = $finalfile.replace($_,' ')}
$path = Join-Path -Path $TargetFolder -ChildPath $finalfile
#fetch the image
Invoke-WebRequest -Uri $finalurl -OutFile $path