Beispiel #1
0
def main(inputDirectory, inputName, inputCategory, inputHash, inputID):

    status = int(1)  # 1 = failed | 0 = success
    root = int(0)
    video = int(0)
    video2 = int(0)
    foundFile = int(0)
    extracted_folder = []
    extractionSuccess = False

    Logger.debug("MAIN: Received Directory: %s | Name: %s | Category: %s", inputDirectory, inputName, inputCategory)

    inputDirectory, inputName, inputCategory, root = category_search(
        inputDirectory, inputName, inputCategory, root, categories
    )  # Confirm the category by parsing directory structure

    for category in categories:
        if category == inputCategory:
            if inputCategory == hpCategory:
                outputDestination = inputDirectory  # HP needs to scan the same dir as passed to downloader.
                break
            if os.path.basename(inputDirectory) == inputName:
                Logger.info("MAIN: Download is a directory")
                outputDestination = os.path.normpath(os.path.join(outputDirectory, category, safeName(inputName)))
            else:
                Logger.info("MAIN: Download is not a directory")
                outputDestination = os.path.normpath(
                    os.path.join(outputDirectory, category, os.path.splitext(safeName(inputName))[0])
                )
                Logger.info("MAIN: Output directory set to: %s", outputDestination)
            break
        else:
            continue

    Logger.debug("MAIN: Scanning files in directory: %s", inputDirectory)

    now = datetime.datetime.now()
    for dirpath, dirnames, filenames in os.walk(inputDirectory):
        for file in filenames:

            filePath = os.path.join(dirpath, file)
            fileName, fileExtension = os.path.splitext(file)
            targetDirectory = os.path.join(outputDestination, file)

            if root == 1:
                if not foundFile:
                    Logger.debug("MAIN: Looking for %s in: %s", inputName, file)
                if (
                    (safeName(inputName) in safeName(file))
                    or (safeName(os.path.splitext(file)[0]) in safeName(inputName))
                    and foundFile == 0
                ):
                    pass  # This file does match the Torrent name
                    foundFile = 1
                    Logger.debug("MAIN: Found file %s that matches Torrent Name %s", file, inputName)
                else:
                    continue  # This file does not match the Torrent name, skip it

            if root == 2:
                Logger.debug("MAIN: Looking for files with modified/created dates less than 5 minutes old.")
                mtime_lapse = now - datetime.datetime.fromtimestamp(os.path.getmtime(os.path.join(dirpath, file)))
                ctime_lapse = now - datetime.datetime.fromtimestamp(os.path.getctime(os.path.join(dirpath, file)))
                if (
                    (mtime_lapse < datetime.timedelta(minutes=5))
                    or (ctime_lapse < datetime.timedelta(minutes=5))
                    and foundFile == 0
                ):
                    pass  # This file does match the date time criteria
                    foundFile = 1
                    Logger.debug("MAIN: Found file %s with date modifed/created less than 5 minutes ago.", file)
                else:
                    continue  # This file has not been recently moved or created, skip it

            if not (
                inputCategory == cpsCategory or inputCategory == sbCategory or inputCategory == hpCategory
            ):  # process all for non-video categories.
                Logger.info("MAIN: Found file %s for category %s", filePath, inputCategory)
                copy_link(filePath, targetDirectory, useLink, outputDestination)
            elif fileExtension in mediaContainer and not inputCategory == hpCategory:  # If the file is a video file
                if is_sample(filePath, inputName, minSampleSize):  # Ignore samples
                    Logger.info("MAIN: Ignoring sample file: %s  ", filePath)
                    continue
                else:
                    video = video + 1
                    Logger.info("MAIN: Found video file %s in %s", fileExtension, filePath)
                    try:
                        copy_link(filePath, targetDirectory, useLink, outputDestination)
                    except:
                        Logger.exception("MAIN: Failed to link file: %s", file)
            elif fileExtension in metaContainer and not inputCategory == hpCategory:
                Logger.info("MAIN: Found metadata file %s for file %s", fileExtension, filePath)
                try:
                    copy_link(filePath, targetDirectory, useLink, outputDestination)
                except:
                    Logger.exception("MAIN: Failed to link file: %s", file)
            elif fileExtension in compressedContainer:
                # find part numbers in second "extension" from right, if we have more than 1 compressed file in the same directory.
                if (
                    re.search(r"\d+", os.path.splitext(fileName)[1])
                    and os.path.dirname(filePath) in extracted_folder
                    and not (os.path.splitext(fileName)[1] in [".720p", ".1080p"])
                ):
                    part = int(re.search(r"\d+", os.path.splitext(fileName)[1]).group())
                    if part == 1:  # we only want to extract the primary part.
                        Logger.debug("MAIN: Found primary part of a multi-part archive %s. Extracting", file)
                    else:
                        Logger.debug("MAIN: Found part %s of a multi-part archive %s. Ignoring", part, file)
                        continue
                Logger.info("MAIN: Found compressed archive %s for file %s", fileExtension, filePath)
                try:
                    extractor.extract(filePath, outputDestination)
                    extractionSuccess = (
                        True
                    )  # we use this variable to determine if we need to pause a torrent or not in uTorrent (don't need to pause archived content)
                    extracted_folder.append(os.path.dirname(filePath))
                except:
                    Logger.exception("MAIN: Extraction failed for: %s", file)
            elif inputCategory == hpCategory:
                continue
            else:
                Logger.debug("MAIN: Ignoring unknown filetype %s for file %s", fileExtension, filePath)
                continue
    flatten(outputDestination)

    # Now check if movie files exist in destination:
    for dirpath, dirnames, filenames in os.walk(outputDestination):
        for file in filenames:
            filePath = os.path.join(dirpath, file)
            fileExtension = os.path.splitext(file)[1]
            if fileExtension in mediaContainer:  # If the file is a video file
                if is_sample(filePath, inputName, minSampleSize):
                    Logger.debug("MAIN: Removing sample file: %s", filePath)
                    os.unlink(filePath)  # remove samples
                else:
                    video2 = video2 + 1
    if video2 >= video and video2 > 0:  # Check that all video files were moved
        status = 0

    # Hardlink solution for uTorrent, need to implent support for deluge, transmission
    if clientAgent in ["utorrent", "transmission"] and extractionSuccess == False and inputHash:
        if clientAgent == "utorrent":
            try:
                Logger.debug("MAIN: Connecting to %s: %s", clientAgent, uTorrentWEBui)
                utorrentClass = UTorrentClient(uTorrentWEBui, uTorrentUSR, uTorrentPWD)
            except:
                Logger.exception("MAIN: Failed to connect to uTorrent")
        else:
            try:
                Logger.debug("MAIN: Connecting to %s: http://%s:%s", clientAgent, TransmissionHost, TransmissionPort)
                TransmissionClass = TransmissionClient(
                    TransmissionHost, TransmissionPort, TransmissionUSR, TransmissionPWD
                )
            except:
                Logger.exception("MAIN: Failed to connect to Transmission")

        # if we are using links with uTorrent it means we need to pause it in order to access the files
        if useLink != "no":
            Logger.debug("MAIN: Stoping torrent %s in %s while processing", inputName, clientAgent)
            if clientAgent == "utorrent":
                utorrentClass.stop(inputHash)
            else:
                TransmissionClass.stop_torrent(inputID)
            time.sleep(5)  # Give uTorrent some time to catch up with the change

        # Delete torrent and torrentdata from uTorrent
        if deleteOriginal == 1:
            Logger.debug("MAIN: Deleting torrent %s from %s", inputName, clientAgent)
            if clientAgent == "utorrent":
                utorrentClass.removedata(inputHash)
                if not inputCategory == hpCategory:
                    utorrentClass.remove(inputHash)
            else:
                if inputCategory == hpCategory:
                    TransmissionClass.remove_torrent(inputID, False)
                else:
                    TransmissionClass.remove_torrent(inputID, True)
            time.sleep(5)

    processCategories = Set([cpsCategory, sbCategory, hpCategory, mlCategory, gzCategory])

    if inputCategory and not (inputCategory in processCategories):  # no extra processing to be done... yet.
        Logger.info("MAIN: No further processing to be done for category %s.", inputCategory)
        result = 1
    elif status == 0 or (
        inputCategory in [hpCategory, mlCategory, gzCategory]
    ):  # if movies linked/extracted or for other categories.
        Logger.debug("MAIN: Calling autoProcess script for successful download.")
        status = 0  # hp, my, gz don't support failed.
    else:
        Logger.error("MAIN: Something failed! Please check logs. Exiting")
        sys.exit(-1)

    if inputCategory == cpsCategory:
        Logger.info("MAIN: Calling CouchPotatoServer to post-process: %s", inputName)
        download_id = inputHash
        result = autoProcessMovie.process(outputDestination, inputName, status, clientAgent, download_id)
    elif inputCategory == sbCategory:
        Logger.info("MAIN: Calling Sick-Beard to post-process: %s", inputName)
        result = autoProcessTV.processEpisode(outputDestination, inputName, status)
    elif inputCategory == hpCategory:
        Logger.info("MAIN: Calling HeadPhones to post-process: %s", inputName)
        result = autoProcessMusic.process(outputDestination, inputName, status)
    elif inputCategory == mlCategory:
        Logger.info("MAIN: Calling Mylar to post-process: %s", inputName)
        result = autoProcessComics.processEpisode(outputDestination, inputName, status)
    elif inputCategory == gzCategory:
        Logger.info("MAIN: Calling Gamez to post-process: %s", inputName)
        result = autoProcessGames.process(outputDestination, inputName, status)

    if result == 1:
        Logger.info(
            "MAIN: A problem was reported in the autoProcess* script. If torrent was pasued we will resume seeding"
        )

    # Hardlink solution for uTorrent, need to implent support for deluge, transmission
    if (
        clientAgent in ["utorrent", "transmission"]
        and extractionSuccess == False
        and inputHash
        and useLink != "no"
        and deleteOriginal == 0
    ):  # we always want to resume seeding, for now manually find out what is wrong when extraction fails
        Logger.debug("MAIN: Starting torrent %s in %s", inputName, clientAgent)
        if clientAgent == "utorrent":
            utorrentClass.start(inputHash)
        else:
            TransmissionClass.start_torrent(inputID)

    Logger.info("MAIN: All done.")
