XBMC Community Forum
SortTV: Sort TV episodes, movies, and music into directories for xbmc (Linux/Win/Mac) - Printable Version

+- XBMC Community Forum (http://forum.xbmc.org)
+-- Forum: Help and Support (/forumdisplay.php?fid=33)
+--- Forum: Supplementary Tools for XBMC (/forumdisplay.php?fid=116)
+--- Thread: SortTV: Sort TV episodes, movies, and music into directories for xbmc (Linux/Win/Mac) (/showthread.php?tid=75949)

Pages: 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44


SortTV: Sort TV episodes, movies, and music into directories for xbmc (Linux/Win/Mac) - cliffe - 2010-06-20 09:59

~~~~~~
SortTV
~~~~~~
Automatically sorts TV show episodes, movies, and music into an organised directory structure.
For example, TV/[Show Name]/[Season X]/[Show Name - SXXEXX - Episode Name]

SortTV works on Linux, Windows, and Mac OS X.

- Automatically sorts your media, so you don't have to.
- Intelligently understands lots of different file naming schemes.
- Many renaming options, so you can have your files organised just the way you like.
- Can optionally rename episodes and movies using information from thetvdb and tmdb.
- Image downloading for movies and TV shows, seasons, and episodes.
- Extract compressed files such as .rar or .zip.
- Sort by move, copy, symlink, or hardlink.
- Integration with xbmc (notification and library update).
- Many many other options (see below / the README file).

SortTV provides the magic required to have your home theatre PC (HTPC) keep itself sorted out.
You can combine this script with an automatic episode downloader (there are many available*),
and this script runs in the background, sorting all of your new episodes, renaming them appropriately if you like.
Then, if you use xbmc as a front end, it can tell xbmc to check for the new episodes and
display a message to anyone viewing, that new episodes are available.

All you will have to do is sit back and enjoy.

* Episodes of many openly licensed shows are available, and episodes can be downloaded using
RSS feeds, torrent downloaders, file hosting site downloaders etc.

SortTV development has been driven by suggestions and code submissions. Thanks!

---
Copyright 2010 Z. Cliffe Schreuders
z.cliffe {at} schreuders.org
http://schreuders.org

Get SortTV here:
https://sourceforge.net/projects/sorttv/files/

Please discuss SortTV here on the xbmc forum:
http://forum.xbmc.org/showthread.php?t=75949

Paypal donations:
http://sourceforge.net/donate/index.php?group_id=330009

---
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.

---
Install

Make sure you have Perl installed.
If you are using Windows I recommend Strawberry Perl. Linux and Mac OS X often have Perl already installed.
On Mac OS X you may need to install the CLI developer tools.

Extract the SortTV script (sorttv.pl etc) wherever you like.
For example, on Linux into /home/[yourusername]/sorttv/

Edit the sorttv.conf file to the settings you want (at minimum specify the location of the directories to sort from and to)

Install the modules the script uses.
To do so, run this command on Linux or Mac OS X:
sudo cpan File::Copy::Recursive File::Glob LWP::Simple TVDB::API Getopt::Long Switch WWW::TheMovieDB::Search XML::Simple
On Windows run the same command, without "sudo"

---
Automation

If you want to make it sort new episodes every hour:

On Linux:
In a console type: crontab -e
On a new line enter "5 * * * * " followed by the command to run the script.
This will run the script 5 mins past every hour.
Eg:
5 * * * * nice perl /home/cliffe/sorttv/sorttv.pl

Every hour this would sort new episodes according to the configuration in /home/cliffe/sorttv/sorttv.conf

On Windows:
Create a .bat file with the command and use scheduler.
Google is your friend.

---
USAGE:

sorttv.pl [OPTIONS] [directory-to-sort directory-to-sort-into]

By default SortTV tries to read the configuration from sorttv.conf
(an example config file is provided with SortTV)

The simplest way to use SortTV is to edit the example config file, then just run "perl sorttv.pl" to do the sorting

You can overwrite any config options with commandline arguments, which match the format of the config file (except that each argument starts with "--")

OPTIONS:
There are now too many options to fit them in one forum post Smile
Please refer to the README document, which describes them all in detail

http://sourceforge.net/projects/sorttv/files//README/

EXAMPLES:
Does a sort, as configured in sorttv.conf:
perl sorttv.pl

The directory-to-sort and directory-to-sort-to can be supplied directly:
To sort a Downloads directory contents into a TV directory
perl sorttv.pl /home/me/Downloads /home/me/Videos/TV
Alternatively:
perl sorttv.pl --directory-to-sort=/home/me/Downloads --directory-to-sort-into=/home/me/Videos/TV

To move non-episode files in a separate directory:
perl sorttv.pl --directory-to-sort=/home/me/Downloads --directory-to-sort-into=/home/me/Videos/TV --non-episode-dir=/home/me/Videos/Non-episodes

To integrate with xbmc (notification and automatic library update):
perl sorttv.pl --directory-to-sort=/home/me/Downloads --directory-to-sort-into=/home/me/Videos/TV --xbmc-webserver=localhost:8080

And so on...

---

Frequently Asked Queries

Q: SortTV wont run, it gives an error indicating that modules are missing (cant find a .pm file)
A: Make sure you followed the instructions on installing the cpan modules. If that doesn't work, see below.

Q: I am having trouble installing the cpan modules...
A: If they are not passing the tests, you can force cpan to install them anyway. Run sudo cpan, then type "force install Switch", where Switch is the name of the module you are trying to install.

Q: Why doesn't SortTV download images for TV shows/episodes on Windows?
A: This is currently a limitation in the TVDB::API cpan module. The images should download fine for movies, or if you use SortTV on Linux all images should download correctly. Images downloaded by the module on Windows are corrupted, therefore this features is now automatically disabled for Windows users. Feel free to discuss this problem with the creator of the module.

Q: How do I submit code?
A: For simple patches, I suggest you make your changes in a copy, and create a patch using the command: "diff -ur dir1 dir2 > patch", where dir1 is a directory containing the latest release of SortTV, and dir2 is your-new-version. This creates a file named "patch" containing just your changes. Send "patch" via an email or via the forum. Alternatively, if you wish you can use Git. Find repo information on the SortTV sourceforge page.

Q: How can I say thank you / make a donation?
A: I am really glad you have found SortTV useful! Pop by the forum and say hello, or make a donation via Paypal here: http://sourceforge.net/donate/index.php?group_id=330009 If you would rather donate via Bitcoin let me know and I'll give you details.

Q: Something else...
A: Please discuss the issue on the forum

---
Enjoy!

Paypal donations welcome:
http://sourceforge.net/donate/index.php?group_id=330009

If you find this script helpful then please consider making a $5 paypal donation Smile


- davemex - 2010-07-13 20:56

Awesome tool! This worked like a charm, thanks for sharing it.

Used in conjunction with showrss and torrentflux-b4rt my system now automatically downloads the new TV shows and then sorts them where they need to go!


- cliffe - 2010-07-15 09:39

Davemex: Thanks for the feedback! Great to know it is helpful to other people too.

If anyone else is happily using it I would love to hear from you.

Cheers,

Cliffe.


- lukifer - 2010-07-25 00:20

I've got this running and it works great for flat files but seems to choke on copying directories.

From a log:


File /services/transmission/completed/District 9 (2009) DVDRip XviD-MAXSPEED cannot be copied to /media/0efb3f84-055d-4a8f-8790-7664dfb5a926/Downloads/. : Is a directory at /home/luke/Scripts/sorttv.pl line 44.
File /services/transmission/completed/Entourage - Season 06 cannot be copied to /media/0efb3f84-055d-4a8f-8790-7664dfb5a926/Downloads/. : Is a directory at /home/luke/Scripts/sorttv.pl line 44.
File /services/transmission/completed/Entourage - Season 1 cannot be copied to /media/0efb3f84-055d-4a8f-8790-7664dfb5a926/Downloads/. : Is a directory at /home/luke/Scripts/sorttv.pl line 44.
File /services/transmission/completed/IOpus.iMacros.V6.60.0.107-NoPE cannot be copied to /media/0efb3f84-055d-4a8f-8790-7664dfb5a926/Downloads/. : Is a directory at /home/luke/Scripts/sorttv.pl line 44.
File /services/transmission/completed/Iron.Man[2008]DvDrip-aXXo cannot be copied to /media/0efb3f84-055d-4a8f-8790-7664dfb5a926/Downloads/. : Is a directory at /home/luke/Scripts/sorttv.pl line 44.
File /services/transmission/completed/Kick-Ass (2010) R5 XViD-MAXSPEED cannot be copied to /media/0efb3f84-055d-4a8f-8790-7664dfb5a926/Downloads/. : Is a directory at /home/luke/Scripts/sorttv.pl line 44.


- cliffe - 2010-07-25 11:35

Hi lukifer. Thanks for letting me know about that.

What version of Perl are you using?

I have tested on v5.10.0 on OpenSUSE Linux 11.2 and v5.10.1 on Windows strawberry Perl, but couldn't replicate your problem. Directories sort with the same rules as regular files and the move command should work with directories.

Please try updating Perl, and check that the script is authorised to access the directories and let me know whether that fixes it or you are still having the same problem.

Thanks,

Cliffe.


- davemex - 2010-08-03 20:21

FYI I wrote up a full post on how I set up a hands-off home media server/XBMC box using your tool as an integral part for the media sorting and put it in the hardware forum at http://forum.xbmc.org/showthread.php?tid=77665

It looks like other people are finding this pretty useful!

If you want to show off people putting your hard work to good use the full write-up on my little project is on my blog. Big Grin

It's still working like a charm!


Great stuff! - marcozd - 2010-08-03 20:25

This is a fantastic piece of work.

Many thanks to you!

It is great to see such talented people contributing to XBMC!


- nitrag - 2010-08-20 01:55

I have two suggestions (still need help with the answer) for your script.

The issue is this: In the downloads folder (/home/username/downloads) all torrents are started/completed here. Firstly, your script doesn’t distinguish finished/still downloading, therefore, moves unfinished downloads to there respective folders. (Which works wonderfully by the way!) So how do we solve this?
Secondly, sometimes torrents look like this:

[HTML]sometorrentshow.torrent
->Tosh_0_S02E01_REALTV
–>toshmovie.avi
–>nfofile.nfo
–>downloadedfrom.txt[/HTML]

Here’s what your script does:

Code:
Sorting /home/torrents/matador/
into /home/Storage/TV/
trying to move Tosh.0 season 2 episode 19
making directory: /home/Storage/TV/Tosh 0
trying to move Tosh.0 season 2 episode 19
found a matching show:
        /home/Storage/TV/Tosh 0
making directory: /home/Storage/TV/Tosh 0/Season 2
found a matching show:
        /home/Storage/TV/Tosh 0
found a matching season:
        /home/Storage/TV/Tosh 0/Season 2
moving /home/torrents/matador/Tosh.0.S02E19.HDTV.XviD-aAF to /home/Storage/TV/Tosh 0/Season 2/Tosh.0.S02E19.HDTV.XviD-aAF
File /home/Storage/TV/Tosh 0 cannot be copied to /home/Storage/TV/Tosh 0/Season 2. : Is a directory at /home/torrents/sorttv.pl line 59.

My possible workaround that I’ve been trying to figure out:
The locating show/season works great, we just need to get the script to test if it’s a directory and if so, move all the directories sub-files which happen to be mainly avi’s.

Here’s my unfinished code:
Code:
#add header to line 14 or so
    use File::Glob ‘:glob’;
     #replacing line 58+59 with the following
     print “moving $file to “, $season . ‘/’ . filename($file), “\n”;
     if (-f $file) {
           #This is a file;
           move($file, $season . ‘/’ . filename($file)) or warn “File $show cannot be copied to $season. : $!”;
     }
     if (-d $file) {
           print “\nfound directory instead of file!\n”;
           foreach $file (glob(“$file/*.avi”)) {
                  #move later, let's just test now
                  #move($file, $season . ‘/’ . filename($file)) or warn “File $show cannot be copied to $season. : $!”;
                  print $file;
           }
     }

When I run the above I get the print statement saying it’s a directory, but not the print statement of the filename, which means that the for loop isn’t working.

Mainly I would just like to figure out the second issue. However, it is just a small change seeing as 95% of the other torrents work fine.

Great work!


- cliffe - 2010-08-20 03:05

davemex: Thanks for telling people about it. I am glad you have found it so helpful. Check out the slightly newer version, which have the arguments in a different order and some minor fixes.

marcozd: Thanks Smile


nitrag Wrote:Firstly, your script doesn’t distinguish finished/still downloading, therefore, moves unfinished downloads to there respective folders. (Which works wonderfully by the way!) So how do we solve this?

Most downloading programs have the option to move completed downloads into a different folder. Have a look at the options available in the program you are using.

nitrag Wrote:File /home/Storage/TV/Tosh 0 cannot be copied to /home/Storage/TV/Tosh 0/Season 2. : Is a directory at /home/torrents/sorttv.pl line 59.

This sounds like the same problem lukifer reported. As I mentioned, the script seems to move directories fine on my Linux and Windows boxes. So before we try to fix the problem it is important to figure out where the problem is. Could you please tell me what version of Perl you are using. If your Perl version is older than v5.10.1 please try updating it and tell me if that fixes directory moves. Also check the script has permission to access the directories and files. If that doesn't fix it then we can just switch the move command for another function which works better with directories.


- cliffe - 2010-08-20 03:13

nitrag Wrote:the for loop isn’t working.

After you try updating Perl, if you still want to just move the avi files out of the directory, try using bsd_glob() rather than glob(). glob() does strange things when file names contain spaces.


- nitrag - 2010-08-20 22:47

I have v5.10.1 of perl running on Ubuntu Server x64.

I also tried your bsd_glob suggestion with no luck.


New version of SortTV! - cliffe - 2010-09-02 11:07

A new and improved version of SortTV is now available!

New feature:
Sorts complete season directories. Eg "[sort]/Myshow Season 1" or "[sort]/Myshow - Series 1 Complete" would be moved to "[tv]/Myshow/Season 1" (if it does not already exist).

Fixes:
Supports even more naming schemes.
Various fixes such as handling underscores.
Hopefully fixes the directory move problem reported by some users. Please let me know.

--

You may need to install the modules the script uses.
To do so, run this command on Linux:
sudo cpan File::Copy::Recursive File::Glob LWP::Simple
On Windows run the same command, without "sudo"

--

Get SortTV here: https://sourceforge.net/projects/sorttv/files/

--

If you feel like this script saves you hours of manual sorting then please consider making a $5 paypal donation Smile
http://schreuders.org


- davemex - 2010-09-02 17:15

Thanks for the update! I updated the file on my server and so far it appears to be working correctly. I really appreciate all the work you've put into this project as it really puts a nice shinny coat on my whole media acquisition/management setup!


- megacrypto - 2010-09-03 01:22

just tested this script and it works nicely but for some reason it takes some movies with it. i had 3 episodes and 2 movies in one folder and ran the script on it, the script moved the 3 episodes just fine, each in its proper folder and season folder, but also moved one of the 2 movies and created a folder for it and a season folder too.

i will look more into this, but i thought i'd post it.

btw, i have a script that looks up tv episodes off thetvdb and creates its nfo and grabs its screen-shot. i will see if i can merge the two together, so after the move, it will lookup the episode info (i know xbmc does that for you, but i had this since some time ago, and im still debating whether to use the xbmc db or use mine and insert it into xbmc db)


- cliffe - 2010-09-03 17:59

megacrypto Wrote:just tested this script and it works nicely but for some reason it takes some movies with it. i had 3 episodes and 2 movies in one folder and ran the script on it, the script moved the 3 episodes just fine, each in its proper folder and season folder, but also moved one of the 2 movies and created a folder for it and a season folder too.

i will look more into this, but i thought i'd post it.

The new pattern matching is quite liberal. I pretty much only use it to sort episodes so I haven't had that problem. The movie title that was moved was probably followed by two numbers, which the script interpreted as season and episode numbers.

There are two simple solutions:
download tv episodes to a separate directory
or
edit the SortTV script to remove the third (catchall) condition in the tv pattern matching

I might do something about this in the future. If anyone else sees this behaviour let me know.

megacrypto Wrote:btw, i have a script that looks up tv episodes off thetvdb and creates its nfo and grabs its screen-shot. i will see if i can merge the two together, so after the move, it will lookup the episode info (i know xbmc does that for you, but i had this since some time ago, and im still debating whether to use the xbmc db or use mine and insert it into xbmc db)

I am pretty happy with the xbmc lookup and database, but it would be pretty easy to edit SortTV to call the other script once it has moved a new episode.

Thanks for the feedback Smile