PoshCode Logo PowerShell Code Repository

Fetch IMDB info by Jeremiah 4 weeks ago (modification of post by DollarUnderscore view diff)
diff | embed code: <script type="text/javascript" src="http://PoshCode.org/embed/6822"></script>download | new post

I’m not sure if IMDB updated their site or what, but much of the HTML code referenced in the script either no longer exists on the sites or doesn’t seem to grab the correct information.

I went ahead and updated those pieces to get the right info. Here’s a couple more things I changed:

– $Released is now a DateTime value. – $RuntimeMinutes is now a TimeSpan value. – I added genres and the MPAA rating (R, PG-13, etc.)

I’m really glad you posted this because it seems to be the only IMDB-related script out there and I’ve been looking for this exact thing! Also, I’ve never messed with HTML before so that was a learning experience.

  1. function Get-IMDBMatch
  2. {
  3.     <#
  4.     .Synopsis
  5.        Retrieves search results from IMDB
  6.     .DESCRIPTION
  7.        This cmdlet posts a search to IMDB and returns the results.
  8.     .EXAMPLE
  9.        Get-IMDBMatch -Title 'American Dad!'
  10.     .EXAMPLE
  11.        Get-IMDBMatch -Title 'American Dad!' | Where-Object { $_.Type -eq 'TV Series' }
  12.     .PARAMETER Title
  13.        Specify the name of the tv show/movie you want to search for.
  14.  
  15.     #>
  16.  
  17.     [cmdletbinding()]
  18.     param([Parameter(Mandatory=$True, ValueFromPipeline=$true, ValueFromPipelineByPropertyName=$true)]
  19.           [string[]] $Title)
  20.  
  21.     BEGIN { }
  22.  
  23.     PROCESS {
  24.         foreach ($MediaTitle in $Title) {
  25.             $IMDBSearch = Invoke-WebRequest -Uri "http://www.imdb.com/find?q=$($MediaTitle -replace " ","+")&s=all" -UseBasicParsing
  26.  
  27.             $FoundMatches = $IMDBSearch.Content -split "<tr class=`"findresult " | select -Skip 1 | % { (($_ -split "<TD class=`"result_text`">")[1] -split "</TD>")[0] } | Select-String -Pattern "fn_al_tt_"
  28.  
  29.             foreach ($Match in $FoundMatches) {
  30.  
  31.                 $ID = (($Match -split "/title/")[1] -split "/")[0]
  32.                 $MatchTitle = (($Match -split ">")[1] -split "</a")[0]
  33.                 $Released = (($Match -split "</a> \(")[1] -split "\)")[0]
  34.                 $Type = (($Match -split "\) \(")[1] -split "\) ")[0]
  35.  
  36.                 if ($Type -eq "") {
  37.                     $Type = "Movie"
  38.                 }
  39.  
  40.                 if ($ID -eq "") {
  41.                     Continue
  42.                 }
  43.  
  44.                 $returnObject = New-Object System.Object
  45.                 $returnObject | Add-Member -Type NoteProperty -Name ID -Value $ID
  46.                 $returnObject | Add-Member -Type NoteProperty -Name Title -Value $MatchTitle
  47.                 $returnObject | Add-Member -Type NoteProperty -Name Released -Value $Released
  48.                 $returnObject | Add-Member -Type NoteProperty -Name Type -Value $Type
  49.  
  50.                 Write-Output $returnObject
  51.  
  52.                 Remove-Variable ID, MatchTitle, Released, Type
  53.    
  54.             }
  55.  
  56.             Remove-Variable FoundMatches, IMDBSearch
  57.         }
  58.     }
  59.  
  60.     END { }
  61. }
  62.  
  63.  
  64. function Get-IMDBItem
  65. {
  66.     <#
  67.     .Synopsis
  68.        Retrieves information about a movie/tv show etc. from IMDB.
  69.     .DESCRIPTION
  70.        This cmdlet fetches information about the movie/tv show matching the specified ID from IMDB.
  71.        The ID is often seen at the end of the URL at IMDB.
  72.     .EXAMPLE
  73.         Get-IMDBItem -ID tt0848228
  74.     .EXAMPLE
  75.        Get-IMDBMatch -Title 'American Dad!' | Get-IMDBItem
  76.  
  77.        This will fetch information about the item(s) piped from the Get-IMDBMatch cmdlet.
  78.     .PARAMETER ID
  79.        Specify the ID of the tv show/movie you want get. The ID has the format of tt0123456
  80.     #>
  81.  
  82.     [cmdletbinding()]
  83.     param([Parameter(Mandatory=$True, ValueFromPipeline=$true, ValueFromPipelineByPropertyName=$true)]
  84.           [string[]] $ID)
  85.  
  86.     BEGIN { }
  87.  
  88.     PROCESS {
  89.         foreach ($ImdbID in $ID) {
  90.  
  91.             $IMDBItem = Invoke-WebRequest -Uri "http://www.imdb.com/title/$ImdbID" -UseBasicParsing
  92.  
  93.             $ItemInfo = (($IMDBItem.Content -split "<div id=`"title-overview-widget`" class=`"heroic-overview`">")[1] -split "<div id=`"sidebar`">")[0]
  94.  
  95.             $ItemTitle = (($ItemInfo -split "<h1 itemprop=`"name`" class=`"`">")[1] -split "&nbsp;")[0]
  96.            
  97.             If (($ItemInfo -split "itemprop=`"datePublished`" content=`"").Length -gt 1) {
  98.                 $Type = "Movie"
  99.                 [DateTime]$Released = (($ItemInfo -split "<meta itemprop=`"datePublished`" content=`"")[1] -split "`" />")[0]
  100.             } Else {
  101.                 $Type = "TV Series"
  102.                 $Released = $null
  103.             }
  104.  
  105.             $Description = ((($ItemInfo -split "<div class=`"summary_text`" itemprop=`"description`">")[1] -split "</div>")[0]).Trim()
  106.            
  107.             $Rating = (($ItemInfo -split "<span itemprop=`"ratingValue`">")[1] -split "</span>")[0]
  108.            
  109.             $GenreSplit = $ItemInfo -split "itemprop=`"genre`">"
  110.             $NumGenres = ($GenreSplit.Length)-1
  111.             $Genres = foreach ($Genre in $GenreSplit[1..$NumGenres]) {
  112.                 ($Genre -split "</span>")[0]
  113.             }
  114.  
  115.             $MPAARating = (($ItemInfo -split "<meta itemprop=`"contentRating`" content=`"")[1] -split "`">")[0]
  116.  
  117.             try {
  118.                 $RuntimeMinutes = New-TimeSpan -Minutes (($ItemInfo -split "<time itemprop=`"duration`" datetime=`"PT")[1] -split "M`">")[0]
  119.             }
  120.             catch {
  121.                 $RuntimeMinutes = $null
  122.             }
  123.  
  124.             if ($Description -like '*Add a plot*') {
  125.                 $Description = $null
  126.             }
  127.  
  128.             $returnObject = New-Object System.Object
  129.             $returnObject | Add-Member -Type NoteProperty -Name ID -Value $ImdbID
  130.             $returnObject | Add-Member -Type NoteProperty -Name Type -Value $Type
  131.             $returnObject | Add-Member -Type NoteProperty -Name Title -Value $ItemTitle
  132.             $returnObject | Add-Member -Type NoteProperty -Name Genre -Value $Genres
  133.             $returnObject | Add-Member -Type NoteProperty -Name Description -Value $Description
  134.             $returnObject | Add-Member -Type NoteProperty -Name Released -Value $Released
  135.             $returnObject | Add-Member -Type NoteProperty -Name RuntimeMinutes -Value $RuntimeMinutes
  136.             $returnObject | Add-Member -Type NoteProperty -Name Rating -Value $Rating
  137.             $returnObject | Add-Member -Type NoteProperty -Name MPAARating -Value $MPAARating
  138.  
  139.             Write-Output $returnObject
  140.  
  141.             Remove-Variable IMDBItem, ItemInfo, ItemTitle, Genres, Description, Released, Type, Rating, RuntimeMinutes, MPAARating -ErrorAction SilentlyContinue
  142.         }
  143.     }
  144.  
  145.     END { }
  146. }

Submit a correction or amendment below (
click here to make a fresh posting)
After submitting an amendment, you'll be able to view the differences between the old and new posts easily.

Syntax highlighting:


Remember me