Ejemplo n.º 1
0
    def addDefaultShow(indexer, indexer_id, name, status):
        """
        Adds a new show with the default settings
        """
        if not findCertainShow(sickrage.srCore.SHOWLIST, int(indexer_id)):
            sickrage.srLogger.info("Adding show " + str(indexer_id))
            root_dirs = sickrage.srConfig.ROOT_DIRS.split('|')

            try:
                location = root_dirs[int(root_dirs[0]) + 1]
            except Exception:
                location = None

            if location:
                showPath = os.path.join(location, sanitizeFileName(name))
                dir_exists = makeDir(showPath)

                if not dir_exists:
                    sickrage.srLogger.warning("Unable to create the folder %s , can't add the show" % showPath)
                    return
                else:
                    chmodAsParent(showPath)

                sickrage.srCore.SHOWQUEUE.addShow(int(indexer), int(indexer_id), showPath,
                                              default_status=status,
                                              quality=int(sickrage.srConfig.QUALITY_DEFAULT),
                                              flatten_folders=int(sickrage.srConfig.FLATTEN_FOLDERS_DEFAULT),
                                              paused=sickrage.srConfig.TRAKT_START_PAUSED,
                                              default_status_after=status,
                                              archive=sickrage.srConfig.ARCHIVE_DEFAULT)
            else:
                sickrage.srLogger.warning("There was an error creating the show, no root directory setting found")
                return
Ejemplo n.º 2
0
    def update_show_indexer_metadata(self, show_obj):
        if self.show_metadata and show_obj and self._has_show_metadata(
                show_obj):
            sickrage.srLogger.debug(
                "Metadata provider " + self.name +
                " updating show indexer info metadata file for " +
                show_obj.name)

            nfo_file_path = self.get_show_file_path(show_obj)

            try:
                with io.open(nfo_file_path, 'rb') as xmlFileObj:
                    showXML = ElementTree(file=xmlFileObj)

                indexerid = showXML.find('id')

                root = showXML.getroot()
                if indexerid is not None:
                    indexerid.text = str(show_obj.indexerid)
                else:
                    SubElement(root, "id").text = str(show_obj.indexerid)

                # Make it purdy
                indentXML(root)

                showXML.write(nfo_file_path, encoding='UTF-8')
                chmodAsParent(nfo_file_path)

                return True
            except IOError as e:
                sickrage.srLogger.error(
                    "Unable to write file to " + nfo_file_path +
                    " - are you sure the folder is writable? {}".format(
                        e.message))
Ejemplo n.º 3
0
    def update_show_indexer_metadata(self, show_obj):
        if self.show_metadata and show_obj and self._has_show_metadata(show_obj):
            sickrage.srLogger.debug(
                    "Metadata provider " + self.name + " updating show indexer info metadata file for " + show_obj.name)

            nfo_file_path = self.get_show_file_path(show_obj)

            try:
                with io.open(nfo_file_path, 'rb') as xmlFileObj:
                    showXML = ElementTree(file=xmlFileObj)

                indexerid = showXML.find('id')

                root = showXML.getroot()
                if indexerid is not None:
                    indexerid.text = str(show_obj.indexerid)
                else:
                    SubElement(root, "id").text = str(show_obj.indexerid)

                # Make it purdy
                indentXML(root)

                showXML.write(nfo_file_path, encoding='UTF-8')
                chmodAsParent(nfo_file_path)

                return True
            except IOError as e:
                sickrage.srLogger.error(
                        "Unable to write file to " + nfo_file_path + " - are you sure the folder is writable? {}".format(e.message))
