Skip to content

Instantly share code, notes, and snippets.

@matthenning
Created October 4, 2017 18:12
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 matthenning/e6685f2ce4b533147abeec2a11f2fe8e to your computer and use it in GitHub Desktop.
Save matthenning/e6685f2ce4b533147abeec2a11f2fe8e to your computer and use it in GitHub Desktop.
Kings Singers Parser
$albums = @()
$done = @()
$album_list = Invoke-WebRequest -Uri "http://www.kingssing.de/album-check.php"
$album_list.AllElements | Where {$_.href -like "album/*"} | %{
$id = ($_.href -split "/")[1]
Try {
$html = Invoke-WebRequest -Uri ("http://www.kingssing.de/album/{0}" -f $id) -ErrorAction Stop
}
Catch [Exception] {
Continue
}
$title = ($html.AllElements | Where {$_.TagName -eq "h1"}).outerText
if ($title -in $done) {
Continue
}
$done += $title
$album = New-Object -TypeName PSObject
$album | Add-Member -MemberType NoteProperty -Name "title" -Value $title
$album | Add-Member -MemberType NoteProperty -Name "tracks" -Value @()
$i = 1
$html.AllElements | Where {$_.TagName -eq "li" -and $_.class -eq "songlist"} | %{
$track = New-Object -TypeName PSObject
$track | Add-Member -MemberType NoteProperty -Name "num" -Value $i
$track | Add-Member -MemberType NoteProperty -Name "title" -Value ($_.outerText -split [Environment]::NewLine)[0]
$track | Add-Member -MemberType NoteProperty -Name "composers" -Value (($_.outerHTML -split "(<|>)")[16] -split ", ")
$track | Add-Member -MemberType NoteProperty -Name "singers" -Value (New-Object PSObject)
$track_details = Invoke-WebRequest -Uri ("http://www.kingssing.de/song/{0}" -f (($_.outerHTML -split "(<|>)")[6] -split '("|/)')[6])
$singers = ($track_details.ParsedHtml.getElementById("singers").textContent -csplit "(?=[A-Z])").Trim()
For ($j = 1; $j -lt $singers.Length; $j+=3) {
$role = ($singers[$j] -replace ":","") -replace "BaÃ", "Bass"
$name = $singers[($j+1)..($j+2)] -join " "
If ($track.singers.$role) {
$track.singers.$role += ", " + $name
}
Else {
$track.singers | Add-Member NoteProperty -Name $role -Value $name
}
}
$album.tracks += $track
$i += 1
}
$album.tracks = $album.tracks | Sort num
$albums += $album
Write-Host $album.title
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment