Series renaming script
Posted: Fri Oct 31, 2008 9:31 pm
I've been working with this for a while now and I think I've got it just right for me.
I use plex which will automatically scrape TV shows and download information about them if they are named appropriately. This is the reason for the organization of the files that I have.
By the way, I love plex and highly recommend it.
Write the rules you want, then chose Run Shell Script -> Embedded Script
Put the #!/usr/bin/php in the Shell line and everything from the php tag on, into the box below it.
Alternatively, save your script somewhere like your home directory and from terminal run
Here is the script:
This takes the file that hazel passes it, cleans it up how I like it and moves it to where it should be.
For example:
would be moved to:
It's long, I know, but it does everything I need perfectly. Combined with a few other hazel rules, transmission and TED, my media center is completely automated. A beautiful thing.
I'd be happy to help anyone as much as I can that wants to adjust this for their own preferences.
I use plex which will automatically scrape TV shows and download information about them if they are named appropriately. This is the reason for the organization of the files that I have.
By the way, I love plex and highly recommend it.
Write the rules you want, then chose Run Shell Script -> Embedded Script
Put the #!/usr/bin/php in the Shell line and everything from the php tag on, into the box below it.
Alternatively, save your script somewhere like your home directory and from terminal run
- Code: Select all
chmod 775 nameofyourscript
Here is the script:
- Code: Select all
#!/usr/bin/php
<?php
$moveTo = "/Users/myname/TV Shows/"; // The base directory that files will be moved to
$downloadDir = ''; // Initialize the variable for where the dowloaded file was saved
$paramArray = explode('/', $argv[1]); // Get all the parts of the path
for($i = 0; $i<sizeof($paramArray)-1; $i++){ // For each part (except the last) add it on to the download directory
$downloadDir .= $paramArray[$i].'/'; // Add the path part onto the previous part
}
$newName = $originalName = $paramArray[$i++]; // Save the file name
// Crap to strip out of the filename is stored in this directory
$crap = array('(XviD asd)', 'HDTV', 'PDTV', 'WS', 'DSRip', 'XviD', 'LOL', '2HD', 'aAF', '0TV', 'DOT', 'FQM', 'VTV', 'MOMENTUM',
'proper', 'GNARLY', '[ENG]', '[webrip]', 'ITALFYAH', 'REPACK', '720p', 'HDTV', 'x264', 'CTU', 'ANGELiC', 'NoTV', 'XOR');
// The extension is usually 4 chars (.avi) but could also be 3 or 5 chars. This loop finds the extension.
$i = -1; //The number of places that the extension should be
while(!preg_match("/\..*/", $ext)){ // While we haven't found the extension
$i--; // Try one charctar deeper
$ext = substr($newName, $i); // Save it to a variable
}// Now we should have the extension
$newName = substr($newName, 0, $i); //Get just the filename, no extension
foreach($crap as $v){ // Loop through $crap and remove anything in it
$newName = str_replace($v, '', $newName);
}
$newName = str_replace("."," ", $newName); // Replace . with a space
$newName = str_replace("-"," ", $newName); // Replace - with a space
$newName = str_replace("_"," ", $newName); // Replace _ with a space
while(preg_match('/ /', $newName)){ // While we have 2 spaces in a row
$newName = str_replace (" "," ", $newName); // Replace it with only one
}
$ep = preg_split("/(S0(\d)E(\d\d))?(?(1)|S(\d\d)E(\d\d))/i", $newName);// See if there is anything after the episode
preg_match("/(S0(\d)E(\d\d))?(?(1)|S(\d\d)E(\d\d))/i", $newName, $s); // Get the matches
if($s[2]){ // If we matched on 2, set that as the season
$season = $s[2];
}else{ //Otherwise the season match must be on 4 so set that as the season
$season = $s[4];
}
if($ep[1] == ' ' || !$ep[1]){ // If we have an episode name, add the trailing dash
$newName = preg_replace("/(S0(\d)E(\d\d))?(?(1)|S(\d\d)E(\d\d))/i", "- \\2\\4x\\3\\5", $newName);
}else{ // Otherwise, don't add a trailing dash
$newName = preg_replace("/(S0(\d)E(\d\d))?(?(1)|S(\d\d)E(\d\d))/i", "- \\2\\4x\\3\\5 -", $newName);
}
$newName = trim($newName); // Trip whitespace off then end of the filename
switch($ep[0]){ // Match the series with a list that tells us where to move it to
case '24 ':
$folder = '24 (2001)/';
$year = 2001 - 1 + $season;
$sfolder = "Season $season ($year)/";
break;
case '30 Rock ':
$folder = '30 Rock (2006)/';
$year = 2006 - 1 + $season;
$sfolder = "Season $season ($year)/";
break;
case 'Battlestar Galactica ':
$folder = 'Battlestar Galactica (2003)/';
$year = 2003 - 1 + $season;
$sfolder = "Season $season ($year)/";
break;
case 'Bones ':
$folder = 'Bones (2005)/';
$year = 2005 - 1 + $season;
$sfolder = "Season $season ($year)/";
break;
case 'Burn Notice ':
$folder = 'Burn Notice (2007)/';
$year = 2007 - 1 + $season;
$sfolder = "Season $season ($year)/";
break;
case 'CSI ':
$folder = 'CSI - Crime Scene Investigation (2000)/';
$year = 2000 - 1 + $season;
$sfolder = "Season $season ($year)/";
break;
case 'Chuck ':
$folder = 'Chuck (2007)/';
$year = 2007 - 1 + $season;
$sfolder = "Season $season ($year)/";
break;
case 'Dexter ':
$folder = 'Dexter (2006)/';
$year = 2006 - 1 + $season;
$sfolder = "Season $season ($year)/";
break;
case 'Doctor Who ':
$folder = 'Doctor Who (2005)/';
$year = 2005 - 1 + $season;
$sfolder = "Season $season ($year)/";
break;
case 'Fringe ':
$folder = 'Fringe (2008)/';
$year = 2008 - 1 + $season;
$sfolder = "Season $season ($year)/";
break;
case 'Ghost Whisperer ':
$folder = 'Ghost Whisperer (2005)/';
$year = 2005 - 1 + $season;
$sfolder = "Season $season ($year)/";
break;
case "Grey's Anatomy ":
case "Greys Anatomy ":
$folder = "Grey's Anatomy (2005)/";
$year = 2005 - 1 + $season;
$sfolder = "Season $season ($year)/";
break;
case 'Heroes ':
$folder = 'Heroes (2006)/';
$year = 2006 - 1 + $season;
$sfolder = "Season $season ($year)/";
break;
case 'Knight Rider ':
case 'Knight Rider 2008 ':
$folder = 'Knight Rider (2008)/';
$year = 2008 - 1 + $season;
$sfolder = "Season $season ($year)/";
break;
case 'Monk ':
$folder = 'Monk (2002)/';
$year = 2002 - 1 + $season;
$sfolder = "Season $season ($year)/";
break;
case 'My Name Is Earl ':
case 'My Name is Earl ':
$folder = 'My Name Is Earl (2005)/';
$year = 2005 - 1 + $season;
$sfolder = "Season $season ($year)/";
break;
case 'Mythbusters ':
$folder = 'Mythbusters (2003)/';
$year = 2003 - 1 + $season;
$sfolder = "Season $season ($year)/";
break;
case 'Psych ':
$folder = 'Psych (2006)/';
$year = 2006 - 1 + $season;
$sfolder = "Season $season ($year)/";
break;
case 'Pushing Daisies ':
$folder = 'Pushing Daisies (2007)/';
$year = 2007 - 1 + $season;
$sfolder = "Season $season ($year)/";
break;
case 'Scrubs ':
$folder = 'Scrubs (2001)/';
$year = 2001 - 1 + $season;
$sfolder = "Season $season ($year)/";
break;
case 'The IT Crowd ':
$folder = 'The IT Crowd Season (2006)/';
$year = 2006 - 1 + $season;
$sfolder = "Season $season ($year)/";
break;
case 'The Office ':
$folder = 'The Office (2005)/';
$year = 2005 - 1 + $season;
$sfolder = "Season $season ($year)/";
break;
case 'The Unit ':
$folder = 'The Unit (2006)/';
$year = 2006 - 2 + $season;
$sfolder = "Season $season ($year)/";
break;
case 'Ugly Betty ':
$folder = 'Ugly Betty (2006)/';
$year = 2006 - 1 + $season;
$sfolder = "Season $season ($year)/";
break;
}
$moveTo .= $folder.$sfolder.$newName.$ext; // Save the entire path including the new filename into $moveTo
if(!rename($downloadDir.$originalName, $moveTo)){ // Move the file. If it doesn't work, diagnose the problem.
// Note: Anything inside this IF will only happen if the file doesn't get moved.
if(!is_file($downloadDir.$originalName)){ // If we don't have a valid original file, print it and say so
error_log($downloadDir.$originalName,"is NOT a file");
}
if(!is_dir($moveTo.$folder.$sfolder)){ // If we don't have a valid folder to move to, print it and say so
error_log($moveTo.$folder.$sfolder." is NOT a folder");
}
error_log('Season'.$season); // The season might be useful
error_log(print_r($s, true)); // And let's see what's in $s, too.
}
This takes the file that hazel passes it, cleans it up how I like it and moves it to where it should be.
For example:
- Code: Select all
Mythbusters.S06E16.Alcohol.Myths.720p.HDTV.x264-DHD.avi
would be moved to:
- Code: Select all
/Users/myname/TV Shows/Mythbusters (2003)/Season 6 (2008)/Mythbusters - 6x16 - Alcohol Myths.avi
It's long, I know, but it does everything I need perfectly. Combined with a few other hazel rules, transmission and TED, my media center is completely automated. A beautiful thing.
I'd be happy to help anyone as much as I can that wants to adjust this for their own preferences.