Series renaming script

From your noodle to other noodles. Talk about ways to get the most from Hazel. Even exchange recipes for the cool rules you've thought up. DO NOT POST YOUR QUESTIONS HERE.

Moderators: Mr_Noodle, Moderators

Series renaming script Fri Oct 31, 2008 9:31 pm • by Gidgidonihah
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
Code: Select all
chmod 775 nameofyourscript
then just choose that file instead of embedded script. This is what I'm doing.
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.
Gidgidonihah
 
Posts: 19
Joined: Mon Jul 28, 2008 6:18 pm

Re: Series renaming script Fri Oct 31, 2008 9:34 pm • by Gidgidonihah
By the way, the script won't create any folders, so you have to already have them there.
ie. At the beginning of every season, you have to make a new season folder.

Or just rename the current one and buy the previous season :p
Gidgidonihah
 
Posts: 19
Joined: Mon Jul 28, 2008 6:18 pm

Re: Series renaming script Sat Oct 17, 2009 6:37 pm • by shmerek
This looks totally awesome. I tried to use the script you provided but substituted the series name year etc over one in the script but Hazel just gives me an alert
At least one of the actions has an empty field or an error. Please fill in the field, correct the error or remove the action.

The script is way beyond me for me to solve the error I put the #!/usr/bin/php in the shell box and the rest of in in the box below to no avail. Any ideas? There is also a note
Use $argv[1] to refer the file being processed
Is that something I need to worry about?
shmerek
 
Posts: 5
Joined: Wed Sep 16, 2009 7:48 pm

Re: Series renaming script Mon Oct 19, 2009 11:11 am • by Mr_Noodle
Remove the #! from the shell field (i.e. just enter "/usr/bin/php").
Mr_Noodle
Site Admin
 
Posts: 8571
Joined: Sun Sep 03, 2006 1:30 am
Location: New York City

