Marc Kean
Marc Kean

Reputation: 533

Using PowerShell & Invoke-Webrequest to get specific information

My code is below, it's pretty much complete, however a tiny part I am stuck on.

Basically what the code does is, based on a Song, the script goes out to a couple of websites on the internet to bring back the song label, composer & year that the song was released.

Only thing is, with the particular song Alicia Keys - Girl On Fire, if you manually go to the website http://staff.australian-charts.com/showitem.asp?interpret=Alicia+Keys+feat%2E+Nicki+Minaj&titel=Girl+On+Fire&cat=s you will notice that there is more than one Composer listed under the Music/Lyrics section.

In the event the song has more than one composer, my script below in it's current state will grab the first composer listed only.

What I need is for the script to grab all the composers. If there's one composer, or if there are multiple composers, I need them captured in the format of "Composer1, Composer2, Composer3, Composer4" etc (Commas included)

I am thinking of changing the Invoke-Webrequest to get TABLES back and particular tables with rows etc, not sure....

$song = "Alicia Keys - Girl On Fire"
Write-Host $song
$SearchSong = $song -replace '\(' -replace '\)' -replace '&' -replace ' - ', ' ' -replace '\s','+'
$MatchSong = $song -replace ' - ', '&titel=' -replace '\s','\+'

#Check iTunes for music Label information
$uri = "https://itunes.apple.com/search?term=$SearchSong&country=au&entity=song"
$x = Invoke-WebRequest -Uri $uri
$iTunesResults = ($x.Content | ConvertFrom-Json).results
$y = Invoke-WebRequest -Uri $iTunesResults[0].trackViewUrl
$iTunesSongCopyright = ($y.ParsedHtml.getElementsByTagName('li') | ? {$_.ClassName -eq 'copyright'}).innerText -replace '℗ '
$iTunesSongLabel = $iTunesSongCopyright -replace '.*\d\s'

#The check australian-charts for Composer & Year infomation
$domain = 'http://staff.australian-charts.com/'
$uri = $domain + "search.asp?search=$SearchSong&cat=s"
$x = Invoke-WebRequest -Uri $uri

$x.AllElements[462].outerHTML -match 'A.href="(.*)"';$resultURL = $domain + $Matches[1]
$resultURL = $resultURL -replace("&","&") -replace('"','"')

$y = Invoke-WebRequest -Uri $resultURL

$Element = ($y.AllElements | ? {$_.tagName -eq 'HTML'})

    if($Element.innerText -match 'Music\/Lyrics:(.*)')
    {
        $Element.innerText -match 'Music\/Lyrics:(.*)'
        $Composer = $Matches[1]
        Write-Host $Composer

    } else {

        $Composer = $null
    }

    if($Element.innerText -match 'Year:(.*)')
    {
        $Element.innerText -match 'Year:(.*)'
        $Year = $Matches[1]
        Write-Host $Year

    } else {

        $Year = $null
    }

    Write-Host $iTunesSongLabel

Upvotes: 1

Views: 1291

Answers (1)

ClumsyPuffin
ClumsyPuffin

Reputation: 4069

you can use this for getting composer list :

 if($Element.innerText -match 'Music\/Lyrics:(.*)')
    {
    $startpos = $Element.innertext.IndexOf("Lyrics:") + 7
    $endpos = $Element.innertext.IndexOf("Producer:") -1
    $composer=$Element.innertext.substring($startpos,($endpos - $startpos))
    #even the below line will give the same result as the above line if uncommented
    #$composer = $Element.innertext[$startpos..$endpos] -join ""
    Write-Host $Composer

    }

Upvotes: 1

Related Questions