Beispiel #2
0
    # 8 Failure URL
    Logger.info("MAIN: Script triggered from SABnzbd 0.7.17+")
    clientAgent = "sabnzbd"
    nzbDir, inputName, status, inputCategory, download_id = (sys.argv[1], sys.argv[2], sys.argv[7], sys.argv[5], '')
else: # only CPS supports this manual run for now.
    Logger.warn("MAIN: Invalid number of arguments received from client.")
    Logger.info("MAIN: Running autoProcessMovie as a manual run...")
    clientAgent = "manual"
    nzbDir, inputName, status, inputCategory, download_id = ('Manual Run', 'Manual Run', 0, cpsCategory[0], '')

if inputCategory in cpsCategory:
    Logger.info("MAIN: Calling CouchPotatoServer to post-process: %s", inputName)
    result = autoProcessMovie.process(nzbDir, inputName, status, clientAgent, download_id, inputCategory)
elif inputCategory in sbCategory:
    Logger.info("MAIN: Calling Sick-Beard to post-process: %s", inputName)
    result = autoProcessTV.processEpisode(nzbDir, inputName, status, clientAgent, inputCategory)
elif inputCategory in hpCategory:
    Logger.info("MAIN: Calling HeadPhones to post-process: %s", inputName)
    result = autoProcessMusic.process(nzbDir, inputName, status, inputCategory)
elif inputCategory in mlCategory:
    Logger.info("MAIN: Calling Mylar to post-process: %s", inputName)
    result = autoProcessComics.processEpisode(nzbDir, inputName, status, inputCategory)
elif inputCategory in gzCategory:
    Logger.info("MAIN: Calling Gamez to post-process: %s", inputName)
    result = autoProcessGames.process(nzbDir, inputName, status, inputCategory)
else:
    Logger.warning("MAIN: The download category %s does not match any category defined in autoProcessMedia.cfg. Exiting.", inputCategory)
    sys.exit(POSTPROCESS_ERROR)

if result == 0:
    Logger.info("MAIN: The autoProcess* script completed successfully.")
Beispiel #3
0
        if not status == 1:
            Logger.info(
                "Neither archive- nor par2-files found, _brokenlog.txt doesn't exist, considering download successful"
            )

    # Check if destination directory exists (important for reprocessing of history items)
    if not os.path.isdir(os.environ['NZBPP_DIRECTORY']):
        Logger.error(
            "Post-Process: Nothing to post-process: destination directory %s doesn't exist",
            os.environ['NZBPP_DIRECTORY'])
        status = 1

    # All checks done, now launching the script.
    Logger.info("Script triggered from NZBGet, starting autoProcessTV...")
    result = autoProcessTV.processEpisode(os.environ['NZBPP_DIRECTORY'],
                                          os.environ['NZBPP_NZBFILENAME'],
                                          status)
# SABnzbd
elif len(sys.argv) == SABNZB_NO_OF_ARGUMENTS:
    # SABnzbd argv:
    # 1 The final directory of the job (full path)
    # 2 The original name of the NZB file
    # 3 Clean version of the job name (no path info and ".nzb" removed)
    # 4 Indexer's report number (if supported)
    # 5 User-defined category
    # 6 Group that the NZB was posted in e.g. alt.binaries.x
    # 7 Status of post processing. 0 = OK, 1=failed verification, 2=failed unpack, 3=1+2
    Logger.info("Script triggered from SABnzbd, starting autoProcessTV...")
    result = autoProcessTV.processEpisode(sys.argv[1], sys.argv[2],
                                          sys.argv[7])
# NZBGet
Beispiel #4
0
        sys.argv[4],
        sys.argv[5],
    )

else:  # only CPS supports this manual run for now.
    Logger.warn("MAIN: Invalid number of arguments received from client.")
    Logger.info("MAIN: Running autoProcessMovie as a manual run...")
    clientAgent = "manual"
    nzbDir, inputName, status, inputCategory, download_id = ("Manual Run", "Manual Run", 0, cpsCategory, "")

if inputCategory == cpsCategory:
    Logger.info("MAIN: Calling CouchPotatoServer to post-process: %s", inputName)
    result = autoProcessMovie.process(nzbDir, inputName, status, clientAgent, download_id)
elif inputCategory == sbCategory:
    Logger.info("MAIN: Calling Sick-Beard to post-process: %s", inputName)
    result = autoProcessTV.processEpisode(nzbDir, inputName, status)
elif inputCategory == hpCategory:
    Logger.info("MAIN: Calling HeadPhones to post-process: %s", inputName)
    result = autoProcessMusic.process(nzbDir, inputName, status)
elif inputCategory == mlCategory:
    Logger.info("MAIN: Calling Mylar to post-process: %s", inputName)
    result = autoProcessComics.processEpisode(nzbDir, inputName, status)
elif inputCategory == gzCategory:
    Logger.info("MAIN: Calling Gamez to post-process: %s", inputName)
    result = autoProcessGames.process(nzbDir, inputName, status)