Ejemplo n.º 4
0
    def addDefaultShow(indexer, indexer_id, name, status):
        """
        Adds a new show with the default settings
        """
        if not findCertainShow(sickrage.srCore.SHOWLIST, int(indexer_id)):
            sickrage.srLogger.info("Adding show " + str(indexer_id))
            root_dirs = sickrage.srConfig.ROOT_DIRS.split('|')

            try:
                location = root_dirs[int(root_dirs[0]) + 1]
            except Exception:
                location = None

            if location:
                showPath = os.path.join(location, sanitizeFileName(name))
                dir_exists = makeDir(showPath)

                if not dir_exists:
                    sickrage.srLogger.warning("Unable to create the folder %s , can't add the show" % showPath)
                    return
                else:
                    chmodAsParent(showPath)

                sickrage.srCore.SHOWQUEUE.addShow(int(indexer), int(indexer_id), showPath,
                                              default_status=status,
                                              quality=int(sickrage.srConfig.QUALITY_DEFAULT),
                                              flatten_folders=int(sickrage.srConfig.FLATTEN_FOLDERS_DEFAULT),
                                              paused=sickrage.srConfig.TRAKT_START_PAUSED,
                                              default_status_after=status,
                                              archive=sickrage.srConfig.ARCHIVE_DEFAULT)
            else:
                sickrage.srLogger.warning("There was an error creating the show, no root directory setting found")
                return
Ejemplo n.º 5
0
    def dumpHTML(data):
        dumpName = os.path.join(sickrage.srConfig.CACHE_DIR, "custom_torrent.html")

        try:
            with io.open(dumpName, "wb") as fileOut:
                fileOut.write(data)
            chmodAsParent(dumpName)
        except IOError as e:
            sickrage.srLogger.error("Unable to save the file: %s " % repr(e))
            return False
        sickrage.srLogger.info("Saved custom_torrent html dump %s " % dumpName)
        return True
Ejemplo n.º 6
0
def getSubtitlesPath(video_path):
    if os.path.isabs(sickrage.srConfig.SUBTITLES_DIR):
        new_subtitles_path = sickrage.srConfig.SUBTITLES_DIR
    elif sickrage.srConfig.SUBTITLES_DIR:
        new_subtitles_path = os.path.join(os.path.dirname(video_path), sickrage.srConfig.SUBTITLES_DIR)
        dir_exists = makeDir(new_subtitles_path)
        if not dir_exists:
            sickrage.srLogger.error('Unable to create subtitles folder ' + new_subtitles_path)
        else:
            chmodAsParent(new_subtitles_path)
    else:
        new_subtitles_path = os.path.join(os.path.dirname(video_path))

    return new_subtitles_path
Ejemplo n.º 7
0
def getSubtitlesPath(video_path):
    if os.path.isabs(sickrage.srConfig.SUBTITLES_DIR):
        new_subtitles_path = sickrage.srConfig.SUBTITLES_DIR
    elif sickrage.srConfig.SUBTITLES_DIR:
        new_subtitles_path = os.path.join(os.path.dirname(video_path),
                                          sickrage.srConfig.SUBTITLES_DIR)
        dir_exists = makeDir(new_subtitles_path)
        if not dir_exists:
            sickrage.srLogger.error('Unable to create subtitles folder ' +
                                    new_subtitles_path)
        else:
            chmodAsParent(new_subtitles_path)
    else:
        new_subtitles_path = os.path.join(os.path.dirname(video_path))

    return new_subtitles_path
Ejemplo n.º 8
0
    def write_ep_file(self, ep_obj):
        """
        Generates and writes ep_obj's metadata under the given path with the
        given filename root. Uses the episode's name with the extension in
        _ep_nfo_extension.

        ep_obj: TVEpisode object for which to create the metadata

        file_name_path: The file name to use for this metadata. Note that the extension
                will be automatically added based on _ep_nfo_extension. This should
                include an absolute path.

        Note that this method expects that _ep_data will return an ElementTree
        object. If your _ep_data returns data in another format yo'll need to
        override this method.
        """

        data = self._ep_data(ep_obj)

        if not data:
            return False

        nfo_file_path = self.get_episode_file_path(ep_obj)
        nfo_file_dir = os.path.dirname(nfo_file_path)

        try:
            if not os.path.isdir(nfo_file_dir):
                sickrage.srLogger.debug(
                    "Metadata dir didn't exist, creating it at " +
                    nfo_file_dir)
                os.makedirs(nfo_file_dir)
                chmodAsParent(nfo_file_dir)

            sickrage.srLogger.debug("Writing episode nfo file to " +
                                    nfo_file_path)
            nfo_file = io.open(nfo_file_path, 'wb')
            data.write(nfo_file, encoding='UTF-8')
            nfo_file.close()
            chmodAsParent(nfo_file_path)
        except IOError as e:
            sickrage.srLogger.error(
                "Unable to write file to " + nfo_file_path +
                " - are you sure the folder is writable? {}".format(e.message))
            return False

        return True
