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)



- CoinTos - 2010-10-28 06:14

Cliffe,

Took some time after work to remove my Capitalization 'bug' then add a more platform independant solution and fix my $#*! lookup issue. Meet version 1.14rc, due to me working on windows for this and so I can test it there, Eclipse formated the file differently with my new code so a patch file was all but impossible. I mostly likely made it worst when I tried to fix the formating but anyhoo...

New features (if accepted):
> --show-title-rename - uses tvdb lookup for proper title format. (replaces Text::Capitalize)
> added tvdb id support for shows that the ep lookup gets confused on, now you can use the tvdb id # for the sub name. ie. $#*! My Dad Says ep lookup now works with tvdb id.

Once again, sorry about the lack of patch file, tomorrow when I am rested again I can copy additions into a basic editor that won't mess with it's formating so I can make a useable patch file.

Here is my release candidate if you wish to look at it and implement.
Sorttv 1.14rc


- cliffe - 2010-10-28 09:15

CoinTos Wrote:Cliffe,

Took some time after work to remove my Capitalization 'bug' then add a more platform independant solution and fix my $#*! lookup issue. Meet version 1.14rc, due to me working on windows for this and so I can test it there, Eclipse formated the file differently with my new code so a patch file was all but impossible. I mostly likely made it worst when I tried to fix the formating but anyhoo...

New features (if accepted):
> --show-title-rename - uses tvdb lookup for proper title format. (replaces Text::Capitalize)
> added tvdb id support for shows that the ep lookup gets confused on, now you can use the tvdb id # for the sub name. ie. $#*! My Dad Says ep lookup now works with tvdb id.

Once again, sorry about the lack of patch file, tomorrow when I am rested again I can copy additions into a basic editor that won't mess with it's formating so I can make a useable patch file.

Here is my release candidate if you wish to look at it and implement.
Sorttv 1.14rc

First of all thanks for taking the time to do this. It is fantastic to get the code contribution. I look forward to incorporating these features into SortTV.

A few constructive comments: Please stick to the code style and layout used. I realise that spacing is a matter of preference, but as with contributing to any open source projects, it is polite to use the established style, or discuss changes. I agree with reformatting the eval lines (and maybe some other changes), but when submitting patches please try to stick with the current coding style. It also makes it hard to review feature changes, when you have made lots of unrelated changes.

Example of style (no need for blank lines in if statements these are the product of the forum):
Code:
if(condition1) {
[INDENT]statement;[/INDENT]
} elsif(condition2) {
[INDENT]statement;[/INDENT]
} else {
[INDENT]statement;
# one line if statements like the following could be turned into multi-line ones if the condition gets too long.
statement if condition3;[/INDENT]}

I can review changes to style separately, but please send your patch without style changes.

Update: I notice you mention your editor made some changes, so perhaps these are all the result of your text editor Smile

Regarding the actual feature changes:

Please call the argument "--fetch-show-title" or something that can not imply that it forces a file rename.

Your version included this strange output:
Code:
DOWNLOAD: downloading images for CSI: Crime Scene Investigation
/Season 1ing season directory: /home/cliffe/Files/Media/TV/CSI- Crime Scene Investigation
DOWNLOAD: downloading season image for CSI: Crime Scene Investigation
Season 1 Episode 4.episode title for CSI: Crime Scene Investigation
- S01E04.avi- Crime Scene Investigationds/Unsorted/CSI s1e4.avi to /home/cliffe/Files/Media/TV/CSI- Crime Scene Investigation
Season 1 Episode 5.episode title for CSI: Crime Scene Investigation
- S01E05.avi- Crime Scene Investigationds/Unsorted/csi s1e5.avi to /home/cliffe/Files/Media/TV/CSI- Crime Scene Investigation

I look forward to the patch. Please create the patch using:
Code:
diff -u /path/to/sorttv.pl /path/to/your-new-version-of-sorttv.pl > sorttv.patch
You can use directories if you have made changes to all the files.
(or you can use git and pull the latest SortTV version from sourceforge)
Please only include feature changes. (i.e. don't include your own config file settings changes)

As you are probably aware, the command dos2unix can convert the Windows formatted file into a Linux formatted file.

Anyway, thanks again, I look forward to receiving a patch.


- CoinTos - 2010-10-28 14:23

Quote:Update: I notice you mention your editor made some changes, so perhaps these are all the result of your text editor

That exactly what happened and I again apologize for it, I will submit a proper patch today as I am now not running off of few hours of sleep and not seeing the matrix everywhere I look. :-)

