failed_processor.py 3.77 KB
Newer Older
1
# Author: echel0n <[email protected]>
echel0n's avatar
echel0n committed
2
# URL: https://sickrage.ca
3
#
echel0n's avatar
echel0n committed
4
# This file is part of SickRage.
5
#
echel0n's avatar
echel0n committed
6
# SickRage is free software: you can redistribute it and/or modify
7
8
9
10
# 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.
#
echel0n's avatar
echel0n committed
11
# SickRage is distributed in the hope that it will be useful,
12
13
14
15
16
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
17
# along with SickRage.  If not, see <http://www.gnu.org/licenses/>.
18

19
from __future__ import print_function, unicode_literals
20

21
import sickrage
echel0n's avatar
echel0n committed
22
from sickrage.core.common import Quality, WANTED, DOWNLOADED, SNATCHED, SNATCHED_PROPER, SNATCHED_BEST
23
24
25
from sickrage.core.exceptions import FailedPostProcessingFailedException
from sickrage.core.helpers import show_names
from sickrage.core.nameparser import InvalidNameException, InvalidShowException, \
26
    NameParser
27
from sickrage.core.queues.search import FailedQueueItem
28
29
30
31
32
33
34


class FailedProcessor(object):
    """Take appropriate action when a download fails to complete"""

    def __init__(self, dirName, nzbName):
        """
35
36
        :param dirName: Full path to the folder of the failed download
        :param nzbName: Full name of the nzb file that failed
37
38
39
40
41
42
43
        """
        self.dir_name = dirName
        self.nzb_name = nzbName

        self.log = ""

    def process(self):
44
45
46
47
48
        """
        Do the actual work

        :return: True
        """
49
        self._log("Failed download detected: (" + str(self.nzb_name) + ", " + str(self.dir_name) + ")")
50

51
52
        release_name = show_names.determineReleaseName(self.dir_name, self.nzb_name)
        if release_name is None:
53
            self._log("Warning: unable to find a valid release name.", sickrage.app.log.WARNING)
54
            raise FailedPostProcessingFailedException()
55
56

        try:
57
            parsed = NameParser(False).parse(release_name)
58
        except InvalidNameException:
59
            self._log("Error: release name is invalid: " + release_name, sickrage.app.log.DEBUG)
60
            raise FailedPostProcessingFailedException()
61
        except InvalidShowException:
62
63
64
65
66
67
            self._log("Error: unable to parse release name " + release_name + " into a valid show",
                      sickrage.app.log.DEBUG)
            raise FailedPostProcessingFailedException()

        if parsed.show.paused:
            self._log("Warning: skipping failed processing for {} because the show is paused".format(release_name),
68
                      sickrage.app.log.DEBUG)
69
            raise FailedPostProcessingFailedException()
70

71
72
73
74
75
76
77
        sickrage.app.log.debug("name_parser info: ")
        sickrage.app.log.debug(" - " + str(parsed.series_name))
        sickrage.app.log.debug(" - " + str(parsed.season_number))
        sickrage.app.log.debug(" - " + str(parsed.episode_numbers))
        sickrage.app.log.debug(" - " + str(parsed.extra_info))
        sickrage.app.log.debug(" - " + str(parsed.release_group))
        sickrage.app.log.debug(" - " + str(parsed.air_date))
echel0n's avatar
echel0n committed
78

79
        for episode in parsed.episode_numbers:
80
            segment = parsed.show.getEpisode(parsed.season_number, episode)
echel0n's avatar
echel0n committed
81
82

            curStatus, curQuality = Quality.splitCompositeStatus(segment.status)
echel0n's avatar
echel0n committed
83
            if curStatus not in {SNATCHED, SNATCHED_BEST, SNATCHED_PROPER}:
84
85
86
                continue

            sickrage.app.search_queue.put(FailedQueueItem(parsed.show, [segment]))
87
88
89

        return True

90
    def _log(self, message, level=None):
91
        """Log to regular logfile and save for return for PP script log"""
92
        sickrage.app.log.log(level or sickrage.app.log.INFO, message)
93
        self.log += message + "\n"