if result == 0:
    Logger.info("MAIN: The autoProcess* script completed successfully.")
    if os.environ.has_key("NZBOP_SCRIPTDIR"):  # return code for nzbget v11
        sys.exit(POSTPROCESS_SUCCESS)
else:
Beispiel #5
0
                    sys.exit(POSTPROCESS_NONE)

        if os.path.isfile(os.path.join(os.environ['NZBPP_DIRECTORY'], "_brokenlog.txt")):
            Logger.warning("Post-Process: _brokenlog.txt exists, download is probably damaged, exiting")
            sys.exit(POSTPROCESS_NONE)

        Logger.info("Neither archive- nor par2-files found, _brokenlog.txt doesn't exist, considering download successful")

    # Check if destination directory exists (important for reprocessing of history items)
    if not os.path.isdir(os.environ['NZBPP_DIRECTORY']):
        Logger.error("Post-Process: Nothing to post-process: destination directory %s doesn't exist", os.environ['NZBPP_DIRECTORY'])
        status = 1

    # All checks done, now launching the script.
    Logger.info("Script triggered from NZBGet, starting autoProcessTV...")
    result = autoProcessTV.processEpisode(os.environ['NZBPP_DIRECTORY'], os.environ['NZBPP_NZBNAME'], status)
# SABnzbd
elif len(sys.argv) == SABNZB_NO_OF_ARGUMENTS:
    # SABnzbd argv:
    # 1 The final directory of the job (full path)
    # 2 The original name of the NZB file
    # 3 Clean version of the job name (no path info and ".nzb" removed)
    # 4 Indexer's report number (if supported)
    # 5 User-defined category
    # 6 Group that the NZB was posted in e.g. alt.binaries.x
    # 7 Status of post processing. 0 = OK, 1=failed verification, 2=failed unpack, 3=1+2
    Logger.info("Script triggered from SABnzbd, starting autoProcessTV...")
    result = autoProcessTV.processEpisode(sys.argv[1], sys.argv[2], sys.argv[7])
# NZBGet
elif len(sys.argv) == NZBGET_NO_OF_ARGUMENTS:
    # NZBGet argv:
Beispiel #6
0
            Logger.info("MAIN: Please check your Par-check/repair settings for future downloads.")
            status = 1

        else:
            Logger.info("MAIN: Par-check/repair disabled or no .par2 files found, and Unpack not required. Health is ok so handle as though download successful")
            Logger.info("MAIN: Please check your Par-check/repair settings for future downloads.")

    # Check if destination directory exists (important for reprocessing of history items)
    if not os.path.isdir(os.environ['NZBPP_DIRECTORY']):
        Logger.error("MAIN: Nothing to post-process: destination directory %s doesn't exist. Setting status \"failed\"", os.environ['NZBPP_DIRECTORY'])
        status = 1

    # All checks done, now launching the script.
    Logger.info("MAIN: Script triggered from NZBGet, starting autoProcessTV...")
    clientAgent = "nzbget"
    result = autoProcessTV.processEpisode(os.environ['NZBPP_DIRECTORY'], os.environ['NZBPP_NZBFILENAME'], status, clientAgent, os.environ['NZBPP_CATEGORY'])
# SABnzbd Pre 0.7.17
elif len(sys.argv) == SABNZB_NO_OF_ARGUMENTS:
    # SABnzbd argv:
    # 1 The final directory of the job (full path)
    # 2 The original name of the NZB file
    # 3 Clean version of the job name (no path info and ".nzb" removed)
    # 4 Indexer's report number (if supported)
    # 5 User-defined category
    # 6 Group that the NZB was posted in e.g. alt.binaries.x
    # 7 Status of post processing. 0 = OK, 1=failed verification, 2=failed unpack, 3=1+2
    Logger.info("MAIN: Script triggered from SABnzbd, starting autoProcessTV...")
    clientAgent = "sabnzbd"
    result = autoProcessTV.processEpisode(sys.argv[1], sys.argv[2], sys.argv[7], clientAgent, sys.argv[5])
