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

+- Kodi Community Forum (https://forum.kodi.tv)
+-- Forum: Support (https://forum.kodi.tv/forumdisplay.php?fid=33)
+--- Forum: Supplementary Tools for Kodi (https://forum.kodi.tv/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 45 46 47 48 49


- gardz - 2011-02-20

Nice suggestions Cliffe, I think I've implemented them properly now Smile I'm still getting the hang of perl, and love some of the conventions you've been using throughout your code!

Code:
diff -u defaults/sorttv.pl sorttv.pl
--- defaults/sorttv.pl  2011-02-18 11:53:07.918356001 +1100
+++ sorttv.pl   2011-02-20 23:36:20.698356001 +1100
@@ -49,7 +49,7 @@
my ($sortdir, $tvdir, $nonepisodedir, $xbmcwebserver, $matchtype);
my ($showname, $series, $episode, $pureshowname) = "";
my ($newshows, $new, $log);
-my (@showrenames, @showtvdbids, @whitelist, @blacklist);
+my (@showrenames, @showtvdbids, @whitelist, @blacklist, @sizerange);
my $REDO_FILE = my $moveseasons = my $windowsnames = my $tvdbrename = my $lookupseasonep = my $extractrar = "TRUE";
my $usedots = my $rename = my $verbose = my $seasondoubledigit = my $removesymlinks = my $needshowexist = my $flattennonepisodefiles = "FALSE";
my $logfile = 0;
@@ -133,6 +133,10 @@
                        next FILE;
                }

+               if (check_filesize($file) eq "NEXT") {
+                       next FILE;
+               }
+
                if(-l $file) {
                        if($removesymlinks eq "TRUE") {
                                out("std", "DELETE: Removing symlink: $file\n");
@@ -308,6 +312,8 @@
                        $lookupseasonep = $1;
                } elsif($arg =~ /^--verbose:(.*)/ || $arg =~ /^-v:(.*)/) {
                        $verbose = $1;
+               } elsif($arg =~ /^--filesize-range:(.*-.*)/ || $arg =~ /^-fsrange:(.*-.*)/) {
+                       push @sizerange, $1;
                } elsif($arg =~ /^--no-network/ || $arg =~ /^-no-net/) {
                        $xbmcwebserver = "";
                        $tvdbrename = $fetchimages = $lookupseasonep = "FALSE";
@@ -429,6 +435,10 @@
        Uses shell-like simple pattern matches (eg *.avi)
        This argument can be repeated to add more rules

+--filesize-range:pattern
+       Only copy files which fall within these filesize ranges.
+       Examples for the pattern include 345MB-355MB or 1.05GB-1.15GB
+
--xbmc-web-server:host:port
        host:port for xbmc webserver, to automatically update library when new episodes arrive
        Remember to enable the webserver within xbmc, and "set the content" of your TV directory in xbmc.
@@ -759,6 +769,56 @@
        return "OK";
}

+sub check_filesize {
+       my ($file) = @_;
+       my $filesize = (-s $file) / 1024 / 1024;
+       my $check_flag = 'FALSE';
+
+
+       # Needed to support recursively filled directories
+       if (-d $file) {
+               return "OK";
+       }
+
+       # Loop through the size ranges passed in via the config file
+       foreach my $size (@sizerange) {
+               if ($size =~ /(.*)-(.*)/) {
+                       # Extract the min & max values, can mix and match postfixes
+                       my $minfilesize = $1;
+                       my $maxfilesize = $2;
+                       $minfilesize =~ s/MB//;
+                       $maxfilesize =~ s/MB//;
+
+                       # Fix filesizes passed in to all MB
+                       if ($minfilesize =~ /(.*)GB/) {
+                               $minfilesize = $1 * 1024;
+                       }
+
+                       if ($maxfilesize =~ /(.*)GB/) {
+                               $maxfilesize = $1 * 1024;
+                       }
+
+                       # Check the actual filesize
+                       if (-f $file && ($minfilesize < $filesize && $filesize < $maxfilesize)) {
+                               return "OK";
+                       }
+
+                       # Use the flag to say we didn't find any match for the filesize
+                       $check_flag = 'TRUE';
+               }
+       }
+
+       if ($check_flag =~ "TRUE") {
+               # Skip the file as it didn't fall within a specified filesize range
+               my $filename = filename($file);
+               out("std", "SKIP: Doesn't fit the filesize requirements: $filename\n");
+               return "NEXT";
+       }
+
+       # Defaults to OK so the filesize options become OPTIONAL.
+       return "OK";
+}
+
sub num_found_in_list {
        my ($find, @list) = @_;
        foreach (@list) {

And for the config file:
Code:
# OPTIONAL - sort files which fall within these ranges. Filesizes use the MB and GB postfixes, such as
# 170MB-400MB or 1.0GB-1.2GB. If no postfix specified, MB is assumed.
filesize-range:170MB-400MB
filesize-range:1.05GB-1.15GB
filesize-range:697MB-700MB

Let me know what you think :-)

Regexp is certainly my weakness, I'm not sure that I can implement the multiple episode fix sufficiently. I'll certainly give it a bit of a think and analyze your code further. You may end up working it out long before I do!


- newphreak - 2011-02-20

I have the same "issue" as stated earlier in this thread, i guess a simple regex would be the fix for this.
Some shows comes with Pt.I Pt.II Pt.III Pt.V and so on, and some comes with .Part.04
.Part.02. tagging instead of the usual S01E01. Could we include these?


Runnable exe - spyvingen - 2011-02-21

Hi i would love for this app to be an exe that has built in timer and a gui for settings is that totally out of the question?

One more thing that would be awsom is to intergrate scru function in it.

Then this app would be the only program u need for torrent exept the torrent program itself.

Sorry for my bad english Smile

//jonas


- fergulator - 2011-02-23

cliffe Wrote:Thanks. I restructured it so that if you disable the tvdb features it should work for you, to avoid the mysterious problem you have with the tvdb api module.

If you get this error message:
Code:
DBM: Deep : Cannot sysopen file '/.tvdb' : Permission denied
Disable the tvdb features in sorttv.conf:
Code:
fetch-images:FALSE
rename-format:[SHOW_NAME] - [EP1]
I.e. Turn off image downloading and set the rename to not include the episode title. Alternatively, turn off renaming by setting:
Code:
fetch-images:FALSE
rename-episodes:FALSE

Hey Cliffe - Thanks. I love sorttv. Exactly what I was looking for. So, I was getting the same mysterious TVDB API error. Found out that it related to the source folder name I was using (was 'unsorted media') on Windows. When calling the pl, I surrounded the path with quotes; it executed fine, but I got the TVDB error. Changing the directory names to non-spaced fixed it for me.


- carmenm - 2011-02-23

If i am correct looking at this script description, it does the same thing as therenamer on windows?
If so it s something we really missed on windows!

Any chance of a movie version? or even both at the same time....

Thanks


move if extracted, otherwise copy - sh0stak - 2011-02-24

I have tried searching this thread with no luck. I just wanted to see if anyone has setup a way to sort by MOVE if you extracted a file, otherwise sort by COPY.

Some of my torrents come as archives, some just a plain file. Either way I want to continue seeding them. If it is a plain file, sort by copy works great. If it is an archive, it leaves the extracted file in the original location so now I have the seeding archive, the extracted file in the "sort-from" dir, and the extracted file in the "sort-to" dir.

I will look into making this change on my own when I have some time. I thought in the meantime I would check and see if this has already been covered though.

thanks


brand new - migeira - 2011-02-26

everyone:

bear with me, i'm working on my first htpc xbmc build and all this stuff is new to me. i tried running the script using the ASpearl instead of strawberry perl and i got a ton of errors.

i am just trying to figure this out, if someone could help me out with this general process i'd really appreciate it?

migeira


- Justcop - 2011-02-28

Obviously this is a long thread and I haven't had chance to read through all of it but I just thought I'd add an idea.

Certainly the way I'd like to use it most of the fiels that aren't TV shows would be movies.

Could there be a way that it checks themoviedb to see if there is a title match and then moves it into a different folder for movies.

That way after the files have been processed everything that is TV goes to one place, movies to another. What is left can be moved to the unsorted folder but hopefully this won't be much at all and can be managed manually.

Two quick questions on how the script works that may have been answered somewhere above.

1. How does the script cope with episodes that are still in the process of being downloaded but appear correctly named?

2. How would the script function if I set it to run very often so that a second copy may start running before the first has finished?


- cliffe - 2011-02-28

oracle_sod Wrote:Just a quick note, when following the install instructions, File::Glob throws an error on the current version of Strawberry Perl as its already included...

There is a bug in the code, folder names are not checked correctly, when i label the move to folder S:\[TV Shows] for example, the script fails

I found that if set a whitelist to .avi the script still doesn't look inside folders

Under Windows 7 when not run as administrator, DRM::Deep fails to open .tvdb.db with an error "permission denied"

im not good enough at coding to fix the issue, but you should be able to reproduce it fairly easily

otherwise, wicked script :)

thanks

Thanks for the feedback. I believe the whitelist issue was fixed in the last release. I thought the directory naming issue had been fixed but we should look into that again.

Does the DRM::Deep message go away when you run the script as administrator? I have never experienced the error message, but it could also be related to the directory names, as fergulator recently mentioned.

Cheers,

Cliffe.


- cliffe - 2011-02-28

newphreak Wrote:I have the same "issue" as stated earlier in this thread, i guess a simple regex would be the fix for this.
Some shows comes with Pt.I Pt.II Pt.III Pt.V and so on, and some comes with .Part.04
.Part.02. tagging instead of the usual S01E01. Could we include these?

Some episode titles also include those phrases, also roman numerals complicates things. Could you give some filename examples?


- cliffe - 2011-02-28

spyvingen Wrote:Hi i would love for this app to be an exe that has built in timer and a gui for settings is that totally out of the question?

One more thing that would be awsom is to intergrate scru function in it.

Then this app would be the only program u need for torrent exept the torrent program itself.

Sorry for my bad english Smile

//jonas

I don't mind incorporating GUI features to make the tool easier to use. A GUI probably wouldn't need a timer, it could use the scheduling tools in Linux and Windows. It's just a matter of finding the time.

I doubt the GUI would be an .exe, but it could be an argument to the script, and a .cmd/.sh could be included for easy access to the GUI.

Anyway, it could just be a matter of including specially formatted lines in the config file that could tell the GUI about the arguments to ask the user for.

Please step forward if this sounds like a fun challenge to code.


- cliffe - 2011-02-28

fergulator Wrote:Hey Cliffe - Thanks. I love sorttv. Exactly what I was looking for. So, I was getting the same mysterious TVDB API error. Found out that it related to the source folder name I was using (was 'unsorted media') on Windows. When calling the pl, I surrounded the path with quotes; it executed fine, but I got the TVDB error. Changing the directory names to non-spaced fixed it for me.

Interesting, what happens if you run the script as administrator? (Since that was mentioned as also being a fix to the problem.) Thanks for sharing. It would be helpful if you could figure out which line in the script was throwing the error.


- cliffe - 2011-02-28

carmenm Wrote:If i am correct looking at this script description, it does the same thing as therenamer on windows?
If so it s something we really missed on windows!

Any chance of a movie version? or even both at the same time....

Thanks

SortTV has lots more features, and is designed to do automatic sorting. That program is mostly for renaming files without moving them (which SortTV can also do).

Yes, I would like to include movie sorting (although I won't actually use that feature). In a previous post I mentioned that a Perl module is available for querying themoviedb, but that for some reason it wasn't available from Strawberry Perl's CPAN. If someone looks into this we can add the feature.


- cliffe - 2011-02-28

sh0stak Wrote:I have tried searching this thread with no luck. I just wanted to see if anyone has setup a way to sort by MOVE if you extracted a file, otherwise sort by COPY.

Some of my torrents come as archives, some just a plain file. Either way I want to continue seeding them. If it is a plain file, sort by copy works great. If it is an archive, it leaves the extracted file in the original location so now I have the seeding archive, the extracted file in the "sort-from" dir, and the extracted file in the "sort-to" dir.

I will look into making this change on my own when I have some time. I thought in the meantime I would check and see if this has already been covered though.

thanks

Not currently. The feature to extract was added recently and just extracts into the same directory so that the files are then sorted along with everything else. The easiest solution would probably be to create a new subdirectory for the extraction, then make sure its empty after the sort. Please feel free to implement that.


- cliffe - 2011-02-28

Justcop Wrote:Obviously this is a long thread and I haven't had chance to read through all of it but I just thought I'd add an idea.

Certainly the way I'd like to use it most of the fiels that aren't TV shows would be movies.

Could there be a way that it checks themoviedb to see if there is a title match and then moves it into a different folder for movies.

That way after the files have been processed everything that is TV goes to one place, movies to another. What is left can be moved to the unsorted folder but hopefully this won't be much at all and can be managed manually.

Two quick questions on how the script works that may have been answered somewhere above.

1. How does the script cope with episodes that are still in the process of being downloaded but appear correctly named?

2. How would the script function if I set it to run very often so that a second copy may start running before the first has finished?

Yes, as mentioned previously movie sorting will hopefully eventually be added to the script. One way to make that happen is implement it Smile

1. That could cause problems. In your downloading program set a different directory for completed downloads, and use that as your directory to sort.

2. Good question. Probably in most cases it would be ok, although maybe not always. With the default settings if the file is being moved, then a second attempt will skip because the destination file will exist. However, if compressed files are being extracted they could be moved before extraction finishes.