Пример #1
0
 def extract_from_rar(self, filter_, file_):
     """
     Extract valid files from RAR file
     :param filter_: valid file filter
     :type filter_: FileFilter
     :param file_: rar file path
     :type file_: unicode
     """
     possible_files = []
     rar = rarfile.RarFile(file_)
     for f in rar.infolist():
         if filter_.test(FileItem.from_filename(f.filename, "")):
             possible_files.append(f)
     if len(possible_files) != 0:
         the_file = possible_files[0]
         for f in possible_files:
             if f.file_size > the_file.file_size:
                 the_file = f
         rar.extract(the_file, os.path.split(file_)[0])
         self.logger.info("extract file, %s --- %s from rar, %s", os.path.split(file_)[0], the_file.filename, file_)
         fake_torrent_file = TorrentFile()
         fake_torrent_file.name = the_file.filename
         fake_torrent_file.size = the_file.file_size
         return fake_torrent_file
     return None
Пример #2
0
 def extract_from_rar(self, filter_, file_):
     """
     Extract valid files from RAR file
     :param filter_: valid file filter
     :type filter_: FileFilter
     :param file_: rar file path
     :type file_: unicode
     """
     possible_files = []
     rar = rarfile.RarFile(file_)
     for f in rar.infolist():
         if filter_.test(FileItem.from_filename(f.filename, "")):
             possible_files.append(f)
     if len(possible_files) != 0:
         the_file = possible_files[0]
         for f in possible_files:
             if f.file_size > the_file.file_size:
                 the_file = f
         rar.extract(the_file, os.path.split(file_)[0])
         self.logger.info("extract file, %s --- %s from rar, %s",
                          os.path.split(file_)[0], the_file.filename, file_)
         fake_torrent_file = TorrentFile()
         fake_torrent_file.name = the_file.filename
         fake_torrent_file.size = the_file.file_size
         return fake_torrent_file
     return None
Пример #3
0
    def get_tv_show_file_from_torrent(self, torrent, filter_):
        """
        :param torrent:
        :type torrent: TorrentObject
        :param filter_:
        :type filter_: FileFilter
        :rtype FileItem:
        """
        files = self.torrent_manager.get_torrent_files(torrent.hash)
        rar_filter = FileFilter(".*", ["rar"])
        valid_files = []
        for file_ in files:
            file_item = FileItem.from_filename(file_.name, "")
            if filter_.test(file_item):
                valid_files.append(file_)
            elif rar_filter.test(file_item):
                extracted_file = self.extract_from_rar(
                    filter_,
                    self.torrent_manager.get_torrent_file_path(
                        torrent.name, file_.name))
                if extracted_file is not None:
                    valid_files.append(extracted_file)

        if len(valid_files) == 0:
            # TODO: Make filter parameter "extensions" not hardcoded
            media_filter = FileFilter(".*", ["mkv", "mp4", "avi"])
            for file_ in files:
                if media_filter.test(FileItem.from_filename(file_.name, "")):
                    valid_files.append(file_)
        if len(valid_files) == 0:
            self.logger.info("No valid files found")
            return None
        id_ = 0
        i = 1
        while i < len(valid_files):
            if valid_files[i].size > valid_files[id_].size:
                id_ = i
            i += 1
        self.logger.debug("validFile id_=%d, name=%s", id_,
                          valid_files[id_].name)
        try:
            complete_path = self.torrent_manager.get_torrent_file_path(
                torrent.name, valid_files[id_].name)
        except IOError as e:
            raise
        file_ = FileItem.from_complete_path(complete_path)
        return file_
Пример #4
0
    def get_tv_show_file_from_torrent(self, torrent, filter_):
        """
        :param torrent:
        :type torrent: TorrentObject
        :param filter_:
        :type filter_: FileFilter
        :rtype FileItem:
        """
        files = self.torrent_manager.get_torrent_files(torrent.hash)
        rar_filter = FileFilter(".*", ["rar"])
        valid_files = []
        for file_ in files:
            file_item = FileItem.from_filename(file_.name, "")
            if filter_.test(file_item):
                valid_files.append(file_)
            elif rar_filter.test(file_item):
                extracted_file = self.extract_from_rar(
                    filter_,
                    self.torrent_manager.get_torrent_file_path(torrent.name, file_.name)
                )
                if extracted_file is not None:
                    valid_files.append(extracted_file)

        if len(valid_files) == 0:
            # TODO: Make filter parameter "extensions" not hardcoded
            media_filter = FileFilter(".*", ["mkv", "mp4", "avi"])
            for file_ in files:
                if media_filter.test(FileItem.from_filename(file_.name, "")):
                    valid_files.append(file_)
        if len(valid_files) == 0:
            self.logger.info("No valid files found")
            return None
        id_ = 0
        i = 1
        while i < len(valid_files):
            if valid_files[i].size > valid_files[id_].size:
                id_ = i
            i += 1
        self.logger.debug("validFile id_=%d, name=%s", id_, valid_files[id_].name)
        try:
            complete_path = self.torrent_manager.get_torrent_file_path(torrent.name, valid_files[id_].name)
        except IOError as e:
            raise
        file_ = FileItem.from_complete_path(complete_path)
        return file_