YellowOnline

Create-BoxeeNFO.ps1

May 3rd, 2013
348
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. <#
  2. .NAME      
  3. Create-BoxeeNFO.ps1
  4.  
  5. .VERSION   
  6. 0.1
  7.  
  8. .AUTHOR    
  9. YellowOnline
  10.  
  11. .DATE      
  12. 03/05/2013
  13.  
  14. .LINK      
  15. http://yellowonline.tweakblogs.net
  16.  
  17. .SYNOPSIS
  18. Creates .nfo files for your movies based on IMDB information so that you aren't dependent on Boxees search feature.
  19.  
  20. .COMMENT
  21. Movies should be properly formatted for this script to function, e.g. 'The.Matrix.(1999).mkv' or 'Triumph.des.Willens.(1935).part1.avi'
  22. You'll have to alter the stripping part of the code to make it function with your collection if you do not comply with Boxee naming
  23. conventions - see http://support.boxee.tv/entries/325263-Naming-Conventions.
  24.  
  25. .USAGE
  26. Create-BoxeeNFO -Path <string> [-DownloadCovers] [-Overwrite]
  27.  
  28. .PARAMETERS
  29. -Path          : The path to your movie(s) folder
  30.  
  31. .SWITCHES
  32. -DownloadCovers: Downloads the cover art to the movie folder and creates an absolute link in the NFO to a location in the local network.
  33.                  Relative paths are not supported by Boxee for obvious reasons.
  34. -Overwrite     : Overwrites existing NFOs
  35.  
  36. .TODO
  37. - Use year to handle multiple hits (eg. remakes) (PRIORITY)
  38. - Add more user options (on demand)
  39. - Handle XML better (not urgent)
  40. - Create a GUI (when I have too much time on my hands)
  41. - Port to C# (first I need to learn C# ^^)
  42.  
  43. .THANKS
  44. Kudos to http://imdbapi.org without whom this script would have involved text scraping and other ugly things.
  45. #>
  46.  
  47. #region PARAMETERS
  48. Param($Path = $(Throw "Please provide a folder to scan for movies."),[switch]$DownloadCovers,[switch]$Overwrite)
  49. If ((Test-Path $Path) -EQ $False){Throw "The provided path was not valid."}
  50. #endregion PARAMETERS
  51.  
  52. #region CONSTANTS
  53. #OK, they're not really constants, but you're not supposed to fiddle with these
  54. $ScriptPath = Split-Path -Parent $myInvocation.MyCommand.Definition
  55. $ScriptStartTime = (Get-Date)
  56. #endregion CONSTANTS
  57.  
  58. #region VARIABLES
  59. #Feel free to add movie formats or to change the log file location
  60. $VideoContainers = "*.3gp", "*.asf", "*.avi", "*.m4v", "*.mkv", "*.mov", "*.flv", "*.mp4", "*.mpeg", "*.mpg","*.rm", "*.vob", "*.wmv"
  61. $LogFile = $($ScriptPath + "\" + $(Get-Date -f yyyy-MM-dd) + " " + $(Get-Date -f HHmm) + " " + "Create-BoxeeNFO.log")
  62. #endregion VARIABLES
  63.  
  64. #region FUNCTIONS
  65. Function Check-IMDB($MovieTitle, $MoviePath)
  66.     {
  67.     $OutputNFO = "$MoviePath\$MovieTitle.nfo"
  68.     If (($MovieTitle -Match "part?") -And ($MovieTitle -NotMatch "part1"))
  69.         {
  70.         Write-Host "Processing $MovieTitle" -ForegroundColor Black -BackgroundColor White
  71.         Write-Host "    Multipart movie. Skipping." -ForegroundColor Yellow
  72.         (Get-Date -f yyyy-MM-dd) + " " + $(Get-Date -f HHmm) + " WARNING: Did not create .nfo for $MovieTitle : not the first part of a multipart movie" | Out-File $LogFile -Encoding UTF8 -Append
  73.         Write-Host "" -ForegroundColor White
  74.         }
  75.     Else
  76.         {
  77.         If (!$Overwrite -And ((Test-Path $OutputNFO) -EQ $True))
  78.             {
  79.             Write-Host "Processing $MovieTitle" -ForegroundColor Black -BackgroundColor White
  80.             Write-Host "    NFO already exists. Skipping." -ForegroundColor Yellow
  81.             (Get-Date -f yyyy-MM-dd) + " " + $(Get-Date -f HHmm) + " WARNING: Did not create .nfo for $MovieTitle : .nfo already exists" | Out-File $LogFile -Encoding UTF8 -Append
  82.             Write-Host "" -ForegroundColor White
  83.             }
  84.         Else
  85.             {
  86.             Write-Host "Processing $MovieTitle" -ForegroundColor Black -BackgroundColor White
  87.             $MovieTitleStripped = $MovieTitle.ToString().ToLower() -Replace "\."," " -Replace "part\d", "" -Replace "\(\d{4}\)",""
  88.             Write-Host "    Stripped title to `'$([System.Threading.Thread]::CurrentThread.CurrentCulture.TextInfo.ToTitleCase($MovieTitleStripped))`'" -ForegroundColor White
  89.             $MovieTitleQuery = $MovieTitle.Replace(" ","+")
  90.             Write-Host "    Sending request to IMDBAPI" -ForegroundColor White
  91.             $URL = "http://imdbapi.org/?title=$MovieTitleQuery&type=xml&plot=simple&episode=1&limit=1&yg=0&mt=none&lang=en-US&offset=&aka=simple&release=simple&business=0&tech=0"
  92.             [Net.HTTPWebRequest] $Request = [Net.WebRequest]::Create($URL)
  93.             Write-Host "    Getting response from IMDBAPI" -ForegroundColor White
  94.             [Net.HTTPWebResponse] $Response = $Request.GetResponse()
  95.             $ResponseStream = $Response.GetResponseStream()
  96.             $StreamReader = New-Object IO.StreamReader($ResponseStream) -ErrorAction SilentlyContinue
  97.             [XML]$XML = $StreamReader.ReadToEnd()
  98.            
  99.             Write-Host "    Interpreting response from IMDBAPI" -ForegroundColor White
  100.             If ($($XML.SelectNodes("/IMDBDocument") | ForEach-Object {$_.code}) -EQ 404)
  101.                 {
  102.                 Write-Host "    `'$([System.Threading.Thread]::CurrentThread.CurrentCulture.TextInfo.ToTitleCase($MovieTitleStripped))`' could not be found :(" -ForegroundColor Red
  103.                 (Get-Date -f yyyy-MM-dd) + " " + $(Get-Date -f HHmm) + " ERROR: Failed to create .nfo for $MovieTitle : could not find it in the IMDB" | Out-File $LogFile -Encoding UTF8 -Append
  104.                 Write-Host "" -ForegroundColor White
  105.                 }
  106.             Else
  107.                 {  
  108.                 Write-Host "    Parsing XML" -ForegroundColor White
  109.                 $NFOInput = New-Object System.Object
  110.                 $NFOInput | Add-Member -Type Noteproperty -Name Title -Value $($XML.SelectNodes("/IMDBDocumentList/item") | ForEach-Object {$_.title})
  111.                 $NFOInput | Add-Member -Type Noteproperty -Name Rating -Value $($XML.SelectNodes("/IMDBDocumentList/item") | ForEach-Object {$_.rating})
  112.                 $NFOInput | Add-Member -Type Noteproperty -Name Year -Value $($XML.SelectNodes("/IMDBDocumentList/item") | ForEach-Object {$_.year})
  113.                 $NFOInput | Add-Member -Type Noteproperty -Name Outline -Value $($XML.SelectNodes("/IMDBDocumentList/item") | ForEach-Object {$_.plot_simple})
  114.                     $RuntimeNode = $XML.SelectNodes("/IMDBDocumentList/item/runtime") | ForEach-Object {$_.InnerXML}
  115.                     If ($RuntimeNode -EQ $Null)
  116.                         {
  117.                         $Runtime = "Unknown"
  118.                         }
  119.                     Else
  120.                         {
  121.                         $RuntimeCleaned = $RuntimeNode.ToString().Replace("</item>",",").Replace("<item>","")
  122.                         $RuntimeTrimmed = $RuntimeCleaned.Substring(0, $RuntimeCleaned.Length - 1)
  123.                         $Runtime = $RuntimeTrimmed.Split(",")
  124.                         }
  125.                 $NFOInput | Add-Member -Type Noteproperty -Name Runtime -Value $Runtime
  126.                 $NFOInput | Add-Member -Type Noteproperty -Name Thumb -Value $($XML.SelectNodes("/IMDBDocumentList/item") | ForEach-Object {$_.poster})
  127.                 $NFOInput | Add-Member -Type Noteproperty -Name MPAA -Value $($XML.SelectNodes("/IMDBDocumentList/item") | ForEach-Object {$_.rated})
  128.                 $NFOInput | Add-Member -Type Noteproperty -Name ID -Value $($XML.SelectNodes("/IMDBDocumentList/item") | ForEach-Object {$_.imdb_id})
  129.                     $GenreNode = $XML.SelectNodes("/IMDBDocumentList/item/genres") | ForEach-Object {$_.InnerXML}
  130.                     If ($GenreNode -EQ $Null)
  131.                         {
  132.                         $Genre = "Unknown"
  133.                         }
  134.                     Else
  135.                         {
  136.                         $GenreCleaned = $GenreNode.ToString().Replace("</item>",",").Replace("<item>","")
  137.                         $Genre = $GenreCleaned.Substring(0, $GenreCleaned.Length - 1)
  138.                         }
  139.                 $NFOInput | Add-Member -Type Noteproperty -Name Genre -Value $Genre
  140.                     $DirectorNode = $XML.SelectNodes("/IMDBDocumentList/item/directors") | ForEach-Object {$_.InnerXML}
  141.                     If ($DirectorNode -EQ $Null)
  142.                         {
  143.                         $Director = "Unknown"
  144.                         }
  145.                     Else
  146.                         {
  147.                         $DirectorCleaned = $DirectorNode.ToString().Replace("</item>",",").Replace("<item>","")
  148.                         $Director = $DirectorCleaned.Substring(0, $DirectorCleaned.Length - 1)
  149.                         }
  150.                 $NFOInput | Add-Member -Type Noteproperty -Name Director -Value $Director
  151.                     $ActorNode = $XML.SelectNodes("/IMDBDocumentList/item/actors") | ForEach-Object {$_.InnerXML}
  152.                     If ($ActorNode -EQ $Null)
  153.                         {
  154.                         $Actor = "Unknown"
  155.                         }
  156.                     Else
  157.                         {
  158.                     $ActorCleaned = $ActorNode.ToString().Replace("</item>",",").Replace("<item>","")
  159.                     $ActorTrimmed = $ActorCleaned.Substring(0, $ActorCleaned.Length - 1)
  160.                     $Actor =$ActorTrimmed.Split(",")
  161.                         }
  162.                 $NFOInput | Add-Member -Type Noteproperty -Name Actor -Value $Actor
  163.                
  164.                 Write-Host "    Writing $OutputNFO" -ForegroundColor Green
  165.                 "<movie>" | Out-File $OutputNFO -Encoding UTF8
  166.                 "    <title>$($NFOInput.Title)</title>" | Out-File $OutputNFO -Encoding UTF8 -Append
  167.                 "    <rating>$($NFOInput.Rating)</rating>" | Out-File $OutputNFO -Encoding UTF8 -Append
  168.                 "    <year>$($NFOInput.Year)</year>" | Out-File $OutputNFO -Encoding UTF8 -Append
  169.                 "    <outline>$($NFOInput.Outline)</outline>" | Out-File $OutputNFO -Encoding UTF8 -Append
  170.                 "    <runtime>$($NFOInput.Runtime[0])</runtime>" | Out-File $OutputNFO -Encoding UTF8 -Append
  171.                 If ($DownloadCovers)
  172.                     {
  173.                     Write-Host "    Downloading cover" -ForegroundColor White
  174.                     If ($NFOInput.Thumb -EQ $Null)
  175.                         {
  176.                         Write-Host "        No cover available." -ForegroundColor Yellow
  177.                         "    <thumb>$($NFOInput.Thumb)</thumb>" | Out-File $OutputNFO -Encoding UTF8 -Append
  178.                         }
  179.                     Else
  180.                         {
  181.                         $SMBPath =  $MoviePath.Replace("\","/").Replace("//","smb://")
  182.                         #The UserAgent is necessary for servers who bounce connections from unknown agents
  183.                         $UserAgent = "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.2;)"
  184.                         $WebClient = New-Object System.Net.WebClient
  185.                         $WebClient.Headers.Add("user-agent", $UserAgent)
  186.                         $ImageExtension = $NFOInput.Thumb.Split("/")[-1].Split(".")[-1]
  187.                         $Thumb = $($SMBPath + "/" + $MovieTitle + "." + $ImageExtension)
  188.                         Try
  189.                             {
  190.                             $WebClient.DownloadFile($NFOInput.Thumb, $($MoviePath + "\" + $MovieTitle + "." + $ImageExtension))
  191.                             "    <thumb>$Thumb</thumb>" | Out-File $OutputNFO -Encoding UTF8 -Append
  192.                             }
  193.                         Catch
  194.                             {
  195.                             Write-Host "        Could not download cover. Creating link instead." -ForegroundColor Yellow
  196.                             "    <thumb>$($NFOInput.Thumb)</thumb>" | Out-File $OutputNFO -Encoding UTF8 -Append
  197.                             }
  198.                         }
  199.                     }
  200.                 Else
  201.                     {
  202.                     "    <thumb>$($NFOInput.Thumb)</thumb>" | Out-File $OutputNFO -Encoding UTF8 -Append
  203.                     }
  204.                 "    <mpaa>$($NFOInput.MPAA)</mpaa>" | Out-File $OutputNFO -Encoding UTF8 -Append
  205.                 "    <id>$($NFOInput.ID)</id>" | Out-File $OutputNFO -Encoding UTF8 -Append
  206.                 "    <genre>$($NFOInput.Genre.ToString())</genre>" | Out-File $OutputNFO -Encoding UTF8 -Append
  207.                 "    <director>$($NFOInput.Director.ToSTring())</director>" | Out-File $OutputNFO -Encoding UTF8 -Append
  208.                 ForEach ($Actor in $NFOInput.Actor)
  209.                     {
  210.                     "    <actor>" | Out-File $OutputNFO -Encoding UTF8 -Append
  211.                     "        <name>$Actor</name>" | Out-File $OutputNFO -Encoding UTF8 -Append
  212.                     "    </actor>" | Out-File $OutputNFO -Encoding UTF8 -Append
  213.                     }
  214.                 "</movie>" | Out-File $OutputNFO -Encoding UTF8 -Append
  215.                                
  216.                 (Get-Date -f yyyy-MM-dd) + " " + $(Get-Date -f HHmm) + " SUCCESS: Created .nfo for $MovieTitle" | Out-File $LogFile -Encoding UTF8 -Append
  217.                 Write-Host "    Done!" -ForegroundColor White
  218.                 Write-Host "" -ForegroundColor White
  219.                 }
  220.             }
  221.         }
  222.     }
  223. #endregion FUNCTIONS
  224.  
  225. #region MAIN
  226. Write-Host "==================================================" -ForegroundColor White
  227. Write-Host "              Create Boxee NFO v0.1               " -ForegroundColor White
  228. Write-Host "==================================================" -ForegroundColor White
  229. Write-Host "" -ForegroundColor White
  230. Write-Host "Enumerating movies in $Path" -ForegroundColor White
  231. Write-Host "" -ForegroundColor White
  232. $Movies = @(Get-ChildItem $Path -Include $Videocontainers -Recurse)
  233. If ($Movies.Count -eq 0)
  234.     {
  235.     }
  236. Else
  237.     {
  238.     ForEach ($Movie in $Movies)
  239.         {
  240.         Check-IMDB $Movie.BaseName $Movie.DirectoryName
  241.         }
  242.     }
  243. Write-Host "Script finished in $(((Get-Date) - $ScriptStartTime).TotalSeconds) seconds" -ForegroundColor White
  244. Write-Host "==================================================" -ForegroundColor White
  245. #endregion MAIN
  246. #EOF
Advertisement
Add Comment
Please, Sign In to add comment