21 Jul 2010

I am suffering a nasty chest infection, and it has been tough getting to bed over the last few days. As a result, I have been staying up late, coding on a few random scripts.

The result of last night’s fevered coding was a PowerShell script to return the ratings for various TV shows on IMDB. This is my first attempt at the script, and it makes use of the IMDB web service provided by Dean Clatworthy.

The script consists of two main parts. The first is a call to the web service, and returning the details for the specified TV show, using an instance of the .NET WebClient.

1: function Get-ShowDetails

2: (

3: [string]$showName = $(Throw "TV show name is required!")

4: )

5: {

6: $webClient = new-object System.Net.WebClient

7: $webClient.Headers.Add("user-agent", "PowerShell Script")

8: $queryString = $showName.Trim().Replace("", "+")

9: $query = [string]::Format("{0}={1}", $queryUrl, $queryString)

10: $showDetails = $webClient.DownloadString($query)

11: return $showDetails

12: }

The second part of the script extracts the rating details for the show from the plain text returned by the web service, using a regular expression:

1: function Extract-RatingDetails

2: (

3: [string]$showName = $(Throw "TV show name is required!"),

4: [string]$showDetails = $(Throw "TV show details are required!")

5: )

6: {

7: $regexPattern = '"rating":"([a-z\\\/\.0-9]+)","votes":([0-9\"]+)'

8: $regexMatcher = [regex] "$regexPattern"

9: $matches = $regexMatcher.Matches($showDetails)

10: $showInfo = $null

11:

12: foreach ($match in $matches)

13: {

14: if ($match.Success)

15: {

16: # To return all of the matched expression, use $match.Groups[0].Value

17: $rating = $match.Groups[1].Value

18:

19: if($rating -eq "n\/a" )

20: {

21: $rating = 0;

22: }

23:

24: $votes = $match.Groups[2].Value.Replace('"', '')

25: $showInfo = New-Object PSObject

26: $showInfo | Add-Member NoteProperty "ShowTitle" $showName

27: $showInfo | Add-Member NoteProperty "Rating" $rating

28: $showInfo | Add-Member NoteProperty "Votes" $votes

29: break;

30: }

31: }

32:

33: return $showInfo

34: }

This isn’t a finished script, though it does work. There is a limit to the number of calls that can be made to the web service (30 per hour), which means that I will have to revisit the script and instead of calling the web service, scrape the details of each TV series from the IMDB website.

Download the current script from here. If you have any comments, I would appreciate all feedback. Thanks.

Also, just to note that the code for this post was highlighted using the Source Code Formatter for Windows Live Writer.