Ejemplo n.º 9
0
    def write_show_file(self, show_obj):
        """
        Generates and writes show_obj's metadata under the given path to the
        filename given by get_show_file_path()

        show_obj: TVShow object for which to create the metadata

        path: An absolute or relative path where we should put the file. Note that
                the file name will be the default show_file_name.

        Note that this method expects that _show_data will return an ElementTree
        object. If your _show_data returns data in another format yo'll need to
        override this method.
        """

        data = self._show_data(show_obj)

        if not data:
            return False

        nfo_file_path = self.get_show_file_path(show_obj)
        nfo_file_dir = os.path.dirname(nfo_file_path)

        try:
            if not os.path.isdir(nfo_file_dir):
                sickrage.srLogger.debug(
                    "Metadata dir didn't exist, creating it at " +
                    nfo_file_dir)
                os.makedirs(nfo_file_dir)
                chmodAsParent(nfo_file_dir)

            sickrage.srLogger.debug("Writing show nfo file to " +
                                    nfo_file_path)

            nfo_file = io.open(nfo_file_path, 'wb')
            data.write(nfo_file, encoding='UTF-8')
            nfo_file.close()
            chmodAsParent(nfo_file_path)
        except IOError as e:
            sickrage.srLogger.error(
                "Unable to write file to " + nfo_file_path +
                " - are you sure the folder is writable? {}".format(e.message))
            return False

        return True
Ejemplo n.º 10
0
    def write_ep_file(self, ep_obj):
        """
        Generates and writes ep_obj's metadata under the given path with the
        given filename root. Uses the episode's name with the extension in
        _ep_nfo_extension.

        ep_obj: TVEpisode object for which to create the metadata

        file_name_path: The file name to use for this metadata. Note that the extension
                will be automatically added based on _ep_nfo_extension. This should
                include an absolute path.

        Note that this method expects that _ep_data will return an ElementTree
        object. If your _ep_data returns data in another format yo'll need to
        override this method.
        """

        data = self._ep_data(ep_obj)

        if not data:
            return False

        nfo_file_path = self.get_episode_file_path(ep_obj)
        nfo_file_dir = os.path.dirname(nfo_file_path)

        try:
            if not os.path.isdir(nfo_file_dir):
                sickrage.srLogger.debug("Metadata dir didn't exist, creating it at " + nfo_file_dir)
                os.makedirs(nfo_file_dir)
                chmodAsParent(nfo_file_dir)

            sickrage.srLogger.debug("Writing episode nfo file to " + nfo_file_path)

            nfo_file = io.open(nfo_file_path, 'wb')

            data.write(nfo_file)
            nfo_file.close()
            chmodAsParent(nfo_file_path)
        except IOError as e:
            sickrage.srLogger.error(
                    "Unable to write file to " + nfo_file_path + " - are you sure the folder is writable? {}".format(e.message))
            return False

        return True
Ejemplo n.º 11
0
def _downloadResult(result):
    """
    Downloads a result to the appropriate black hole folder.

    :param result: SearchResult instance to download.
    :return: boolean, True on success
    """

    resProvider = result.provider
    if resProvider is None:
        sickrage.srLogger.error("Invalid provider name - this is a coding error, report it please")
        return False

    # nzbs with an URL can just be downloaded from the provider
    if result.resultType == "nzb":
        newResult = resProvider.downloadResult(result)
    # if it's an nzb data result
    elif result.resultType == "nzbdata":

        # get the final file path to the nzb
        fileName = os.path.join(sickrage.srConfig.NZB_DIR, result.name + ".nzb")

        sickrage.srLogger.info("Saving NZB to " + fileName)

        newResult = True

        # save the data to disk
        try:
            with io.open(fileName, 'w') as fileOut:
                fileOut.write(result.extraInfo[0])

            chmodAsParent(fileName)

        except EnvironmentError as e:
            sickrage.srLogger.error("Error trying to save NZB to black hole: {}".format(e.message))
            newResult = False
    elif resProvider.type == "torrent":
        newResult = resProvider.downloadResult(result)
    else:
        sickrage.srLogger.error("Invalid provider type - this is a coding error, report it please")
        newResult = False

    return newResult
