1
0
mirror of https://github.com/moparisthebest/SickRage synced 2024-12-12 11:02:21 -05:00

"Borrowed" code from SB's post-processing routine.

This commit is contained in:
echel0n 2014-07-19 16:08:50 -07:00
parent 1b84c6d281
commit 24cd235988

View File

@ -11,7 +11,7 @@
# SickRage is distributed in the hope that it will be useful, # SickRage is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of # but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details. # GNU General Public License for more details.
# #
# You should have received a copy of the GNU General Public License # You should have received a copy of the GNU General Public License
# along with SickRage. If not, see <http://www.gnu.org/licenses/>. # along with SickRage. If not, see <http://www.gnu.org/licenses/>.
@ -44,6 +44,7 @@ from sickbeard.name_parser.parser import NameParser, InvalidNameException, Inval
from lib import adba from lib import adba
class PostProcessor(object): class PostProcessor(object):
""" """
A class which will process a media file according to the post processing settings in the config. A class which will process a media file according to the post processing settings in the config.
@ -56,10 +57,6 @@ class PostProcessor(object):
IGNORED_FILESTRINGS = ["/.AppleDouble/", ".DS_Store"] IGNORED_FILESTRINGS = ["/.AppleDouble/", ".DS_Store"]
NZB_NAME = 1
FOLDER_NAME = 2
FILE_NAME = 3
def __init__(self, file_path, nzb_name=None, process_method=None, is_priority=None): def __init__(self, file_path, nzb_name=None, process_method=None, is_priority=None):
""" """
Creates a new post processor with the given file path and optionally an NZB name. Creates a new post processor with the given file path and optionally an NZB name.
@ -85,19 +82,17 @@ class PostProcessor(object):
self.process_method = process_method if process_method else sickbeard.PROCESS_METHOD self.process_method = process_method if process_method else sickbeard.PROCESS_METHOD
self.in_history = False self.in_history = False
self.release_group = None self.release_group = None
self.release_name = None
self.is_proper = False self.is_proper = False
self.is_priority = is_priority self.is_priority = is_priority
self.good_results = {self.NZB_NAME: False,
self.FOLDER_NAME: False,
self.FILE_NAME: False}
self.log = '' self.log = ''
def __del__(self):
pass
def _log(self, message, level=logger.MESSAGE): def _log(self, message, level=logger.MESSAGE):
""" """
A wrapper for the internal logger which also keeps track of messages and saves them to a string for later. A wrapper for the internal logger which also keeps track of messages and saves them to a string for later.
@ -183,7 +178,7 @@ class PostProcessor(object):
if subtitles_only and not associated_file_path[len(associated_file_path) - 3:] in common.subtitleExtensions: if subtitles_only and not associated_file_path[len(associated_file_path) - 3:] in common.subtitleExtensions:
continue continue
#Exclude .rar files from associated list # Exclude .rar files from associated list
if re.search('(^.+\.(rar|r\d+)$)', associated_file_path): if re.search('(^.+\.(rar|r\d+)$)', associated_file_path):
continue continue
@ -216,7 +211,7 @@ class PostProcessor(object):
for cur_file in file_list: for cur_file in file_list:
if ek.ek(os.path.isfile, cur_file): if ek.ek(os.path.isfile, cur_file):
self._log(u"Deleting file " + cur_file, logger.DEBUG) self._log(u"Deleting file " + cur_file, logger.DEBUG)
#check first the read-only attribute # check first the read-only attribute
file_attribute = ek.ek(os.stat, cur_file)[0] file_attribute = ek.ek(os.stat, cur_file)[0]
if (not file_attribute & stat.S_IWRITE): if (not file_attribute & stat.S_IWRITE):
# File is read-only, so make it writeable # File is read-only, so make it writeable
@ -439,28 +434,24 @@ class PostProcessor(object):
# remember whether it's a proper # remember whether it's a proper
if parse_result.extra_info: if parse_result.extra_info:
self.is_proper = re.search('(^|[\. _-])(proper|repack)([\. _-]|$)', parse_result.extra_info,re.I) != None self.is_proper = re.search('(^|[\. _-])(proper|repack)([\. _-]|$)', parse_result.extra_info, re.I) != None
# if the result is complete then remember that for later # if the result is complete then remember that for later
if parse_result.series_name and parse_result.season_number != None and parse_result.episode_numbers and parse_result.release_group: # if the result is complete then set release name
test_name = helpers.remove_extension(ek.ek(os.path.basename, parse_result.original_name)) if parse_result.series_name and ((parse_result.season_number is not None and parse_result.episode_numbers)
or parse_result.air_date) and parse_result.release_group:
if not self.release_name:
self.release_name = helpers.remove_extension(ek.ek(os.path.basename, parse_result.original_name))
if test_name == self.nzb_name:
self.good_results[self.NZB_NAME] = True
elif test_name == self.folder_name:
self.good_results[self.FOLDER_NAME] = True
elif test_name == self.file_name:
self.good_results[self.FILE_NAME] = True
else:
logger.log(u"Nothing was good, found " + repr(test_name) + " and wanted either " + repr(
self.nzb_name) + ", " + repr(self.folder_name) + ", or " + repr(self.file_name))
else: else:
logger.log(u"Parse result not sufficient(all following have to be set). Will not save release name", logger.log(u"Parse result not sufficient (all following have to be set). will not save release name",
logger.DEBUG) logger.DEBUG)
logger.log("Parse result(series_name): " + str(parse_result.series_name), logger.DEBUG) logger.log(u"Parse result(series_name): " + str(parse_result.series_name), logger.DEBUG)
logger.log("Parse result(season_number): " + str(parse_result.season_number), logger.DEBUG) logger.log(u"Parse result(season_number): " + str(parse_result.season_number), logger.DEBUG)
logger.log("Parse result(episode_numbers): " + str(parse_result.episode_numbers), logger.DEBUG) logger.log(u"Parse result(episode_numbers): " + str(parse_result.episode_numbers), logger.DEBUG)
logger.log("Parse result(release_group): " + str(parse_result.release_group), logger.DEBUG) logger.log(u" or Parse result(air_date): " + str(parse_result.air_date), logger.DEBUG)
logger.log(u"Parse result(release_group): " + str(parse_result.release_group), logger.DEBUG)
def _analyze_name(self, name, file=True): def _analyze_name(self, name, file=True):
""" """
@ -520,7 +511,7 @@ class PostProcessor(object):
else: else:
self.anidbEpisode = ep self.anidbEpisode = ep
#TODO: clean code. it looks like it's from hell # TODO: clean code. it looks like it's from hell
for name in ep.allNames: for name in ep.allNames:
indexer_id = name_cache.retrieveNameFromCache(name) indexer_id = name_cache.retrieveNameFromCache(name)
@ -621,12 +612,14 @@ class PostProcessor(object):
# for air-by-date shows we need to look up the season/episode from database # for air-by-date shows we need to look up the season/episode from database
if season == -1 and show and episodes: if season == -1 and show and episodes:
self._log(u"Looks like this is an air-by-date or sports show, attempting to convert the date to season/episode", self._log(
logger.DEBUG) u"Looks like this is an air-by-date or sports show, attempting to convert the date to season/episode",
logger.DEBUG)
airdate = episodes[0].toordinal() airdate = episodes[0].toordinal()
myDB = db.DBConnection() myDB = db.DBConnection()
sql_result = myDB.select("SELECT season, episode FROM tv_episodes WHERE showid = ? and indexer = ? and airdate = ?", sql_result = myDB.select(
[show.indexerid, show.indexer, airdate]) "SELECT season, episode FROM tv_episodes WHERE showid = ? and indexer = ? and airdate = ?",
[show.indexerid, show.indexer, airdate])
if sql_result: if sql_result:
season = int(sql_result[0][0]) season = int(sql_result[0][0])
@ -652,7 +645,7 @@ class PostProcessor(object):
if show and season and episodes: if show and season and episodes:
return (show, season, episodes, quality) return (show, season, episodes, quality)
return (show, season, episodes, quality) return (show, season, episodes, quality)
def _get_ep_obj(self, show, season, episodes): def _get_ep_obj(self, show, season, episodes):
@ -701,7 +694,7 @@ class PostProcessor(object):
# if there is a quality available in the status then we don't need to bother guessing from the filename # if there is a quality available in the status then we don't need to bother guessing from the filename
if ep_obj.status in common.Quality.SNATCHED + common.Quality.SNATCHED_PROPER + common.Quality.SNATCHED_BEST: if ep_obj.status in common.Quality.SNATCHED + common.Quality.SNATCHED_PROPER + common.Quality.SNATCHED_BEST:
oldStatus, ep_quality = common.Quality.splitCompositeStatus(ep_obj.status) #@UnusedVariable oldStatus, ep_quality = common.Quality.splitCompositeStatus(ep_obj.status) # @UnusedVariable
if ep_quality != common.Quality.UNKNOWN: if ep_quality != common.Quality.UNKNOWN:
self._log( self._log(
u"The old status had a quality in it, using that: " + common.Quality.qualityStrings[ep_quality], u"The old status had a quality in it, using that: " + common.Quality.qualityStrings[ep_quality],
@ -733,7 +726,9 @@ class PostProcessor(object):
if ep_obj.status in common.Quality.SNATCHED + common.Quality.SNATCHED_PROPER: if ep_obj.status in common.Quality.SNATCHED + common.Quality.SNATCHED_PROPER:
oldStatus, ep_quality = common.Quality.splitCompositeStatus(ep_obj.status) # @UnusedVariable oldStatus, ep_quality = common.Quality.splitCompositeStatus(ep_obj.status) # @UnusedVariable
if ep_quality != common.Quality.UNKNOWN: if ep_quality != common.Quality.UNKNOWN:
self._log(u"The old status had a quality in it, using that: " + common.Quality.qualityStrings[ep_quality], logger.DEBUG) self._log(
u"The old status had a quality in it, using that: " + common.Quality.qualityStrings[ep_quality],
logger.DEBUG)
return ep_quality return ep_quality
# Try guessing quality from the file name # Try guessing quality from the file name
@ -853,7 +848,8 @@ class PostProcessor(object):
# get the quality of the episode we're processing # get the quality of the episode we're processing
if quality: if quality:
self._log(u"Snatch history had a quality in it, using that: " + common.Quality.qualityStrings[quality], logger.DEBUG) self._log(u"Snatch history had a quality in it, using that: " + common.Quality.qualityStrings[quality],
logger.DEBUG)
new_ep_quality = quality new_ep_quality = quality
else: else:
new_ep_quality = self._get_quality(ep_obj) new_ep_quality = self._get_quality(ep_obj)
@ -873,7 +869,9 @@ class PostProcessor(object):
# if there's an existing file that we don't want to replace stop here # if there's an existing file that we don't want to replace stop here
if existing_file_status == PostProcessor.EXISTS_LARGER: if existing_file_status == PostProcessor.EXISTS_LARGER:
if self.is_proper: if self.is_proper:
self._log(u"File exists and new file is smaller, new file is a proper/repack, marking it safe to replace", logger.DEBUG) self._log(
u"File exists and new file is smaller, new file is a proper/repack, marking it safe to replace",
logger.DEBUG)
return True return True
else: else:
@ -891,7 +889,7 @@ class PostProcessor(object):
logger.DEBUG) logger.DEBUG)
# set the status of the episodes # set the status of the episodes
#for curEp in [ep_obj] + ep_obj.relatedEps: # for curEp in [ep_obj] + ep_obj.relatedEps:
# curEp.status = common.Quality.compositeStatus(common.SNATCHED, new_ep_quality) # curEp.status = common.Quality.compositeStatus(common.SNATCHED, new_ep_quality)
# delete the existing file (and company) # delete the existing file (and company)
@ -922,21 +920,12 @@ class PostProcessor(object):
sql_l = [] sql_l = []
for cur_ep in [ep_obj] + ep_obj.relatedEps: for cur_ep in [ep_obj] + ep_obj.relatedEps:
with cur_ep.lock: with cur_ep.lock:
cur_release_name = None
# use the best possible representation of the release name if self.release_name:
if self.good_results[self.NZB_NAME]: self._log("Found release name " + self.release_name, logger.DEBUG)
cur_release_name = self.nzb_name cur_ep.release_name = self.release_name
elif self.good_results[self.FOLDER_NAME]:
cur_release_name = self.folder_name
elif self.good_results[self.FILE_NAME]:
cur_release_name = self.file_name
if cur_release_name:
self._log("Found release name " + cur_release_name, logger.DEBUG)
cur_ep.release_name = cur_release_name
else: else:
logger.log("good results: " + repr(self.good_results), logger.DEBUG) cur_ep.release_name = ""
if ep_obj.status in common.Quality.SNATCHED_BEST: if ep_obj.status in common.Quality.SNATCHED_BEST:
cur_ep.status = common.Quality.compositeStatus(common.ARCHIVED, new_ep_quality) cur_ep.status = common.Quality.compositeStatus(common.ARCHIVED, new_ep_quality)