Exemplo n.º 1
0
    def _handle_torrent(self, magnet_hash):
        h = self.torrent_client.torrents.get(magnet_hash)
        if not h:
            return
        files = [f for f in torrent.get_torrent_info(h).files()]
        filenames = [os.path.basename(f.path) for f in files]

        def is_state(filename, state):
            return self.get_file_status(magnet_hash,
                                        filename)["status"] == state

        if all(is_state(filename, "ready") for filename in filenames):
            self.torrent_client.remove_torrent(magnet_hash, remove_files=True)
            for f in files:
                filepath = os.path.join(settings.DOWNLOAD_DIR, magnet_hash,
                                        f.path)
                self.subtitle_downloads.pop(filepath, None)
            return

        for i, f in enumerate(files):
            if h.file_priorities()[i] == 0:
                continue
            filename = os.path.basename(f.path)

            filepath = os.path.join(settings.DOWNLOAD_DIR, magnet_hash, f.path)

            if is_state(filename, FileStatus.DOWNLOAD_FINISHED):
                self._download_external_subtitles(filepath)
            elif is_state(filename, FileStatus.WAITING_FOR_CONVERSION):
                output_filepath = _get_output_filepath(magnet_hash, filepath)
                os.makedirs(os.path.dirname(output_filepath), exist_ok=True)
                self.video_converter.convert_file(filepath, output_filepath)
Exemplo n.º 2
0
def _subtitle_indexes(h, filename):
    subtitle_filenames = _subtitle_filenames(h, filename)
    files = torrent.get_torrent_info(h).files()
    subtitle_set = []
    subtitle_indexes = []
    for i, f in enumerate(files):
        basename = os.path.basename(f.path).lower()
        if basename in subtitle_filenames and basename not in subtitle_set:
            subtitle_set.append(basename)
            subtitle_indexes.append(i)
    return subtitle_indexes
Exemplo n.º 3
0
def _subtitle_filenames(h, filename):
    files = torrent.get_torrent_info(h).files()
    filename_without_extension = os.path.splitext(filename)[0]
    subtitle_filenames = []
    for f in files:
        basename = os.path.basename(f.path)
        basename_lower = basename.lower()
        if basename_lower.endswith(".srt") and basename_lower.startswith(
                filename_without_extension.lower()):
            subtitle_filenames.append(basename)
    return subtitle_filenames
Exemplo n.º 4
0
 def downloads(self):
     result = {}
     for magnet_hash, h in self.torrent_client.torrents.items():
         result[magnet_hash] = {}
         files = torrent.get_torrent_info(h).files()
         file_priorities = h.file_priorities()
         for priority, f in zip(list(file_priorities), list(files)):
             if priority == 0:
                 continue
             filename = os.path.basename(f.path)
             result[magnet_hash][filename] = self.get_file_status(
                 magnet_hash, filename)
     return result
Exemplo n.º 5
0
    def _handle_torrent(self, magnet_hash):
        h = self.torrent_client.torrents.get(magnet_hash)
        if not h:
            return
        file_priorities = h.file_priorities()
        files = [
            f for priority, f in zip(file_priorities,
                                     torrent.get_torrent_info(h).files())
            if priority != 0
        ]
        filenames = [os.path.basename(f.path) for f in files]
        video_filenames = [
            fn for fn in filenames if any(
                fn.endswith(ext) for ext in settings.SUPPORTED_EXTENSIONS)
        ]

        def is_state(filename, state):
            return self.get_file_status(magnet_hash,
                                        filename)["status"] == state

        if all(
                is_state(filename, FileStatus.READY)
                for filename in video_filenames):
            self.torrent_client.remove_torrent(magnet_hash, remove_files=True)
            for f in files:
                filepath = os.path.join(settings.DOWNLOAD_DIR, magnet_hash,
                                        f.path)
                self.subtitle_downloads.pop(filepath, None)
            return

        for f in files:
            filename = os.path.basename(f.path)

            if not any(
                    filename.endswith(ext)
                    for ext in settings.SUPPORTED_EXTENSIONS):
                continue

            filepath = os.path.join(settings.DOWNLOAD_DIR, magnet_hash, f.path)
            output_filepath = _get_output_filepath(magnet_hash, filepath)

            if is_state(filename, FileStatus.DOWNLOAD_FINISHED):
                self._download_external_subtitles(filepath)
            elif is_state(filename, FileStatus.WAITING_FOR_CONVERSION):
                os.makedirs(os.path.dirname(output_filepath), exist_ok=True)
                self.video_converter.convert_file(filepath, output_filepath)
            elif is_state(filename, FileStatus.READY_TO_COPY) or is_state(
                    filename, FileStatus.CONVERSION_FAILED):
                output_dir = os.path.dirname(output_filepath)
                os.makedirs(output_dir, exist_ok=True)
                shutil.copy(filepath, output_filepath)