Ejemplo n.º 12
0
    def write_show_file(self, show_obj):
        """
        Generates and writes show_obj's metadata under the given path to the
        filename given by get_show_file_path()

        show_obj: TVShow object for which to create the metadata

        path: An absolute or relative path where we should put the file. Note that
                the file name will be the default show_file_name.

        Note that this method expects that _show_data will return an ElementTree
        object. If your _show_data returns data in another format yo'll need to
        override this method.
        """

        data = self._show_data(show_obj)

        if not data:
            return False

        nfo_file_path = self.get_show_file_path(show_obj)
        nfo_file_dir = os.path.dirname(nfo_file_path)

        try:
            if not os.path.isdir(nfo_file_dir):
                sickrage.srLogger.debug("Metadata dir didn't exist, creating it at " + nfo_file_dir)
                os.makedirs(nfo_file_dir)
                chmodAsParent(nfo_file_dir)

            sickrage.srLogger.debug("Writing show nfo file to " + nfo_file_path)

            nfo_file = io.open(nfo_file_path, 'wb')

            data.write(nfo_file)
            nfo_file.close()
            chmodAsParent(nfo_file_path)
        except IOError as e:
            sickrage.srLogger.error(
                    "Unable to write file to " + nfo_file_path + " - are you sure the folder is writable? {}".format(e.message))
            return False

        return True
Ejemplo n.º 13
0
def save_subtitles(video, subtitles, single=False, directory=None):
    saved_subtitles = []
    for subtitle in subtitles:
        # check content
        if subtitle.content is None:
            sickrage.srLogger.debug("Skipping subtitle for %s: no content" %
                                    video.name)
            continue

        # check language
        if subtitle.language in set(s.language for s in saved_subtitles):
            sickrage.srLogger.debug(
                "Skipping subtitle for %s: language already saved" %
                video.name)
            continue

        # create subtitle path
        subtitle_path = subliminal.subtitle.get_subtitle_path(
            video.name, None if single else subtitle.language)
        if directory is not None:
            subtitle_path = os.path.join(directory,
                                         os.path.split(subtitle_path)[1])

        # save content as is or in the specified encoding
        sickrage.srLogger.debug("Saving subtitle for %s to %s" %
                                (video.name, subtitle_path))
        if subtitle.encoding:
            with io.open(subtitle_path, 'w', encoding=subtitle.encoding) as f:
                f.write(subtitle.text)
        else:
            with io.open(subtitle_path, 'wb') as f:
                f.write(subtitle.content)

        # chmod and set group for the saved subtitle
        chmodAsParent(subtitle_path)
        fixSetGroupID(subtitle_path)

        # check single
        if single:
            break

    return saved_subtitles
Ejemplo n.º 14
0
    def _write_image(self, image_data, image_path, obj=None):
        """
        Saves the data in image_data to the location image_path. Returns True/False
        to represent success or failure.

        image_data: binary image data to write to file
        image_path: file location to save the image to
        """

        # don't bother overwriting it
        if os.path.isfile(image_path):
            sickrage.srLogger.debug("Image already exists, not downloading")
            return False

        image_dir = os.path.dirname(image_path)

        if not image_data:
            sickrage.srLogger.debug(
                "Unable to retrieve image to save in %s, skipping" %
                image_path)
            return False

        try:
            if not os.path.isdir(image_dir):
                sickrage.srLogger.debug(
                    "Metadata dir didn't exist, creating it at " + image_dir)
                os.makedirs(image_dir)
                chmodAsParent(image_dir)

            outFile = io.open(image_path, 'wb')
            outFile.write(image_data)
            outFile.close()
            chmodAsParent(image_path)
        except IOError as e:
            sickrage.srLogger.error(
                "Unable to write image to " + image_path +
                " - are you sure the show folder is writable? {}".format(
                    e.message))
            return False

        return True