# SABnzbd 0.7.17+
elif len(sys.argv) >= SABNZB_0717_NO_OF_ARGUMENTS:
Beispiel #7
0
def main(inputDirectory, inputName, inputCategory, inputHash, inputID):

    status = int(1)  # 1 = failed | 0 = success
    root = int(0)
    video = int(0)
    video2 = int(0)
    foundFile = int(0)
    extracted_folder = []
    extractionSuccess = False
    copy_list = []

    Logger.debug("MAIN: Received Directory: %s | Name: %s | Category: %s",
                 inputDirectory, inputName, inputCategory)
    if inputCategory in sbCategory and sbFork in SICKBEARD_TORRENT:
        Logger.info("MAIN: Calling SickBeard's %s branch to post-process: %s",
                    sbFork, inputName)
        result = autoProcessTV.processEpisode(inputDirectory, inputName,
                                              int(0))
        if result == 1:
            Logger.info(
                "MAIN: A problem was reported in the autoProcess* script. If torrent was pasued we will resume seeding"
            )
        Logger.info("MAIN: All done.")
        sys.exit()

    inputDirectory, inputName, inputCategory, root = category_search(
        inputDirectory, inputName, inputCategory, root,
        categories)  # Confirm the category by parsing directory structure

    outputDestination = ""
    for category in categories:
        if category == inputCategory:
            if os.path.basename(inputDirectory) == inputName:
                Logger.info("MAIN: Download is a directory")
                outputDestination = os.path.normpath(
                    os.path.join(outputDirectory, category,
                                 safeName(inputName)))
            else:
                Logger.info("MAIN: Download is not a directory")
                outputDestination = os.path.normpath(
                    os.path.join(outputDirectory, category,
                                 os.path.splitext(safeName(inputName))[0]))
            Logger.info("MAIN: Output directory set to: %s", outputDestination)
            break
        else:
            continue
    if outputDestination == "":
        if inputCategory == "":
            inputCategory = "UNCAT"
        if os.path.basename(inputDirectory) == inputName:
            Logger.info("MAIN: Download is a directory")
            outputDestination = os.path.normpath(
                os.path.join(outputDirectory, inputCategory,
                             safeName(inputName)))
        else:
            Logger.info("MAIN: Download is not a directory")
            outputDestination = os.path.normpath(
                os.path.join(outputDirectory, inputCategory,
                             os.path.splitext(safeName(inputName))[0]))
        Logger.info("MAIN: Output directory set to: %s", outputDestination)

    processOnly = cpsCategory + sbCategory + hpCategory + mlCategory + gzCategory
    if not "NONE" in user_script_categories:  # if None, we only process the 5 listed.
        if "ALL" in user_script_categories:  # All defined categories
            processOnly = categories
        processOnly.extend(
            user_script_categories
        )  # Adds all categories to be processed by userscript.

    if not inputCategory in processOnly:
        Logger.info("MAIN: No processing to be done for category: %s. Exiting",
                    inputCategory)
        Logger.info("MAIN: All done.")
        sys.exit()

    # Hardlink solution for uTorrent, need to implent support for deluge, transmission
    if clientAgent in ['utorrent', 'transmission'] and inputHash:
        if clientAgent == 'utorrent':
            try:
                Logger.debug("MAIN: Connecting to %s: %s", clientAgent,
                             uTorrentWEBui)
                utorrentClass = UTorrentClient(uTorrentWEBui, uTorrentUSR,
                                               uTorrentPWD)
            except:
                Logger.exception("MAIN: Failed to connect to uTorrent")
                utorrentClass = ""
        if clientAgent == 'transmission':
            try:
                Logger.debug("MAIN: Connecting to %s: http://%s:%s",
                             clientAgent, TransmissionHost, TransmissionPort)
                TransmissionClass = TransmissionClient(TransmissionHost,
                                                       TransmissionPort,
                                                       TransmissionUSR,
                                                       TransmissionPWD)
            except:
                Logger.exception("MAIN: Failed to connect to Transmission")
                TransmissionClass = ""

        # if we are using links with uTorrent it means we need to pause it in order to access the files
        Logger.debug("MAIN: Stoping torrent %s in %s while processing",
                     inputName, clientAgent)
        if clientAgent == 'utorrent' and utorrentClass != "":
            utorrentClass.stop(inputHash)
        if clientAgent == 'transmission' and TransmissionClass != "":
            TransmissionClass.stop_torrent(inputID)
        time.sleep(
            5)  # Give Torrent client some time to catch up with the change

    Logger.debug("MAIN: Scanning files in directory: %s", inputDirectory)

    now = datetime.datetime.now()
    for dirpath, dirnames, filenames in os.walk(inputDirectory):
        for file in filenames:

            filePath = os.path.join(dirpath, file)
            fileName, fileExtension = os.path.splitext(file)
            targetDirectory = os.path.join(outputDestination, file)

            if root == 1:
                if foundFile == int(0):
                    Logger.debug("MAIN: Looking for %s in: %s", inputName,
                                 file)
                if (safeName(inputName)
                        in safeName(file)) or (safeName(fileName)
                                               in safeName(inputName)):
                    #pass  # This file does match the Torrent name
                    foundFile = 1
                    Logger.debug(
                        "MAIN: Found file %s that matches Torrent Name %s",
                        file, inputName)
                else:
                    continue  # This file does not match the Torrent name, skip it

            if root == 2:
                Logger.debug(
                    "MAIN: Looking for files with modified/created dates less than 5 minutes old."
                )
                mtime_lapse = now - datetime.datetime.fromtimestamp(
                    os.path.getmtime(os.path.join(dirpath, file)))
                ctime_lapse = now - datetime.datetime.fromtimestamp(
                    os.path.getctime(os.path.join(dirpath, file)))
                if (mtime_lapse < datetime.timedelta(minutes=5)) or (
                        ctime_lapse < datetime.timedelta(minutes=5)):
                    #pass  # This file does match the date time criteria
                    foundFile = 1
                    Logger.debug(
                        "MAIN: Found file %s with date modifed/created less than 5 minutes ago.",
                        file)
                else:
                    continue  # This file has not been recently moved or created, skip it

            if fileExtension in mediaContainer:  # If the file is a video file
                if is_sample(
                        filePath, inputName, minSampleSize
                ) and not inputCategory in hpCategory:  # Ignore samples
                    Logger.info("MAIN: Ignoring sample file: %s  ", filePath)
                    continue
                else:
                    video = video + 1
                    Logger.info("MAIN: Found video file %s in %s",
                                fileExtension, filePath)
                    try:
                        copy_link(filePath, targetDirectory, useLink,
                                  outputDestination)
                        copy_list.append(
                            [filePath,
                             os.path.join(outputDestination, file)])
                    except:
                        Logger.exception("MAIN: Failed to link file: %s", file)
            elif fileExtension in metaContainer:
                Logger.info("MAIN: Found metadata file %s for file %s",
                            fileExtension, filePath)
                try:
                    copy_link(filePath, targetDirectory, useLink,
                              outputDestination)
                    copy_list.append(
                        [filePath,
                         os.path.join(outputDestination, file)])
                except:
                    Logger.exception("MAIN: Failed to link file: %s", file)
                continue
            elif fileExtension in compressedContainer:
                if inputCategory in hpCategory:  # We need to link all files for HP in order to move these back to support seeding.
                    Logger.info(
                        "MAIN: Linking compressed archive file %s for file %s",
                        fileExtension, filePath)
                    try:
                        copy_link(filePath, targetDirectory, useLink,
                                  outputDestination)
                        copy_list.append(
                            [filePath,
                             os.path.join(outputDestination, file)])
                    except:
                        Logger.exception("MAIN: Failed to link file: %s", file)
                # find part numbers in second "extension" from right, if we have more than 1 compressed file in the same directory.
                if re.search(
                        r'\d+',
                        os.path.splitext(fileName)[1]
                ) and os.path.dirname(filePath) in extracted_folder and not (
                        os.path.splitext(fileName)[1] in ['.720p', '.1080p']):
                    part = int(
                        re.search(r'\d+',
                                  os.path.splitext(fileName)[1]).group())
                    if part == 1:  # we only want to extract the primary part.
                        Logger.debug(
                            "MAIN: Found primary part of a multi-part archive %s. Extracting",
                            file)
                    else:
                        Logger.debug(
                            "MAIN: Found part %s of a multi-part archive %s. Ignoring",
                            part, file)
                        continue
                Logger.info("MAIN: Found compressed archive %s for file %s",
                            fileExtension, filePath)
                try:
                    if inputCategory in hpCategory:  # HP needs to scan the same dir as passed to downloader.
                        extractor.extract(filePath, inputDirectory)
                    else:
                        extractor.extract(filePath, outputDestination)
                    extractionSuccess = True  # we use this variable to determine if we need to pause a torrent or not in uTorrent (don't need to pause archived content)
                    extracted_folder.append(os.path.dirname(filePath))
                except:
                    Logger.exception("MAIN: Extraction failed for: %s", file)
                continue
            elif not inputCategory in cpsCategory + sbCategory:  #process all for non-video categories.
                Logger.info("MAIN: Found file %s for category %s", filePath,
                            inputCategory)
                copy_link(filePath, targetDirectory, useLink,
                          outputDestination)
                copy_list.append(
                    [filePath, os.path.join(outputDestination, file)])
                continue
            else:
                Logger.debug("MAIN: Ignoring unknown filetype %s for file %s",
                             fileExtension, filePath)
                continue
    if not inputCategory in hpCategory:  #don't flatten hp in case multi cd albums, and we need to copy this back later.
        flatten(outputDestination)

    # Now check if movie files exist in destination:
    if inputCategory in cpsCategory + sbCategory:
        for dirpath, dirnames, filenames in os.walk(outputDestination):
            for file in filenames:
                filePath = os.path.join(dirpath, file)
                fileName, fileExtension = os.path.splitext(file)
                if fileExtension in mediaContainer:  # If the file is a video file
                    if is_sample(filePath, inputName, minSampleSize):
                        Logger.debug("MAIN: Removing sample file: %s",
                                     filePath)
                        os.unlink(filePath)  # remove samples
                    else:
                        Logger.debug("MAIN: Found media file: %s", filePath)
                        video2 = video2 + 1
                else:
                    Logger.debug("MAIN: File %s is not a media file", filePath)
        if video2 >= video and video2 > int(
                0):  # Check that all video files were moved
            Logger.debug("MAIN: Found %s media files", str(video2))
            status = int(0)
        else:
            Logger.debug(
                "MAIN: Found %s media files in output. %s were found in input",
                str(video2), str(video))

    processCategories = cpsCategory + sbCategory + hpCategory + mlCategory + gzCategory

    if (inputCategory in user_script_categories
            and not "NONE" in user_script_categories) or (
                "ALL" in user_script_categories
                and not inputCategory in processCategories):
        Logger.info("MAIN: Processing user script %s.", user_script)
        result = external_script(outputDestination)
    elif status == int(0) or (
            inputCategory in hpCategory + mlCategory +
            gzCategory):  # if movies linked/extracted or for other categories.
        Logger.debug(
            "MAIN: Calling autoProcess script for successful download.")
        status = int(0)  # hp, my, gz don't support failed.
    else:
        Logger.error("MAIN: Something failed! Please check logs. Exiting")
        sys.exit(-1)

    if inputCategory in cpsCategory:
        Logger.info("MAIN: Calling CouchPotatoServer to post-process: %s",
                    inputName)
        download_id = inputHash
        result = autoProcessMovie.process(outputDestination, inputName, status,
                                          clientAgent, download_id,
                                          inputCategory)
    elif inputCategory in sbCategory:
        Logger.info("MAIN: Calling Sick-Beard to post-process: %s", inputName)
        result = autoProcessTV.processEpisode(outputDestination, inputName,
                                              status, inputCategory)
    elif inputCategory in hpCategory:
        Logger.info("MAIN: Calling HeadPhones to post-process: %s", inputName)
        result = autoProcessMusic.process(inputDirectory, inputName, status,
                                          inputCategory)
    elif inputCategory in mlCategory:
        Logger.info("MAIN: Calling Mylar to post-process: %s", inputName)
        result = autoProcessComics.processEpisode(outputDestination, inputName,
                                                  status, inputCategory)
    elif inputCategory in gzCategory:
        Logger.info("MAIN: Calling Gamez to post-process: %s", inputName)
        result = autoProcessGames.process(outputDestination, inputName, status,
                                          inputCategory)

    if result == 1:
        Logger.info(
            "MAIN: A problem was reported in the autoProcess* script. If torrent was paused we will resume seeding"
        )

    if inputCategory in hpCategory:
        # we need to move the output dir files back...
        Logger.debug(
            "MAIN: Moving temporary HeadPhones files back to allow seeding.")
        for item in copy_list:
            if os.path.isfile(os.path.normpath(
                    item[1])):  # check to ensure temp files still exist.
                if os.path.isfile(os.path.normpath(
                        item[0])):  # both exist, remove temp version
                    Logger.debug(
                        "MAIN: File %s still present. Removing tempoary file %s",
                        str(item[0]), str(item[1]))
                    os.unlink(os.path.normpath(item[1]))
                    continue
                else:  # move temp version back to allow seeding or Torrent removal.
                    Logger.debug("MAIN: Moving %s to %s", str(item[1]),
                                 str(item[0]))
                    shutil.move(os.path.normpath(item[1]),
                                os.path.normpath(item[0]))
                    continue

    # Hardlink solution for uTorrent, need to implent support for deluge, transmission
    if clientAgent in ['utorrent', 'transmission'] and inputHash:
        # Delete torrent and torrentdata from Torrent client if processing was successful.
        if deleteOriginal == 1 and result != 1:
            Logger.debug("MAIN: Deleting torrent %s from %s", inputName,
                         clientAgent)
            if clientAgent == 'utorrent' and utorrentClass != "":
                utorrentClass.removedata(inputHash)
                if not inputCategory in hpCategory:
                    utorrentClass.remove(inputHash)
            if clientAgent == 'transmission' and TransmissionClass != "":
                if inputCategory in hpCategory:  #don't delete actual files for hp category, just remove torrent.
                    TransmissionClass.remove_torrent(inputID, False)
                else:
                    TransmissionClass.remove_torrent(inputID, True)
        # we always want to resume seeding, for now manually find out what is wrong when extraction fails
        else:
            Logger.debug("MAIN: Starting torrent %s in %s", inputName,
                         clientAgent)
            if clientAgent == 'utorrent' and utorrentClass != "":
                utorrentClass.start(inputHash)
            if clientAgent == 'transmission' and TransmissionClass != "":
                TransmissionClass.start_torrent(inputID)
        time.sleep(5)
    #cleanup
    if inputCategory in processCategories and result == 0 and os.path.isdir(
            outputDestination):
        num_files_new = int(0)
        file_list = []
        for dirpath, dirnames, filenames in os.walk(outputDestination):
            for file in filenames:
                filePath = os.path.join(dirpath, file)
                fileName, fileExtension = os.path.splitext(file)
                if fileExtension in mediaContainer or fileExtension in metaContainer:
                    num_files_new = num_files_new + 1
                    file_list.append(file)
        if num_files_new == int(0):
            Logger.info(
                "All files have been processed. Cleaning outputDirectory %s",
                outputDestination)
            shutil.rmtree(outputDestination)
        else:
            Logger.info(
                "outputDirectory %s still contains %s media and/or meta files. This directory will not be removed.",
                outputDestination, num_files_new)
            for item in file_list:
                Logger.debug("media/meta file found: %s", item)
    Logger.info("MAIN: All done.")
