January 1st, 2011

Weekend Scripter: Download Lyrics for Your Favorite Song with PowerShell

 

Summary: Learn how to use a Windows PowerShell function to download lyrics for your favorite songs.

 

Retrieving song lyrics with Windows PowerShell

Microsoft Scripting Guy Ed Wilson here. Well it is New Year’s day in Charlotte, North Carolina. This year, or perhaps I should say last year, the Scripting Wife and I did not do anything for New Year’s Eve. I had spent a few days looking around, and found some pretty cool things going on down in Charleston, South Carolina that looked promising. However, with my continuing ear problems, we decided that going to listen to a live band is not necessarily the best thing to do. What I really wanted to find was a Windows PowerShell New Year’s Eve gathering. I mean we could arrange for a half dozen speakers and do all PowerShell all night. Now that is something to get excited about! It would be way cooler than the Elvis contest we went to a couple of years ago.

Because we did not do anything last night, I am up bright and early and cooking a pot of steel cut Irish Oats. These dudes take nearly an hour to cook, but it is worth the extra trouble. I like to sprinkle them with fresh ground Cinnamon. Anyway, while I wait for the oats to come to a boil, I decided to look around for a web service I could use to retrieve song lyrics for the songs I listen to on my Zune HD. This is something I have wanted to do for quite some time, but I have always had more important things to do.

That is the good thing about scripting on New Year’s Day because one can pretty much spend ones time doing whatever one wishes to do. After all, it is New Year’s Day. Speaking of Elvis, have you ever tried to figure out what he is talking about in some of his songs? Between the “ooh ha ooh ha” and the “thank you, thank you very much” lots of words transpire that are fairly unintelligible. That is the good thing about retrieving song lyrics; I can finally understand the song (in some cases that might not necessarily be such a good thing). The complete Get-Lyrics function appears here.

Get-Lyrics function

Function Get-Lyrics

{

  <#

   .Synopsis

    This function returns song lyrics when provided with the name of a song

   .Description

    This function uses a web service to retrieve song lyrics

   .Example

    Get-Lyrics -artist “John Denver” -song “Rocky Mountain High”

    Returns the lyrics to John Denver’s song “Rocky Mountain High”

   .Example

    Get-Lyrics -artist elvis -song “hound dog”

    Returns the lyrics to Elvis Presley’s song Hound Dog

   .Parameter artist

    The artist parameter accepts an artist name as a string

   .Parameter song

    The song parameter accepts a song title as a string

   .Inputs

    [string]

   .Outputs

    [string[]]

   .Notes

    NAME:  Get-Lyrics

    AUTHOR: ed wilson, msft

    LASTEDIT: 12/21/2010 17:47:31

    KEYWORDS: Using the Internet, Web Services

    HSG: 1/1/11

   .Link

     Http://www.ScriptingGuys.com

 #Requires -Version 2.0

 #>

 

 Param(

  [string]$artist,

  [string]$song )

 $URI = “http://lyrics.wikia.com/server.php?wsdl”

 $Proxy = New-WebServiceProxy -uri $URI -namespace WebServiceProxy

 $details = $Proxy.GetSong($artist,$song)

 “Details”

 $details.Lyrics

} #end Get-Lyrics

Most of the Get-Lyrics function is taken up by the comment-based help. I used my Add-Help function to add most of the help tags, and it therefore only took me about five minutes to fill in the blanks.

I have a written several Hey, Scripting Guy! posts that talk about how to add help to Windows PowerShell scripts. Refer to them for more information about how to use this helpful (pun intended) scripting technique.

The complete comment-based help for the Get-Lyrics function appears here.

<#

   .Synopsis

    This function returns song lyrics when provided with the name of a song

   .Description

    This function uses a web service to retrieve song lyrics

   .Example

    Get-Lyrics -artist “John Denver” -song “Rocky Mountain High”

    Returns the lyrics to John Denver’s song “Rocky Mountain High”

   .Example

    Get-Lyrics -artist elvis -song “hound dog”

    Returns the lyrics to Elvis Presley’s song Hound Dog

   .Parameter artist

    The artist parameter accepts an artist name as a string

   .Parameter song

    The song parameter accepts a song title as a string

   .Inputs

    [string]

   .Outputs

    [string[]]

   .Notes

    NAME:  Get-Lyrics

    AUTHOR: ed wilson, msft

    LASTEDIT: 12/21/2010 17:47:31

    KEYWORDS: Using the Internet, Web Services

    HSG: 1/1/11

   .Link

     Http://www.ScriptingGuys.com

 #Requires -Version 2.0

 #>

The cool thing about the help, is that it integrates with the Get-Help Windows PowerShell cmdlet. Therefore, I can use the following command to retrieve help information.

Get-Help Get-Lyrics

 

In addition to the general help information, I can retrieve only the examples of usage, or the complete help information. The commands to do this appear here.

Get-Help Get-Lyrics –examples

Get-Help Get-Lyrics –full

 

An example of the default help information appears here.

PS C:\Users\ed.NWTRADERS> Get-Help get-lyrics

 

NAME

    Get-Lyrics

   

SYNOPSIS

    This function returns song lyrics when provided with the name of a song

   

   

SYNTAX

    Get-Lyrics [[-artist] <String>] [[-song] <String>] [<CommonParameters>]

   

   

DESCRIPTION

    This function uses a web service to retrieve song lyrics

   

 

RELATED LINKS

    Http://www.ScriptingGuys.com

    #Requires -Version 2.0

   

    Get-Lyrics -artist “John Denver” -song “Rocky Mountain High”

 

REMARKS

    To see the examples, type: “get-help Get-Lyrics -examples”.

    For more information, type: “get-help Get-Lyrics -detailed”.

    For technical information, type: “get-help Get-Lyrics -full”.

The portion of the function that performs the actual work is relative lean. It is seen here.

Param(

  [string]$artist,

  [string]$song )

 $URI = “http://lyrics.wikia.com/server.php?wsdl”

 $Proxy = New-WebServiceProxy -uri $URI -namespace WebServiceProxy

 $details = $Proxy.GetSong($artist,$song)

 “Details”

 $details.Lyrics

 

I create two parameters for the function: the artist name and the song title. The $URI variable holds the path to the WSDL (Web Service Definition Language). The New-WebServiceProxy Windows PowerShell cmdlet accepts the path to the WSDL, and I store the returned object in the $Proxy variable. I then call the getsong method and pass it the artist and the song. The object that returns contains a lyrics property that holds the song’s lyrics. The following figure shows how to use this function directly inside the Windows PowerShell ISE. I run the script one time to load the function into memory, and then I can call the function directly from the command pane.

 

Well, that is about it for today. I think I hear the Scripting Wife calling. She has been busy cooking black-eyed peas and who knows what else. After all, it is New Year’s Day in Charlotte.

I invite you to follow me on Twitter or Facebook. If you have any questions, send email to me at scripter@microsoft.com or post them on the Official Scripting Guys Forum. See you tomorrow. Until then, peace.

 

Ed Wilson, Microsoft Scripting Guy

Author

0 comments

Discussion are closed.