Re: Series renaming script Fri Oct 23, 2009 6:06 pm • by a_freyer
Thats some good work - but I have to admit, highly customized. I use the app Name Mangler (http://www.many-tricks.com/namemangler/) for all of my bulk renaming.
a_freyer
 
Posts: 631
Joined: Tue Sep 30, 2008 9:21 am
Location: Colorado

Re: Series renaming script Wed Oct 28, 2009 2:14 am • by shmerek
Mr_Noodle wrote:Remove the #! from the shell field (i.e. just enter "/usr/bin/php").

Cool thanks for the tip. This script is genius
shmerek
 
Posts: 5
Joined: Wed Sep 16, 2009 7:48 pm

Re: Series renaming script Wed Oct 28, 2009 11:35 am • by Mr_Noodle
By the way, I should've brought this up before but was probably distracted at the time. A good deal of this can be done using Hazel's match patterns and custom tokens.

Taking the example:
Code: Select all
Mythbusters.S06E16.Alcohol.Myths.720p.HDTV.x264-DHD.avi

You can do a match pattern on the name like:
Code: Select all
(•show).S(•season)E(•episode).(•episode name).(number)(letter).(...)

where you define the custom tokens as follows:
  • show: (...)
  • season: (number)
  • episode: (number)
  • episode name: (...)

You can then recombine it as follows:

Sort into subfolder: (•show)/Season (•season)
Rename: (•show) - (•season)x(•episode) - (•episode name)

This is roughly equivalent to the script except the following:
  • It doesn't include the year after the show and season folder names. This seems to be a fixed value that is not derived from the file.
  • The dots in the episode name do not get converted to spaces.

If you are ok with that, then you can use one rule and you don't have to keep adding entries for new shows as long as the files follow that same format. You might have to tweak the above but hopefully it shows you how to utilize the match patterns and custom tokens properly.
Mr_Noodle
Site Admin
 
Posts: 8571
Joined: Sun Sep 03, 2006 1:30 am
Location: New York City

Re: Series renaming script Wed Oct 28, 2009 3:14 pm • by shmerek
Mr_Noodle wrote:By the way, I should've brought this up before but was probably distracted at the time. A good deal of this can be done using Hazel's match patterns and custom tokens.

Taking the example:
Code: Select all
Mythbusters.S06E16.Alcohol.Myths.720p.HDTV.x264-DHD.avi

You can do a match pattern on the name like:
Code: Select all
(•show).S(•season)E(•episode).(•episode name).(number)(letter).(...)

where you define the custom tokens as follows:
  • show: (...)
  • season: (number)
  • episode: (number)
  • episode name: (...)

You can then recombine it as follows:

Sort into subfolder: (•show)/Season (•season)
Rename: (•show) - (•season)x(•episode) - (•episode name)

This is roughly equivalent to the script except the following:
  • It doesn't include the year after the show and season folder names. This seems to be a fixed value that is not derived from the file.
  • The dots in the episode name do not get converted to spaces.

If you are ok with that, then you can use one rule and you don't have to keep adding entries for new shows as long as the files follow that same format. You might have to tweak the above but hopefully it shows you how to utilize the match patterns and custom tokens properly.

sweet that is pretty cool too.
shmerek
 
Posts: 5
Joined: Wed Sep 16, 2009 7:48 pm

Re: Series renaming script Mon Jan 18, 2010 1:07 pm • by Gidgidonihah
Mr_Noodle wrote:...
If you are ok with that, then you can use one rule and you don't have to keep adding entries for new shows as long as the files follow that same format. You might have to tweak the above but hopefully it shows you how to utilize the match patterns and custom tokens properly.


Oh that's good to know. The year is a fixed value, and something Plex needs to be able to scrape and find information for the right show.
I've been meaning to simplify this script so that it uses another flat file for the show information to make it simpler to update, but it's been working and if it ain't broke...

And yep, it's highly customized. Plex needs the files in specific locations for scraping. Now it's all automatic. I have something like Tivo's Season Pass, without the Tivo.

Also, someone PMd me asking what to change to make it so that it only renamed the files and not moved them.
The answer is change the $moveTo at the top of the script. If the files are in /foo/bar and you want them to stay in /foo/bar, change it to /foo/bar.
Gidgidonihah
 
Posts: 19
Joined: Mon Jul 28, 2008 6:18 pm

Re: Series renaming script Mon Feb 08, 2010 3:32 pm • by silvine
How are you handling movie files with this script?
silvine
 
Posts: 17
Joined: Mon Feb 08, 2010 3:30 pm

Re: Series renaming script Mon Feb 08, 2010 4:05 pm • by Gidgidonihah
I actually have transmission running rules against the size of the file to label it as a different color. Or I can manually add this color to to the transfer. Any transfers with that color will be saved into a different folder and thus won't run through this script.
Gidgidonihah
 
Posts: 19
Joined: Mon Jul 28, 2008 6:18 pm

Re: Series renaming script Mon Feb 08, 2010 4:30 pm • by silvine
do you have a script for movies and Plex or is there someway Hazel can do it? Trying to automate my media centre...
silvine
 
Posts: 17
Joined: Mon Feb 08, 2010 3:30 pm

Re: Series renaming script Mon Feb 08, 2010 4:47 pm • by Gidgidonihah
I don't because I don't really download movies that often.
The premise would be the same, but since you don't need to know the year/season for the movie it would be even simpler. Mr Noodle was talking about how if it weren't for that, it could be done solely with Hazel. That's gonna be the way to go.
I haven't done that, though so, might want to break that question off into it's own thread.
Gidgidonihah
 
Posts: 19
Joined: Mon Jul 28, 2008 6:18 pm

Re: Series renaming script Tue Feb 09, 2010 4:08 am • by silvine
Okay I think I've got the custom token renaming rule mentioned above set up. However, I have two questions. Firstly what about TV shows with multiple words in the name e.g. 'Six.Feet.Under.S02EO2.avi'? Secondly do I need to rewrite a rule for files named differently eg '2x01 Six Feet Under Broken Promises.avi'?
silvine
 
Posts: 17
Joined: Mon Feb 08, 2010 3:30 pm

Re: Series renaming script Tue Feb 09, 2010 12:16 pm • by Gidgidonihah
The dots in the episode name do not get converted to spaces.
And yes.
Gidgidonihah
 
Posts: 19
Joined: Mon Jul 28, 2008 6:18 pm

Next

Return to Tips & Tricks - DO NOT POST QUESTIONS