Beispiel #8
0
else:  # only CPS supports this manual run for now.
    Logger.warn("MAIN: Invalid number of arguments received from client.")
    Logger.info("MAIN: Running autoProcessMovie as a manual run...")
    clientAgent = "manual"
    nzbDir, inputName, status, inputCategory, download_id = ('Manual Run',
                                                             'Manual Run', 0,
                                                             cpsCategory, '')

if inputCategory == cpsCategory:
    Logger.info("MAIN: Calling CouchPotatoServer to post-process: %s",
                inputName)
    result = autoProcessMovie.process(nzbDir, inputName, status, clientAgent,
                                      download_id)
elif inputCategory == sbCategory:
    Logger.info("MAIN: Calling Sick-Beard to post-process: %s", inputName)
    result = autoProcessTV.processEpisode(nzbDir, inputName, status)
elif inputCategory == hpCategory:
    Logger.info("MAIN: Calling HeadPhones to post-process: %s", inputName)
    result = autoProcessMusic.process(nzbDir, inputName, status)
elif inputCategory == mlCategory:
    Logger.info("MAIN: Calling Mylar to post-process: %s", inputName)
    result = autoProcessComics.processEpisode(nzbDir, inputName, status)
elif inputCategory == gzCategory:
    Logger.info("MAIN: Calling Gamez to post-process: %s", inputName)
    result = autoProcessGames.process(nzbDir, inputName, status)
else:
    Logger.warning(
        "MAIN: The download category %s does not match any category defines in autoProcessMedia.cfg. Exiting.",
        inputCategory)
    sys.exit(POSTPROCESS_ERROR)
