User Tools

Site Tools


misc:powershell

Powershell scripts

Amazon artwork grabber

If you like storing all your album art in a single folder here is a quick and dirty Windows Powershell script that automatically grabs artwork from Amazon for all tracks in your foobar2000 Media Library (using foo_comserver2). It works reasonably well but could still be improved.

You'll need to modify the script a bit for your own needs. You'll need to set $AWSAccessKeyId (you either find one from e.g. some other software or failing that register at Amazon yourself).

I would have made it a proper Powershell script but I was put off by the restrictions in place by default on executing those so you'll just have to copy and paste it to the prompt and press enter a couple times if needed.

$script = "%album artist% - %album%"
$store = "X:\ArtworkStore"
$AWSAccessKeyId =

[System.Reflection.Assembly]::LoadWithPartialName("System.Web") | out-null

$fb2k = new-object -comObject Foobar2000.Application.0.7
$web = (new-object System.Net.WebClient)
$ml = $fb2k.MediaLibrary

#comserver needs updating!
#$tracks = $ml.GetSortedTracks($script,"NOT %album artist% MISSING AND NOT %album% MISSING")
$tracks = $ml.GetSortedTracks($script,"NOT (ARTIST MISSING AND ALBUM ARTIST MISSING) AND NOT ALBUM MISSING")

$fmt = @()
$albums = @()

for ($i=0; $i -lt $tracks.Count; $i++) {$fmt += $tracks.Item($i).FormatTitle($script)}
for ($i=1; $i -lt $fmt.Count; $i++) {if ($fmt[$i-1] -ne $fmt[$i]) {$albums += $fmt[$i-1]}}
if ($fmt.Count) {$albums += $fmt[$fmt.Count-1]}

foreach ($album in $albums)
{
$albumsafe = $album
$albumsafe = $albumsafe.Replace("?","")
$albumsafe = $albumsafe.Replace(":","")
$albumsafe = $albumsafe.Replace("/","")
$albumsafe = $albumsafe.Replace("\","")
$albumsafe = $albumsafe.Replace("*"," ")
$albumsafe = $albumsafe.Replace("`"","")
$path = $store + $albumsafe + ".jpg"

if (!(Test-Path($path)))
{
$xml = [xml]$web.DownloadString("http://ecs.amazonaws.com/onca/xml?Service=AWSECommerceService&AWSAccessKeyId="+$AWSAccessKeyId+"&Operation=ItemSearch&SearchIndex=Music&ItemPage=1&ResponseGroup=ItemAttributes,Images&Keywords="+[System.Web.HttpUtility]::UrlEncode($album))
Sleep -seconds 1
foreach ($item in $xml.ItemSearchResponse.Items.Item)
{
if ($album.Contains($item.ItemAttributes.Title) -and $item.LargeImage)
{
$web.DownloadFile($item.LargeImage.URL,$path)
Sleep -seconds 1
break
}
}
}
}
misc/powershell.txt · Last modified: 2012/10/07 04:05 by 98.248.89.12