Example #1
0
    def move_subtitle_files(self, sorter_file):
        """Check for existing subtitle files matching media file and move
        them to sort folder too.
        """

        for ext in ('.srt', '.sub', '.idx'):
            subtitle_path = Path(sorter_file.path.parent, '{}{}'.format(
                sorter_file.path.stem, ext))

            if subtitle_path.exists():
                log.info('Moving subtitle file {} to {}'.format(
                    self.relative_path(subtitle_path, self.sort_dir),
                    sorter_file.season_dir))
                subtitle_path.move(Path(self.sort_dir,
                                   sorter_file.season_dir))
    def process_zip(self, zip_file_path):
        """Process the subtitle zip and extract all files with a valid file
        extension. Remove the zip file when done.
        """

        # Make sure the downloaded zip file is valid
        if not zipfile.is_zipfile(zip_file_path):
            log.info('Invalid zip file {}'.format(
                SubtitleDownloader.relative_path(zip_file_path,
                                                 self.download_dir)))
            zip_file_path.remove()
            return False

        zip_file = zipfile.ZipFile(zip_file_path)
        for file in zip_file.namelist():
            file = Path(file)

            # Check file extension
            if not file.ext.lower() in ('.srt'):
                log.debug('Invalid subtitle file extension {}, '
                          'skipping'.format(file))
                continue

            # Name of unpacked subtitle file and the renamed subtitle file
            unpacked_subtitle_file = Path(self.download_dir, file)
            renamed_subtitle_file = Path(self.download_dir, '{}{}'.format(
                self.name, unpacked_subtitle_file.ext.lower()))

            # Skip the subtitle file if it's already exists
            if renamed_subtitle_file.exists():
                log.info('{} already exists'.format(
                    SubtitleDownloader.relative_path(renamed_subtitle_file,
                                                     self.download_dir)))
                continue

            # Extract the file to unpack dir and then move it to match the name
            # of the subtitle search
            log.info('Found subtitle, extracting subtitle file {}'.format(
                SubtitleDownloader.relative_path(renamed_subtitle_file,
                                                 self.download_dir)))
            zip_file.extract(file, self.download_dir)
            unpacked_subtitle_file.move(renamed_subtitle_file)

        # Remove the zip file
        zip_file_path.remove()