Beispiel #9
0
def main(inputDirectory, inputName, inputCategory, inputHash, inputID):

    status = int(1)  # 1 = failed | 0 = success
    root = int(0)
    video = int(0)
    video2 = int(0)
    foundFile = int(0)
    numCompressed = int(0)
    extractionSuccess = False

    Logger.debug("MAIN: Received Directory: %s | Name: %s | Category: %s", inputDirectory, inputName, inputCategory)

    inputDirectory, inputName, inputCategory, root = category_search(inputDirectory, inputName, inputCategory, root, categories)  # Confirm the category by parsing directory structure

    for category in categories:
        if category == inputCategory:
            outputDestination = os.path.normpath(os.path.join(outputDirectory, category, safeName(inputName)))
            Logger.info("MAIN: Output directory set to: %s", outputDestination)
            break
        else:
            continue

    Logger.debug("MAIN: Scanning files in directory: %s", inputDirectory)      

    now = datetime.datetime.now()
    for dirpath, dirnames, filenames in os.walk(inputDirectory):
        for file in filenames:

            filePath = os.path.join(dirpath, file)
            fileName, fileExtension = os.path.splitext(file)
            targetDirectory = os.path.join(outputDestination, file)

            if root == 1:
                if not foundFile: 
                    Logger.debug("MAIN: Looking for %s in: %s", inputName, file)
                if (safeName(inputName) in safeName(file)) or (safeName(os.path.splitext(file)[0]) in safeName(inputName)) and foundFile == 0:
                    pass  # This file does match the Torrent name
                    foundFile = 1
                    Logger.debug("MAIN: Found file %s that matches Torrent Name %s", file, inputName)
                else:
                    continue  # This file does not match the Torrent name, skip it

            if root == 2:
                Logger.debug("MAIN: Looking for files with modified/created dates less than 5 minutes old.")
                mtime_lapse = now - datetime.datetime.fromtimestamp(os.path.getmtime(os.path.join(dirpath, file)))
                ctime_lapse = now - datetime.datetime.fromtimestamp(os.path.getctime(os.path.join(dirpath, file)))
                if (mtime_lapse < datetime.timedelta(minutes=5)) or (ctime_lapse < datetime.timedelta(minutes=5)) and foundFile == 0:
                    pass  # This file does match the date time criteria
                    foundFile = 1
                    Logger.debug("MAIN: Found file %s with date modifed/created less than 5 minutes ago.", file)
                else:
                    continue  # This file has not been recently moved or created, skip it

            if not (inputCategory == cpsCategory or inputCategory == sbCategory): #process all for non-video categories.
                Logger.info("MAIN: Found file %s for category %s", filePath, inputCategory)
                copy_link(filePath, targetDirectory, useLink, outputDestination)
            elif fileExtension in mediaContainer:  # If the file is a video file
                if is_sample(filePath, inputName, minSampleSize):  # Ignore samples
                    Logger.info("MAIN: Ignoring sample file: %s  ", filePath)
                    continue
                else:
                    video = video + 1
                    Logger.info("MAIN: Found video file %s in %s", fileExtension, filePath)
                    try:
                        copy_link(filePath, targetDirectory, useLink, outputDestination)
                    except:
                        Logger.exception("MAIN: Failed to link file: %s", file)
            elif fileExtension in metaContainer:
                Logger.info("MAIN: Found metadata file %s for file %s", fileExtension, filePath)
                try:
                    copy_link(filePath, targetDirectory, useLink, outputDestination)
                except:
                    Logger.exception("MAIN: Failed to link file: %s", file)
            elif fileExtension in compressedContainer:
                numCompressed = numCompressed + 1
                if re.search(r'\d+', os.path.splitext(fileName)[1]) and numCompressed > 1: # find part numbers in second "extension" from right, if we have more than 1 compressed file.
                    part = int(re.search(r'\d+', os.path.splitext(fileName)[1]).group())
                    if part == 1: # we only want to extract the primary part.
                        Logger.debug("MAIN: Found primary part of a multi-part archive %s. Extracting", file)                       
                    else:
                        Logger.debug("MAIN: Found part %s of a multi-part archive %s. Ignoring", part, file)
                        continue
                Logger.info("MAIN: Found compressed archive %s for file %s", fileExtension, filePath)
                try:
                    extractor.extract(filePath, outputDestination)
                    extractionSuccess = True # we use this variable to determine if we need to pause a torrent or not in uTorrent (dont need to pause archived content)
                except:
                    Logger.exception("MAIN: Extraction failed for: %s", file)
            else:
                Logger.debug("MAIN: Ignoring unknown filetype %s for file %s", fileExtension, filePath)
                continue
    flatten(outputDestination)

    # Now check if movie files exist in destination:
    for dirpath, dirnames, filenames in os.walk(outputDestination):
        for file in filenames:
            filePath = os.path.join(dirpath, file)
            fileExtension = os.path.splitext(file)[1]
            if fileExtension in mediaContainer:  # If the file is a video file
                if is_sample(filePath, inputName, minSampleSize):
                    Logger.debug("MAIN: Removing sample file: %s", filePath)
                    os.unlink(filePath)  # remove samples
                else:
                    video2 = video2 + 1
    if video2 >= video and video2 > 0:  # Check that all video files were moved
        status = 0

    # Hardlink solution for uTorrent, need to implent support for deluge, transmission
    if clientAgent == 'utorrent' and extractionSuccess == False and inputHash:
        try:
            Logger.debug("MAIN: Connecting to uTorrent: %s", uTorrentWEBui)
            utorrentClass = UTorrentClient(uTorrentWEBui, uTorrentUSR, uTorrentPWD)
        except:
            Logger.exception("MAIN: Failed to connect to uTorrent")

        # if we are using links with uTorrent it means we need to pause it in order to access the files
        if useLink != "no":
            Logger.debug("MAIN: Stoping torrent %s in uTorrent while processing", inputName)
            utorrentClass.stop(inputHash)
            time.sleep(5)  # Give uTorrent some time to catch up with the change

        # Delete torrent and torrentdata from uTorrent
        if deleteOriginal == 1:
            Logger.debug("MAIN: Deleting torrent %s from uTorrent", inputName)
            utorrentClass.removedata(inputHash)
            utorrentClass.remove(inputHash)
            time.sleep(5)

    processCategories = Set([cpsCategory, sbCategory, hpCategory, mlCategory, gzCategory])

    if inputCategory and not (inputCategory in processCategories): # no extra processign to be done... yet.
        Logger.info("MAIN: No further processing to be done for category %s.", inputCategory)
        result = 1
    elif status == 0:
        Logger.debug("MAIN: Calling autoProcess script for successful download.")
    else:
        Logger.error("MAIN: Something failed! Please check logs. Exiting")
        sys.exit(-1)

    if inputCategory == cpsCategory:
        Logger.info("MAIN: Calling CouchPotatoServer to post-process: %s", inputName)
        if clientAgent == 'utorrent' and inputHash != '':
            download_id = 'uTorrent_' + inputHash
        elif clientAgent == 'transmission' and inputHash != '':
            download_id = 'Transmission_' + inputHash
        else:
            download_id = inputHash
        result = autoProcessMovie.process(outputDestination, inputName, status, clientAgent, download_id)
    elif inputCategory == sbCategory:
        Logger.info("MAIN: Calling Sick-Beard to post-process: %s", inputName)
        result = autoProcessTV.processEpisode(outputDestination, inputName, status)
    elif inputCategory == hpCategory:
        Logger.info("MAIN: Calling HeadPhones to post-process: %s", inputName)
        result = autoProcessMusic.process(outputDestination, inputName, status)
    elif inputCategory == mlCategory:
        Logger.info("MAIN: Calling Mylar to post-process: %s", inputName)
        result = autoProcessComics.processEpisode(outputDestination, inputName, status)
    elif inputCategory == gzCategory:
        Logger.info("MAIN: Calling Gamez to post-process: %s", inputName)
        result = autoProcessGames.process(outputDestination, inputName, status)

    if result == 1:
        Logger.info("MAIN: A problem was reported in the autoProcess* script. If torrent was pasued we will resume seeding")

    # Hardlink solution for uTorrent, need to implent support for deluge, transmission
    if clientAgent == 'utorrent' and extractionSuccess == False and inputHash and useLink != "no" and deleteOriginal == 0: # we always want to resume seeding, for now manually find out what is wrong when extraction fails
        Logger.debug("MAIN: Starting torrent %s in uTorrent", inputName)
        utorrentClass.start(inputHash)

    Logger.info("MAIN: All done.")