Quote:Please call the argument "--fetch-show-title" or something that can not imply that it forces a file rename.
Agreed, I blame lack of sleep.

Quote:Your version included this strange output:
Oops, will be looked at and corrected.

Update
--------

Here is the patch as it stands now. It contains the code changes for --fetch-show-name and the function for using tvdb id for sub name. I fixed Eclipse so that it will never (ever ever :-)) format the original code auto-magicly. I could not reproduce that log output you posted so I will sum it up as the auto formating of Eclipse which is now fixed.

Patch File


- cliffe - 2010-10-28 19:27

CoinTos Wrote:That exactly what happened and I again apologize for it, I will submit a proper patch today as I am now not running off of few hours of sleep and not seeing the matrix everywhere I look. :-)]
Haha. Ok Smile


CoinTos Wrote:Agreed, I blame lack of sleep.


Oops, will be looked at and corrected.

Update
--------

Here is the patch as it stands now. It contains the code changes for --fetch-show-name and the function for using tvdb id for sub name. I fixed Eclipse so that it will never (ever ever :-)) format the original code auto-magicly. I could not reproduce that log output you posted so I will sum it up as the auto formating of Eclipse which is now fixed.

Patch File

Thanks for the patch. As is fairly typical for open source collaboration, I have reviewed your code and made some comments below. Please make and test the changes and send me another patch, then I will test and if all is well add to the next release. Cheers. Smile

