I have been migrating my sabnzbd functionality to nzbget. That's been going very well but I have run into a few hitches with video sorting for dated episodes of tv shows.
I have two separate requirements for dated tv episodes. In one case, I need the show name, broadcast date, and episode title (which is usually the guest list), e.g. The.Colbert.Report.2013.08.13-Rep. John Lewis.mkv. The reason is that I have an application for binge watching The Daily Show with Jon Stewart paired with The Colbert Report. It uses the the broadcast date and guest list to display a list of shows to watch. This format also works well for plain directory file listings.
In the second case, I need the show name, season number, episode number, and episode title, e.g Real.Time.With.Bill.Maher.S11E25-Jay-Z, Barney Frank, Larry Miller, Josh Barro, Alexis Goldstein.mkv. This is a more typical need. Most video jukeboxes need the season and episode of a show to retrieve information and artwork.
My plan was to finish off video sorting for dated tv shows by calling on Filebot. Filebot has the ability to use tv databases in thetvdb.com and tvrage.com to lookup tv shows to provide the season, episode, and episode title. While I can do this manually with simple drag and drop, my preference is to have it automatically performed with a post process script.
Here is where I ran into the first hitch: After VideoSort runs, there is no way for any following pp-scripts to know the new name for a file. Nzbget does its post processing by using environment variables to pass values to a script. However, a parent's environment variable can not be changed by a child (nzbget being the parent and VideoSort being a child). Every pp-script gets its own copy of the environment. There is no means for informing the parent or any further scripts of changes.
I created a method to convey changes back to the parent and any sibling pp-scripts. This works by the pp-script writing the changed environment variables to a text file. When the script exits, the parents and siblings can retrieve the values from the text file.
To make that as pain free as possible, I wrote a Python script called childEnVars.py. Because it works closely with VideoSort, I put it into the lib folder of VideoSort.
childEnVars provides three functions for pp-scripts to use: Put, Get, and Clear. Pp-scripts call Put to save a value; Get to retrieve a value; and Clear to start with a clean slate.
Code: Select all
childEnVars.Clear()
childEnVars.Put('NZBPP_DIRECTORY', os.path.split(path)[0])
childEnVars.Put('NZBPP_NZBFILENAME', os.path.split(path)[1] + '.nzb')
finaldirectory = childEnVars.Get('NZBPP_DIRECTORY')
originalname = childEnVars.Get('NZBPP_NZBFILENAME')
A file "childenvars" is created in the same directory as the log is written to ensure that reading and writing rights are permitted. The pp-scripts need only put/get values without consideration for how the values are cached.
I modified VideoSort to use childEnVars by adding the following lines:
Code: Select all
import childEnVars
...
# We need a means to let the parent and interested
# sibling processes know what the new names are.
#
# The following will not work because chained
# scripts are not run within the same environment
# os.environ['NZBPP_DIRECTORY'] = os.path.split(path)[0])
# os.environ['NZBPP_NZBFILENAME'] = os.path.split(path)[1] + '.nzb')
# os.environ['NZBPP_NZBNAME'] = os.path.split(path)[1])
# Instead we'll stow the renamed file names into a file
# where the parent and sibling processes can retrieve them.
childEnVars.Clear()
childEnVars.Put('NZBPP_DIRECTORY', os.path.split(path)[0])
childEnVars.Put('NZBPP_NZBFILENAME', os.path.split(path)[1] + '.nzb')
childEnVars.Put('NZBPP_NZBNAME', os.path.split(path)[1])
I created a pp-script "FinishDatedEpisode.py" which calls childEnVars.Get() to get the final file name after VideoSort is done. It then finishes the renaming to my requirements.
Another hitch I ran into is that VideoSort ignores specified destination directories for categories. It provides its own MoviesDir, SeriesDir, and DatedDir. But when an nzb's category is known, my preference would be to use the destination directory for the category.
I modified VideoSort to offer a new option: UseCatDestDir. If turned on, VideoSort will put the video files into the destination directory for the category if the category is known. If not turned on or the category is not known, then MoviesDir, SeriesDir, and DatedDir are used as before.
To support using the category destination, I added the following:
Code: Select all
def set_dest_dir(default_dest_dir):
# If it is optioned and we can do it, use the category
# destination directory
if use_cat_dest_dir:
# See if the category is available, and then if there is
# a category destination specified for it
category = os.environ.get('NZBPP_CATEGORY', None)
if category is not None:
regex = re.compile(r"NZBOP_CATEGORY(\d+)_NAME")
for k, v in os.environ.items():
r = regex.search(k)
if r is not None:
if category == v:
category_dest_dir_key = "NZBOP_CATEGORY%s_DESTDIR" % r.groups()[0]
category_dest_dir = os.environ.get(category_dest_dir_key, None)
if category_dest_dir is not None and len(category_dest_dir) > 0:
# Check if there are substitution macros
if category_dest_dir.startswith("${MainDir)"):
ddir = os.environ.get("NZBOP_MAINDIR", "")
category_dest_dir = category_dest_dir.replace("${MainDir)", ddir)
elif category_dest_dir.startswith("${DestDir)"):
ddir = os.environ.get("NZBOP_DESTDIR", "")
category_dest_dir = category_dest_dir.replace("${DestDir)", ddir)
if verbose:
print "[INFO] category_dest_dir = \"%s\"" % category_dest_dir
return category_dest_dir
return default_dest_dir
I changed these lines:
Code: Select all
dest_dir = dated_dir
dest_dir = movies_dir
dest_dir = series_dir
to
Code: Select all
dest_dir = set_dest_dir(dated_dir)
dest_dir = set_dest_dir(movies_dir)
dest_dir = set_dest_dir(series_dir)
In working on these issues, I created a pp-script "Tattle.py". This is just a utility that logs the environment variables that nzbget is using. I found this to be very helpful.
I think these are useful modifications that others may benefit from. I have attached the scripts.