Beispiel #10
0
def main(inputDirectory, inputName, inputCategory, inputHash, inputID):

    status = int(1)  # 1 = failed | 0 = success
    root = int(0)
    video = int(0)
    video2 = int(0)
    foundFile = int(0)
    extracted_folder = []
    extractionSuccess = False
    copy_list = []

    Logger.debug("MAIN: Received Directory: %s | Name: %s | Category: %s", inputDirectory, inputName, inputCategory)
    if  inputCategory in sbCategory and sbFork in SICKBEARD_TORRENT:
        Logger.info("MAIN: Calling SickBeard's %s branch to post-process: %s",sbFork ,inputName)
        result = autoProcessTV.processEpisode(inputDirectory, inputName, int(0))
        if result == 1:
            Logger.info("MAIN: A problem was reported in the autoProcess* script. If torrent was pasued we will resume seeding")
        Logger.info("MAIN: All done.")
        sys.exit()

    inputDirectory, inputName, inputCategory, root = category_search(inputDirectory, inputName, inputCategory, root, categories)  # Confirm the category by parsing directory structure

    outputDestination = ""
    for category in categories:
        if category == inputCategory:
            if os.path.basename(inputDirectory) == inputName:
                Logger.info("MAIN: Download is a directory")
                outputDestination = os.path.normpath(os.path.join(outputDirectory, category, safeName(inputName)))
            else:
                Logger.info("MAIN: Download is not a directory")
                outputDestination = os.path.normpath(os.path.join(outputDirectory, category, os.path.splitext(safeName(inputName))[0]))
            Logger.info("MAIN: Output directory set to: %s", outputDestination)
            break
        else:
            continue
    if outputDestination == "":
        if inputCategory == "":
            inputCategory = "UNCAT" 
        if os.path.basename(inputDirectory) == inputName:
            Logger.info("MAIN: Download is a directory")
            outputDestination = os.path.normpath(os.path.join(outputDirectory, inputCategory, safeName(inputName)))
        else:
            Logger.info("MAIN: Download is not a directory")
            outputDestination = os.path.normpath(os.path.join(outputDirectory, inputCategory, os.path.splitext(safeName(inputName))[0]))
        Logger.info("MAIN: Output directory set to: %s", outputDestination)

    processOnly = cpsCategory + sbCategory + hpCategory + mlCategory + gzCategory
    if not "NONE" in user_script_categories: # if None, we only process the 5 listed.
        if "ALL" in user_script_categories: # All defined categories
            processOnly = categories
        processOnly.extend(user_script_categories) # Adds all categories to be processed by userscript.

    if not inputCategory in processOnly:
        Logger.info("MAIN: No processing to be done for category: %s. Exiting", inputCategory) 
        Logger.info("MAIN: All done.")
        sys.exit()

    # Hardlink solution for uTorrent, need to implent support for deluge, transmission
    if clientAgent in ['utorrent', 'transmission'] and inputHash:
        if clientAgent == 'utorrent':
            try:
                Logger.debug("MAIN: Connecting to %s: %s", clientAgent, uTorrentWEBui)
                utorrentClass = UTorrentClient(uTorrentWEBui, uTorrentUSR, uTorrentPWD)
            except:
                Logger.exception("MAIN: Failed to connect to uTorrent")
                utorrentClass = ""
        if clientAgent == 'transmission':
            try:
                Logger.debug("MAIN: Connecting to %s: http://%s:%s", clientAgent, TransmissionHost, TransmissionPort)
                TransmissionClass = TransmissionClient(TransmissionHost, TransmissionPort, TransmissionUSR, TransmissionPWD)
            except:
                Logger.exception("MAIN: Failed to connect to Transmission")
                TransmissionClass = ""

        # if we are using links with uTorrent it means we need to pause it in order to access the files
        Logger.debug("MAIN: Stoping torrent %s in %s while processing", inputName, clientAgent)
        if clientAgent == 'utorrent' and utorrentClass != "":            
            utorrentClass.stop(inputHash)
        if clientAgent == 'transmission' and TransmissionClass !="":
            TransmissionClass.stop_torrent(inputID)
        time.sleep(5)  # Give Torrent client some time to catch up with the change      

    Logger.debug("MAIN: Scanning files in directory: %s", inputDirectory)      

    now = datetime.datetime.now()
    for dirpath, dirnames, filenames in os.walk(inputDirectory):
        for file in filenames:

            filePath = os.path.join(dirpath, file)
            fileName, fileExtension = os.path.splitext(file)
            targetDirectory = os.path.join(outputDestination, file)

            if root == 1:
                if foundFile == int(0): 
                    Logger.debug("MAIN: Looking for %s in: %s", inputName, file)
                if (safeName(inputName) in safeName(file)) or (safeName(fileName) in safeName(inputName)):
                    #pass  # This file does match the Torrent name
                    foundFile = 1
                    Logger.debug("MAIN: Found file %s that matches Torrent Name %s", file, inputName)
                else:
                    continue  # This file does not match the Torrent name, skip it

            if root == 2:
                Logger.debug("MAIN: Looking for files with modified/created dates less than 5 minutes old.")
                mtime_lapse = now - datetime.datetime.fromtimestamp(os.path.getmtime(os.path.join(dirpath, file)))
                ctime_lapse = now - datetime.datetime.fromtimestamp(os.path.getctime(os.path.join(dirpath, file)))
                if (mtime_lapse < datetime.timedelta(minutes=5)) or (ctime_lapse < datetime.timedelta(minutes=5)):
                    #pass  # This file does match the date time criteria
                    foundFile = 1
                    Logger.debug("MAIN: Found file %s with date modifed/created less than 5 minutes ago.", file)
                else:
                    continue  # This file has not been recently moved or created, skip it

            if fileExtension in mediaContainer:  # If the file is a video file
                if is_sample(filePath, inputName, minSampleSize) and not inputCategory in hpCategory:  # Ignore samples
                    Logger.info("MAIN: Ignoring sample file: %s  ", filePath)
                    continue
                else:
                    video = video + 1
                    Logger.info("MAIN: Found video file %s in %s", fileExtension, filePath)
                    try:
                        copy_link(filePath, targetDirectory, useLink, outputDestination)
                        copy_list.append([filePath, os.path.join(outputDestination, file)])
                    except:
                        Logger.exception("MAIN: Failed to link file: %s", file)
            elif fileExtension in metaContainer:
                Logger.info("MAIN: Found metadata file %s for file %s", fileExtension, filePath)
                try:
                    copy_link(filePath, targetDirectory, useLink, outputDestination)
                    copy_list.append([filePath, os.path.join(outputDestination, file)])
                except:
                    Logger.exception("MAIN: Failed to link file: %s", file)
                continue
            elif fileExtension in compressedContainer:
                if inputCategory in hpCategory: # We need to link all files for HP in order to move these back to support seeding.
                    Logger.info("MAIN: Linking compressed archive file %s for file %s", fileExtension, filePath)
                    try:
                        copy_link(filePath, targetDirectory, useLink, outputDestination)
                        copy_list.append([filePath, os.path.join(outputDestination, file)])
                    except:
                        Logger.exception("MAIN: Failed to link file: %s", file)
                # find part numbers in second "extension" from right, if we have more than 1 compressed file in the same directory.
                if re.search(r'\d+', os.path.splitext(fileName)[1]) and os.path.dirname(filePath) in extracted_folder and not (os.path.splitext(fileName)[1] in ['.720p','.1080p']):
                    part = int(re.search(r'\d+', os.path.splitext(fileName)[1]).group())
                    if part == 1: # we only want to extract the primary part.
                        Logger.debug("MAIN: Found primary part of a multi-part archive %s. Extracting", file)                       
                    else:
                        Logger.debug("MAIN: Found part %s of a multi-part archive %s. Ignoring", part, file)
                        continue
                Logger.info("MAIN: Found compressed archive %s for file %s", fileExtension, filePath)
                try:
                    if inputCategory in hpCategory: # HP needs to scan the same dir as passed to downloader. 
                        extractor.extract(filePath, inputDirectory)
                    else:
                        extractor.extract(filePath, outputDestination)
                    extractionSuccess = True # we use this variable to determine if we need to pause a torrent or not in uTorrent (don't need to pause archived content)
                    extracted_folder.append(os.path.dirname(filePath))
                except:
                    Logger.exception("MAIN: Extraction failed for: %s", file)
                continue
            elif not inputCategory in cpsCategory + sbCategory: #process all for non-video categories.
                Logger.info("MAIN: Found file %s for category %s", filePath, inputCategory)
                copy_link(filePath, targetDirectory, useLink, outputDestination)
                copy_list.append([filePath, os.path.join(outputDestination, file)])
                continue
            else:
                Logger.debug("MAIN: Ignoring unknown filetype %s for file %s", fileExtension, filePath)
                continue
    if not inputCategory in hpCategory: #don't flatten hp in case multi cd albums, and we need to copy this back later. 
        flatten(outputDestination)

    # Now check if movie files exist in destination:
    if inputCategory in cpsCategory + sbCategory: 
        for dirpath, dirnames, filenames in os.walk(outputDestination):
            for file in filenames:
                filePath = os.path.join(dirpath, file)
                fileName, fileExtension = os.path.splitext(file)
                if fileExtension in mediaContainer:  # If the file is a video file
                    if is_sample(filePath, inputName, minSampleSize):
                        Logger.debug("MAIN: Removing sample file: %s", filePath)
                        os.unlink(filePath)  # remove samples
                    else:
                        Logger.debug("MAIN: Found media file: %s", filePath)
                        video2 = video2 + 1
                else:
                    Logger.debug("MAIN: File %s is not a media file", filePath)
        if video2 >= video and video2 > int(0):  # Check that all video files were moved
            Logger.debug("MAIN: Found %s media files", str(video2))
            status = int(0)
        else:
            Logger.debug("MAIN: Found %s media files in output. %s were found in input", str(video2), str(video))

    processCategories = cpsCategory + sbCategory + hpCategory + mlCategory + gzCategory

    if (inputCategory in user_script_categories and not "NONE" in user_script_categories) or ("ALL" in user_script_categories and not inputCategory in processCategories):
        Logger.info("MAIN: Processing user script %s.", user_script)
        result = external_script(outputDestination)
    elif status == int(0) or (inputCategory in hpCategory + mlCategory + gzCategory): # if movies linked/extracted or for other categories.
        Logger.debug("MAIN: Calling autoProcess script for successful download.")
        status = int(0) # hp, my, gz don't support failed.
    else:
        Logger.error("MAIN: Something failed! Please check logs. Exiting")
        sys.exit(-1)

    if inputCategory in cpsCategory:
        Logger.info("MAIN: Calling CouchPotatoServer to post-process: %s", inputName)
        download_id = inputHash
        result = autoProcessMovie.process(outputDestination, inputName, status, clientAgent, download_id, inputCategory)
    elif inputCategory in sbCategory:
        Logger.info("MAIN: Calling Sick-Beard to post-process: %s", inputName)
        result = autoProcessTV.processEpisode(outputDestination, inputName, status, inputCategory)
    elif inputCategory in hpCategory:
        Logger.info("MAIN: Calling HeadPhones to post-process: %s", inputName)
        result = autoProcessMusic.process(inputDirectory, inputName, status, inputCategory)
    elif inputCategory in mlCategory:
        Logger.info("MAIN: Calling Mylar to post-process: %s", inputName)
        result = autoProcessComics.processEpisode(outputDestination, inputName, status, inputCategory)
    elif inputCategory in gzCategory:
        Logger.info("MAIN: Calling Gamez to post-process: %s", inputName)
        result = autoProcessGames.process(outputDestination, inputName, status, inputCategory)

    if result == 1:
        Logger.info("MAIN: A problem was reported in the autoProcess* script. If torrent was paused we will resume seeding")

    if inputCategory in hpCategory:
        # we need to move the output dir files back...
        Logger.debug("MAIN: Moving temporary HeadPhones files back to allow seeding.")
        for item in copy_list:
            if os.path.isfile(os.path.normpath(item[1])): # check to ensure temp files still exist.
                if os.path.isfile(os.path.normpath(item[0])): # both exist, remove temp version
                    Logger.debug("MAIN: File %s still present. Removing tempoary file %s", str(item[0]), str(item[1]))
                    os.unlink(os.path.normpath(item[1]))
                    continue
                else: # move temp version back to allow seeding or Torrent removal.
                    Logger.debug("MAIN: Moving %s to %s", str(item[1]), str(item[0]))
                    shutil.move(os.path.normpath(item[1]), os.path.normpath(item[0]))
                    continue

    # Hardlink solution for uTorrent, need to implent support for deluge, transmission
    if clientAgent in ['utorrent', 'transmission']  and inputHash:
        # Delete torrent and torrentdata from Torrent client if processing was successful.
        if deleteOriginal == 1 and result != 1:
            Logger.debug("MAIN: Deleting torrent %s from %s", inputName, clientAgent)
            if clientAgent == 'utorrent' and utorrentClass != "":
                utorrentClass.removedata(inputHash)
                if not inputCategory in hpCategory:
                    utorrentClass.remove(inputHash)
            if clientAgent == 'transmission' and TransmissionClass !="":
                if inputCategory in hpCategory: #don't delete actual files for hp category, just remove torrent.
                    TransmissionClass.remove_torrent(inputID, False)
                else:
                    TransmissionClass.remove_torrent(inputID, True)
        # we always want to resume seeding, for now manually find out what is wrong when extraction fails
        else:
            Logger.debug("MAIN: Starting torrent %s in %s", inputName, clientAgent)
            if clientAgent == 'utorrent' and utorrentClass != "":
                utorrentClass.start(inputHash)
            if clientAgent == 'transmission' and TransmissionClass !="":
                TransmissionClass.start_torrent(inputID)
        time.sleep(5)        
    #cleanup
    if inputCategory in processCategories and result == 0 and os.path.isdir(outputDestination):
        num_files_new = int(0)
        file_list = []
        for dirpath, dirnames, filenames in os.walk(outputDestination):
            for file in filenames:
                filePath = os.path.join(dirpath, file)
                fileName, fileExtension = os.path.splitext(file)
                if fileExtension in mediaContainer or fileExtension in metaContainer:
                    num_files_new = num_files_new + 1
                    file_list.append(file)
        if num_files_new == int(0): 
            Logger.info("All files have been processed. Cleaning outputDirectory %s", outputDestination)
            shutil.rmtree(outputDestination)
        else:
            Logger.info("outputDirectory %s still contains %s media and/or meta files. This directory will not be removed.", outputDestination, num_files_new)
            for item in file_list:
                Logger.debug("media/meta file found: %s", item)
    Logger.info("MAIN: All done.")