Code:
--- /home/greg/SortTV1.13/sorttv/sorttv.pl    2010-10-27 00:05:18.000000000 -0400
+++ /home/deluge/sorttv/sorttv.pl    2010-10-28 09:02:10.000000000 -0400
@@ -33,7 +33,6 @@
use File::Basename;
use TVDB::API;
use File::Find;
-use Text::Capitalize;
use FileHandle;
use warnings;
use strict;
@@ -48,6 +47,7 @@
my $renameformat = "[SHOW_NAME] - [EP1][EP_NAME1]";
my $treatdir = "RECURSIVELY_SORT_CONTENTS";
my $fetchimages = "NEW_SHOWS";
+my $tvdbrename   = "FALSE";
lets start it off as TRUE (add to the line of TRUE variables)
Code:
my $imagesformat = "POSTER";
my @showrenames;
my $scriptpath = dirname(rel2abs($0));
@@ -175,7 +175,9 @@
            push @showrenames, $1;
        } elsif($arg =~ /^--log-file:(.*)/ || $arg =~ /^-o:(.*)/) {
            $logfile = $1;
-        } elsif($arg =~ /^--rename-episodes:(.*)/ || $arg =~ /^-rn:(.*)/) {
+        } elsif ( $arg =~ /^--fetch-show-title:(.*)/ || $arg =~ /^-fst:(.*)/ ) {
+            $tvdbrename = $1;
+        } elsif($arg =~ /^--rename-episodes:(.*)/ || $arg =~ /^-rn:(.*)/) {
Tabs rather than spaces please
Code:
            $rename = $1;
        } elsif($arg =~ /^--lookup-language:(.*)/ || $arg =~ /^-lang:(.*)/) {
            $tvdblanguage = $1;
@@ -306,6 +308,11 @@
    Secondary config file, overwrites settings loaded so far
    If not specified, only the default config file is loaded (sorttv.conf)

+--fetch-show-title:[TRUE|FALSE]
change description to: "Fetch show titles from thetvdb.com (for proper formatting)"
You mean "with or without".
Code:
+    Rename show title to the name stored in thetvdb.com lookup format when moving, can be used with out
+    or without rename-episodes
+    If not specified, FALSE
+
--rename-episodes:[TRUE|FALSE]
    Rename episodes to "show name S01E01.ext" format when moving
    If not specified, FALSE
@@ -464,11 +471,15 @@

# substitutes show names as configured
sub substitute_name {
-    my ($from) = @_;
Looks like you only use this second arg once. Instead, just add a check in here (see my next comment)
Code:
+    my ($from, $filter) = @_;
    foreach my $substitute (@showrenames) {
        if($substitute =~ /(.*)-->(.*)/) {
            my $subsrc = $1, my $subdest = $2;
            if($from =~ /^\Q$subsrc\E$/i) {
maybe: if($subdest =~ /^[+]?\d+$/ && $tvdbrename eq "TRUE")
Code:
+                if ( $subdest =~ /^[+]?\d+$/ && !defined($filter) )
+                {
+                    $subdest = fetchshowdetails($subdest);
+                }
                return $subdest;
            }
        }
@@ -553,7 +564,7 @@
    }
    if($needshowexist ne "TRUE") {
        # if we are here then we couldn't find a matching show, make DIR
-        my $newshowdir = $tvdir .escape_myfilename(substitute_name(capitalize_title(remdot($pureshowname), PRESERVE_ALLCAPS => 1)));
+        my $newshowdir = $tvdir .escape_myfilename(substitute_name(tvdb_title(remdot($pureshowname))));
        out("std", "INFO: making show directory: $newshowdir\n");
        if(mkdir($newshowdir, 0777)) {
            fetchshowimages(substitute_name(remdot($pureshowname)), $newshowdir) if $fetchimages ne "FALSE";
@@ -610,6 +621,38 @@
    }
}
I know I haven't commented all my subroutines (sorry), but please write a one or two line comment describing how new subroutines work.

I am inclined to not include this sub since it looks like it pretty much does a different one line thing for each request... Why not just put these one liners where this is being called from?
Code:
+sub fetchshowdetails {
+    my ( $request, $show, $series, $episode ) = @_;
+    my ( $return, $foundname );
+    $request = ""       if !defined($request);
+    $show    = $request if !defined($show);
+    if ( $request eq "EPISODE" ) {
+        #Get Episode Name in string using either tvdb id or search string
+        $return = $tvdb->getEpisodeName( $show, $series, $episode );
+    }elsif ( $request eq "SERIESFROMNAME" ) {
+        #Get Series info in hashref
+        $return = $tvdb->getSeries($show);
+    }elsif ( $request eq "SERIESALL" ) {
+        #Get the entire show in hashref
+        $return = $tvdb->getSeriesAll($show);
+    }else {
if this sub was staying I would set $request here too, to be consistent
Code:
+        $return = tvdb_title($show);
+    }
+    return $return;
+}
+
+sub tvdb_title {
please rename @before to something more descriptive
Code:
+    my @before = @_;
+    my $return;
+    if ( $tvdbrename eq "TRUE" || $before[0] =~ /^[+]?\d+$/ ) {
+        $return = fetchshowdetails( "SERIESFROMNAME", $before[0] );
+        if ( defined($return) ) {
+            return $return->{'SeriesName'};
+        }
+    }
+    return $before[0];
+}
+
sub move_an_ep {
    my($file, $season, $show, $series, $episode) = @_;
    my $newfilename = filename($file);
@@ -623,7 +666,7 @@
        }
        if($renameformat =~ /\[EP_NAME(\d)]/i) {
            out("verbose", "INFO: Fetching episode title for ", substitute_name(remdot($pureshowname)), " Season $series Episode $episode.\n");
-            my $name = $tvdb->getEpisodeName(substitute_name(remdot($pureshowname)), $series, $episode);
please remove the 1 (see comment above about removing the argument)
Code:
+            my $name = fetchshowdetails("EPISODE", substitute_name(remdot($pureshowname), 1), $series, $episode);
            if($name) {
                $eptitle = " - $name" if $1 == 1;
                $eptitle = ".$name" if $1 == 2;
@@ -631,7 +674,7 @@
                out("warn", "WARN: Could not get episode title for ", substitute_name(remdot($pureshowname)), " Season $series Episode $episode.\n");
            }
        }
-        my $sname = substitute_name(capitalize_title(remdot($pureshowname), PRESERVE_ALLCAPS => 1));
+        my $sname = substitute_name(tvdb_title(remdot($pureshowname)));
        my $ep1 = sprintf("S%02dE%02d", $series, $episode);
        my $ep2 = sprintf("%dx%d", $series, $episode);
        my $ep3 = sprintf("%dx%02d", $series, $episode);
@@ -733,7 +776,7 @@
    }
    if($needshowexist ne "TRUE") {
        # if we are here then we couldn't find a matching show, make DIR
-        my $newshowdir = $tvdir .escape_myfilename(substitute_name(capitalize_title(remdot($pureshowname), PRESERVE_ALLCAPS => 1)));
+        my $newshowdir = $tvdir .escape_myfilename(substitute_name(tvdb_title(remdot($pureshowname))));
        out("std", "INFO: making show directory: $newshowdir\n");
        if(mkdir($newshowdir, 0777)) {
            fetchshowimages(substitute_name(remdot($pureshowname)), $newshowdir) if $fetchimages ne "FALSE";

Also please check the spacing for the lines you have changed. They are often formatted differently. Check the spaces between () and also {}


- CoinTos - 2010-10-28 22:21

Quote:As is fairly typical for open source collaboration, I have reviewed your code and made some comments below.
I sure hope you do cause I am as proficient in programming as I am with flying without having wings. :-)

Quote:Tabs rather than spaces please
D'oh though I got all them.

Quote:Looks like you only use this second arg once. Instead, just add a check in here (see my next comment)
You caught me I cheated, I kept running into a problem with it suppling the name not the ID on episode lookup.

Quote:I am inclined to not include this sub since it looks like it pretty much does a different one line thing for each request... Why not just put these one liners where this is being called from?
It's soon to be gone, I had this crazy idea for the look up with only Show Title and Ep Title known to make one big tvdb sub but it didn't work out like I hoped.

Quote:Also please check the spacing for the lines you have changed. They are often formatted differently. Check the spaces between () and also {}

I'll work on that.

Update
--------
Cliffe,

This more of a take a look up date I did what you said above but I am still stuck on that used one variable. I cheated with it cause I wanted the actual name sub'ed for everything but episode lookup where I want the tvdb id to be used. I also include a sub that will take a show title and episode title and find the season number and episode number, sadly, I can't figure out how make it work in the reg detection of sort_directory.

Anyways, here the patch so you can take a look if you like.
patch


- Malexx - 2010-10-29 00:44

having a problem again and dont know if i make a mistake or if this just isnt possible.

Ok,in my Download Folder i have a file called"ABC-two.and.a.half.men.s01e01.avi"

im my config i added"show-name-substitute:ABC-two.and.a.half.men-->Two and a Half Men"

what i get is this here
Sorting D:\Download\Series/ into Z:\/
INFO: trying to move ABC-two.and.a.half.men season 1 episode 1
INFO: found a matching show:
Z:\/Two and a Half Men
INFO: found a matching season:
Z:\/Two and a Half Men/Season 1
INFO: Fetching episode title for ABC-two.and.a.half.men Season 1 Episode 1.
WARN: Could not get episode title for ABC-two.and.a.half.men Season Episode 1.

The Problem is when i download my Shows they wont be named like Two and a half Men S01e01 etc ...
And if i have to touch the files before using the SortTV Script it would make it more or less useless.

My Question is,am i doing something wrong here,or is the script just not mentioned for task like this?
THX for any help

greets ...


- CoinTos - 2010-10-29 01:16

Hi Malexx,

I can't test this to confirm at the moment but remove the periods.
ABC-two and a half men-->Two and a Half Men

That should sort it for you.


- Malexx - 2010-10-29 01:49

edit


- cliffe - 2010-10-29 12:04

CoinTos Wrote:Cliffe,

This more of a take a look up date I did what you said above but I am still stuck on that used one variable. I cheated with it cause I wanted the actual name sub'ed for everything but episode lookup where I want the tvdb id to be used. I also include a sub that will take a show title and episode title and find the season number and episode number, sadly, I can't figure out how make it work in the reg detection of sort_directory.

Anyways, here the patch so you can take a look if you like.
patch

Maybe just include a separate config option for
Code:
lookup-using-tvdb-id:name->TVDBID

To look for files matching "Show - Episode Title.avi':

Just before
Code:
} elsif(defined $nonepisodedir) {
Put this
Code:
        } elsif(filename($file) =~ /(.*)-(.*)(?:\..*)/) {
            $pureshowname = $1;
            $showname = fixtitle($pureshowname);
            my $episodetitle = fixtitle($2);
            $series = "";
            $episode = "";

            # remove these lines
            print "the extracted possible show name is: $showname\n";
            print "the extracted possible episode title is: $episodetitle\n";

            # here is where you should set $series and $episode if they are found.
            # when doing the comparison with $episodetitle use fixtitle on the tvdb episode title.
            
            if($series ne "" && $episode ne "") {
                if($seasondoubledigit eq "TRUE") {
                    $series = sprintf("%02d", $2);
                }
                if(move_episode($pureshowname, $showname, $series, $episode, $file) eq $REDO_FILE) {
                    redo FILE;
                }
            }

Maybe include a conf option for detecting turning this feature on and add a check that the feature is on in the above elsif condition.


- Targettio - 2010-10-29 19:51

I am now using it for the first time in anger (previously I was just trying to make it move one file for testing purposes).

When I have more than 1 file in the file that needs sorted (unsorted), the script will move the first one but then just freeze. I have to manually close the cmd screen and re-run the batch file.

I am guessing a little bit, but I assume there is something in the loop which is messing upConfused

[Image: frozensorttv.png]
(there were 2 files to sort, this screen will never go away unless I clear it)