Ejemplo n.º 15
0
    def write_ep_file(self, ep_obj):
        """
        Generates and writes ep_obj's metadata under the given path with the
        given filename root. Uses the episode's name with the extension in
        _ep_nfo_extension.

        ep_obj: TVEpisode object for which to create the metadata

        file_name_path: The file name to use for this metadata. Note that the extension
                will be automatically added based on _ep_nfo_extension. This should
                include an absolute path.
        """
        data = self._ep_data(ep_obj)

        if not data:
            return False

        nfo_file_path = self.get_episode_file_path(ep_obj)
        nfo_file_dir = os.path.dirname(nfo_file_path)

        try:
            if not os.path.isdir(nfo_file_dir):
                sickrage.srLogger.debug("Metadata dir didn't exist, creating it at " + nfo_file_dir)
                os.makedirs(nfo_file_dir)
                chmodAsParent(nfo_file_dir)

            sickrage.srLogger.debug("Writing episode nfo file to " + nfo_file_path)

            with io.open(nfo_file_path, 'w') as nfo_file:
                # Calling encode directly, b/c often descriptions have wonky characters.
                nfo_file.write(data.encode("utf-8"))

            chmodAsParent(nfo_file_path)

        except EnvironmentError as e:
            sickrage.srLogger.error(
                    "Unable to write file to " + nfo_file_path + " - are you sure the folder is writable? {}".format(e.message))
            return False

        return True
Ejemplo n.º 16
0
    def _write_image(self, image_data, image_path, obj=None):
        """
        Saves the data in image_data to the location image_path. Returns True/False
        to represent success or failure.

        image_data: binary image data to write to file
        image_path: file location to save the image to
        """

        # don't bother overwriting it
        if os.path.isfile(image_path):
            sickrage.srLogger.debug("Image already exists, not downloading")
            return False

        image_dir = os.path.dirname(image_path)

        if not image_data:
            sickrage.srLogger.debug("Unable to retrieve image to save in %s, skipping" % image_path)
            return False

        try:
            if not os.path.isdir(image_dir):
                sickrage.srLogger.debug("Metadata dir didn't exist, creating it at " + image_dir)
                os.makedirs(image_dir)
                chmodAsParent(image_dir)

            outFile = io.open(image_path, 'wb')
            outFile.write(image_data)
            outFile.close()
            chmodAsParent(image_path)
        except IOError as e:
            sickrage.srLogger.error(
                    "Unable to write image to " + image_path + " - are you sure the show folder is writable? {}".format(e.message))
            return False

        return True
Ejemplo n.º 17
0
def save_subtitles(video, subtitles, single=False, directory=None):
    saved_subtitles = []
    for subtitle in subtitles:
        # check content
        if subtitle.content is None:
            sickrage.srLogger.debug("Skipping subtitle for %s: no content" % video.name)
            continue

        # check language
        if subtitle.language in set(s.language for s in saved_subtitles):
            sickrage.srLogger.debug("Skipping subtitle for %s: language already saved" % video.name)
            continue

        # create subtitle path
        subtitle_path = subliminal.subtitle.get_subtitle_path(video.name, None if single else subtitle.language)
        if directory is not None:
            subtitle_path = os.path.join(directory, os.path.split(subtitle_path)[1])

        # save content as is or in the specified encoding
        sickrage.srLogger.debug("Saving subtitle for %s to %s" % (video.name, subtitle_path))
        if subtitle.encoding:
            with io.open(subtitle_path, 'w', encoding=subtitle.encoding) as f:
                f.write(subtitle.text)
        else:
            with io.open(subtitle_path, 'wb') as f:
                f.write(subtitle.content)

        # chmod and set group for the saved subtitle
        chmodAsParent(subtitle_path)
        fixSetGroupID(subtitle_path)

        # check single
        if single:
            break

    return saved_subtitles