Beispiel #11
0
    # Check if destination directory exists (important for reprocessing of history items)
    if not os.path.isdir(os.environ["NZBPP_DIRECTORY"]):
        Logger.error(
            "Post-Process: Nothing to post-process: destination directory %s doesn't exist",
            os.environ["NZBPP_DIRECTORY"],
        )
        status = 1

    # All checks done, now launching the script.
    Logger.info("Script triggered from NZBGet, starting autoProcessTV...")
    clientAgent = "nzbget"
    result = autoProcessTV.processEpisode(
        os.environ["NZBPP_DIRECTORY"],
        os.environ["NZBPP_NZBFILENAME"],
        status,
        clientAgent,
        os.environ["NZBPP_CATEGORY"],
    )
# SABnzbd Pre 0.7.17
elif len(sys.argv) == SABNZB_NO_OF_ARGUMENTS:
    # SABnzbd argv:
    # 1 The final directory of the job (full path)
    # 2 The original name of the NZB file
    # 3 Clean version of the job name (no path info and ".nzb" removed)
    # 4 Indexer's report number (if supported)
    # 5 User-defined category
    # 6 Group that the NZB was posted in e.g. alt.binaries.x
    # 7 Status of post processing. 0 = OK, 1=failed verification, 2=failed unpack, 3=1+2
    Logger.info("Script triggered from SABnzbd, starting autoProcessTV...")
    clientAgent = "sabnzbd"