Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <#
- .NAME
- Create-BoxeeNFO.ps1
- .VERSION
- 0.1
- .AUTHOR
- YellowOnline
- .DATE
- 03/05/2013
- .LINK
- http://yellowonline.tweakblogs.net
- .SYNOPSIS
- Creates .nfo files for your movies based on IMDB information so that you aren't dependent on Boxees search feature.
- .COMMENT
- Movies should be properly formatted for this script to function, e.g. 'The.Matrix.(1999).mkv' or 'Triumph.des.Willens.(1935).part1.avi'
- 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
- conventions - see http://support.boxee.tv/entries/325263-Naming-Conventions.
- .USAGE
- Create-BoxeeNFO -Path <string> [-DownloadCovers] [-Overwrite]
- .PARAMETERS
- -Path : The path to your movie(s) folder
- .SWITCHES
- -DownloadCovers: Downloads the cover art to the movie folder and creates an absolute link in the NFO to a location in the local network.
- Relative paths are not supported by Boxee for obvious reasons.
- -Overwrite : Overwrites existing NFOs
- .TODO
- - Use year to handle multiple hits (eg. remakes) (PRIORITY)
- - Add more user options (on demand)
- - Handle XML better (not urgent)
- - Create a GUI (when I have too much time on my hands)
- - Port to C# (first I need to learn C# ^^)
- .THANKS
- Kudos to http://imdbapi.org without whom this script would have involved text scraping and other ugly things.
- #>
- #region PARAMETERS
- Param($Path = $(Throw "Please provide a folder to scan for movies."),[switch]$DownloadCovers,[switch]$Overwrite)
- If ((Test-Path $Path) -EQ $False){Throw "The provided path was not valid."}
- #endregion PARAMETERS
- #region CONSTANTS
- #OK, they're not really constants, but you're not supposed to fiddle with these
- $ScriptPath = Split-Path -Parent $myInvocation.MyCommand.Definition
- $ScriptStartTime = (Get-Date)
- #endregion CONSTANTS
- #region VARIABLES
- #Feel free to add movie formats or to change the log file location
- $VideoContainers = "*.3gp", "*.asf", "*.avi", "*.m4v", "*.mkv", "*.mov", "*.flv", "*.mp4", "*.mpeg", "*.mpg","*.rm", "*.vob", "*.wmv"
- $LogFile = $($ScriptPath + "\" + $(Get-Date -f yyyy-MM-dd) + " " + $(Get-Date -f HHmm) + " " + "Create-BoxeeNFO.log")
- #endregion VARIABLES
- #region FUNCTIONS
- Function Check-IMDB($MovieTitle, $MoviePath)
- {
- $OutputNFO = "$MoviePath\$MovieTitle.nfo"
- If (($MovieTitle -Match "part?") -And ($MovieTitle -NotMatch "part1"))
- {
- Write-Host "Processing $MovieTitle" -ForegroundColor Black -BackgroundColor White
- Write-Host " Multipart movie. Skipping." -ForegroundColor Yellow
- (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
- Write-Host "" -ForegroundColor White
- }
- Else
- {
- If (!$Overwrite -And ((Test-Path $OutputNFO) -EQ $True))
- {
- Write-Host "Processing $MovieTitle" -ForegroundColor Black -BackgroundColor White
- Write-Host " NFO already exists. Skipping." -ForegroundColor Yellow
- (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
- Write-Host "" -ForegroundColor White
- }
- Else
- {
- Write-Host "Processing $MovieTitle" -ForegroundColor Black -BackgroundColor White
- $MovieTitleStripped = $MovieTitle.ToString().ToLower() -Replace "\."," " -Replace "part\d", "" -Replace "\(\d{4}\)",""
- Write-Host " Stripped title to `'$([System.Threading.Thread]::CurrentThread.CurrentCulture.TextInfo.ToTitleCase($MovieTitleStripped))`'" -ForegroundColor White
- $MovieTitleQuery = $MovieTitle.Replace(" ","+")
- Write-Host " Sending request to IMDBAPI" -ForegroundColor White
- $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"
- [Net.HTTPWebRequest] $Request = [Net.WebRequest]::Create($URL)
- Write-Host " Getting response from IMDBAPI" -ForegroundColor White
- [Net.HTTPWebResponse] $Response = $Request.GetResponse()
- $ResponseStream = $Response.GetResponseStream()
- $StreamReader = New-Object IO.StreamReader($ResponseStream) -ErrorAction SilentlyContinue
- [XML]$XML = $StreamReader.ReadToEnd()
- Write-Host " Interpreting response from IMDBAPI" -ForegroundColor White
- If ($($XML.SelectNodes("/IMDBDocument") | ForEach-Object {$_.code}) -EQ 404)
- {
- Write-Host " `'$([System.Threading.Thread]::CurrentThread.CurrentCulture.TextInfo.ToTitleCase($MovieTitleStripped))`' could not be found :(" -ForegroundColor Red
- (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
- Write-Host "" -ForegroundColor White
- }
- Else
- {
- Write-Host " Parsing XML" -ForegroundColor White
- $NFOInput = New-Object System.Object
- $NFOInput | Add-Member -Type Noteproperty -Name Title -Value $($XML.SelectNodes("/IMDBDocumentList/item") | ForEach-Object {$_.title})
- $NFOInput | Add-Member -Type Noteproperty -Name Rating -Value $($XML.SelectNodes("/IMDBDocumentList/item") | ForEach-Object {$_.rating})
- $NFOInput | Add-Member -Type Noteproperty -Name Year -Value $($XML.SelectNodes("/IMDBDocumentList/item") | ForEach-Object {$_.year})
- $NFOInput | Add-Member -Type Noteproperty -Name Outline -Value $($XML.SelectNodes("/IMDBDocumentList/item") | ForEach-Object {$_.plot_simple})
- $RuntimeNode = $XML.SelectNodes("/IMDBDocumentList/item/runtime") | ForEach-Object {$_.InnerXML}
- If ($RuntimeNode -EQ $Null)
- {
- $Runtime = "Unknown"
- }
- Else
- {
- $RuntimeCleaned = $RuntimeNode.ToString().Replace("</item>",",").Replace("<item>","")
- $RuntimeTrimmed = $RuntimeCleaned.Substring(0, $RuntimeCleaned.Length - 1)
- $Runtime = $RuntimeTrimmed.Split(",")
- }
- $NFOInput | Add-Member -Type Noteproperty -Name Runtime -Value $Runtime
- $NFOInput | Add-Member -Type Noteproperty -Name Thumb -Value $($XML.SelectNodes("/IMDBDocumentList/item") | ForEach-Object {$_.poster})
- $NFOInput | Add-Member -Type Noteproperty -Name MPAA -Value $($XML.SelectNodes("/IMDBDocumentList/item") | ForEach-Object {$_.rated})
- $NFOInput | Add-Member -Type Noteproperty -Name ID -Value $($XML.SelectNodes("/IMDBDocumentList/item") | ForEach-Object {$_.imdb_id})
- $GenreNode = $XML.SelectNodes("/IMDBDocumentList/item/genres") | ForEach-Object {$_.InnerXML}
- If ($GenreNode -EQ $Null)
- {
- $Genre = "Unknown"
- }
- Else
- {
- $GenreCleaned = $GenreNode.ToString().Replace("</item>",",").Replace("<item>","")
- $Genre = $GenreCleaned.Substring(0, $GenreCleaned.Length - 1)
- }
- $NFOInput | Add-Member -Type Noteproperty -Name Genre -Value $Genre
- $DirectorNode = $XML.SelectNodes("/IMDBDocumentList/item/directors") | ForEach-Object {$_.InnerXML}
- If ($DirectorNode -EQ $Null)
- {
- $Director = "Unknown"
- }
- Else
- {
- $DirectorCleaned = $DirectorNode.ToString().Replace("</item>",",").Replace("<item>","")
- $Director = $DirectorCleaned.Substring(0, $DirectorCleaned.Length - 1)
- }
- $NFOInput | Add-Member -Type Noteproperty -Name Director -Value $Director
- $ActorNode = $XML.SelectNodes("/IMDBDocumentList/item/actors") | ForEach-Object {$_.InnerXML}
- If ($ActorNode -EQ $Null)
- {
- $Actor = "Unknown"
- }
- Else
- {
- $ActorCleaned = $ActorNode.ToString().Replace("</item>",",").Replace("<item>","")
- $ActorTrimmed = $ActorCleaned.Substring(0, $ActorCleaned.Length - 1)
- $Actor =$ActorTrimmed.Split(",")
- }
- $NFOInput | Add-Member -Type Noteproperty -Name Actor -Value $Actor
- Write-Host " Writing $OutputNFO" -ForegroundColor Green
- "<movie>" | Out-File $OutputNFO -Encoding UTF8
- " <title>$($NFOInput.Title)</title>" | Out-File $OutputNFO -Encoding UTF8 -Append
- " <rating>$($NFOInput.Rating)</rating>" | Out-File $OutputNFO -Encoding UTF8 -Append
- " <year>$($NFOInput.Year)</year>" | Out-File $OutputNFO -Encoding UTF8 -Append
- " <outline>$($NFOInput.Outline)</outline>" | Out-File $OutputNFO -Encoding UTF8 -Append
- " <runtime>$($NFOInput.Runtime[0])</runtime>" | Out-File $OutputNFO -Encoding UTF8 -Append
- If ($DownloadCovers)
- {
- Write-Host " Downloading cover" -ForegroundColor White
- If ($NFOInput.Thumb -EQ $Null)
- {
- Write-Host " No cover available." -ForegroundColor Yellow
- " <thumb>$($NFOInput.Thumb)</thumb>" | Out-File $OutputNFO -Encoding UTF8 -Append
- }
- Else
- {
- $SMBPath = $MoviePath.Replace("\","/").Replace("//","smb://")
- #The UserAgent is necessary for servers who bounce connections from unknown agents
- $UserAgent = "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.2;)"
- $WebClient = New-Object System.Net.WebClient
- $WebClient.Headers.Add("user-agent", $UserAgent)
- $ImageExtension = $NFOInput.Thumb.Split("/")[-1].Split(".")[-1]
- $Thumb = $($SMBPath + "/" + $MovieTitle + "." + $ImageExtension)
- Try
- {
- $WebClient.DownloadFile($NFOInput.Thumb, $($MoviePath + "\" + $MovieTitle + "." + $ImageExtension))
- " <thumb>$Thumb</thumb>" | Out-File $OutputNFO -Encoding UTF8 -Append
- }
- Catch
- {
- Write-Host " Could not download cover. Creating link instead." -ForegroundColor Yellow
- " <thumb>$($NFOInput.Thumb)</thumb>" | Out-File $OutputNFO -Encoding UTF8 -Append
- }
- }
- }
- Else
- {
- " <thumb>$($NFOInput.Thumb)</thumb>" | Out-File $OutputNFO -Encoding UTF8 -Append
- }
- " <mpaa>$($NFOInput.MPAA)</mpaa>" | Out-File $OutputNFO -Encoding UTF8 -Append
- " <id>$($NFOInput.ID)</id>" | Out-File $OutputNFO -Encoding UTF8 -Append
- " <genre>$($NFOInput.Genre.ToString())</genre>" | Out-File $OutputNFO -Encoding UTF8 -Append
- " <director>$($NFOInput.Director.ToSTring())</director>" | Out-File $OutputNFO -Encoding UTF8 -Append
- ForEach ($Actor in $NFOInput.Actor)
- {
- " <actor>" | Out-File $OutputNFO -Encoding UTF8 -Append
- " <name>$Actor</name>" | Out-File $OutputNFO -Encoding UTF8 -Append
- " </actor>" | Out-File $OutputNFO -Encoding UTF8 -Append
- }
- "</movie>" | Out-File $OutputNFO -Encoding UTF8 -Append
- (Get-Date -f yyyy-MM-dd) + " " + $(Get-Date -f HHmm) + " SUCCESS: Created .nfo for $MovieTitle" | Out-File $LogFile -Encoding UTF8 -Append
- Write-Host " Done!" -ForegroundColor White
- Write-Host "" -ForegroundColor White
- }
- }
- }
- }
- #endregion FUNCTIONS
- #region MAIN
- Write-Host "==================================================" -ForegroundColor White
- Write-Host " Create Boxee NFO v0.1 " -ForegroundColor White
- Write-Host "==================================================" -ForegroundColor White
- Write-Host "" -ForegroundColor White
- Write-Host "Enumerating movies in $Path" -ForegroundColor White
- Write-Host "" -ForegroundColor White
- $Movies = @(Get-ChildItem $Path -Include $Videocontainers -Recurse)
- If ($Movies.Count -eq 0)
- {
- }
- Else
- {
- ForEach ($Movie in $Movies)
- {
- Check-IMDB $Movie.BaseName $Movie.DirectoryName
- }
- }
- Write-Host "Script finished in $(((Get-Date) - $ScriptStartTime).TotalSeconds) seconds" -ForegroundColor White
- Write-Host "==================================================" -ForegroundColor White
- #endregion MAIN
- #EOF
Advertisement
Add Comment
Please, Sign In to add comment