示例#1
0
    def update_content(self):

        self.content = dict()
        self.subdirectories = dict()

        for file in os.listdir(self.full_path):
            if os.path.isdir(os.path.join(self.full_path, file)):
                sub_content = dict()
                for sub_file in os.listdir(os.path.join(self.full_path, file)):
                    sub_content[sub_file] = tools.hash_file(os.path.join(self.full_path, file, sub_file))
                self.subdirectories[file] = sub_content
            else:
                self.content[file] = tools.hash_file(os.path.join(self.full_path, file))
    def update_content(self):

        self.content = dict()
        self.subdirectories = dict()

        for file in os.listdir(self.full_path):
            if os.path.isdir(os.path.join(self.full_path, file)):
                sub_content = dict()
                for sub_file in os.listdir(os.path.join(self.full_path, file)):
                    sub_content[sub_file] = tools.hash_file(os.path.join(self.full_path, file, sub_file))
                self.subdirectories[file] = sub_content
            else:
                self.content[file] = tools.hash_file(os.path.join(self.full_path, file))
示例#3
0
    def move_videos(self, downloaded_videos_meta, tmp_folder):

        def copy_file():
            if not os.path.isdir(os.path.split(target_path)[0]):
                os.mkdir(os.path.split(target_path)[0])
            shutil.move(source_path, target_path)

        def record_file():
            vid_id = 'unknown'
            for meta in downloaded_videos_meta:
                if meta['title'] + '.' + meta['ext'] == file:
                    vid_id = meta['id']
                    break

            self.directory.record.append(
                {'hash': file_hash,
                 'file_path': os.path.join(self.directory.full_path, self.config.extra_type, file),
                 'file_name': file,
                 'youtube_video_id': vid_id,
                 'config_type': self.config.extra_type})

        def determine_case():
            for content_file, content_file_hash in self.directory.content.items():
                if content_file == file:
                    return 'name_in_directory'

                if file_hash == content_file_hash:
                    return 'hash_in_directory'

            for sub_content in self.directory.subdirectories.values():
                for content_file, content_file_hash in sub_content.items():
                    if content_file == file:
                        return 'name_in_directory'
                    if file_hash == content_file_hash:
                        return 'hash_in_directory'

            return ''

        def handle_name_in_directory():
            if self.config.force:
                copy_file()
                record_file()
                self.directory.subdirectories[self.config.extra_type][file] = file_hash
            else:
                os.remove(source_path)

        def handle_hash_in_directory():
            if self.config.force:
                copy_file()
                record_file()
                if self.config.extra_type in self.directory.subdirectories:
                    self.directory.subdirectories[self.config.extra_type] = {file: file_hash}
                else:
                    self.directory.subdirectories = {self.config.extra_type: {file: file_hash}}
            else:
                os.remove(source_path)

        for file in os.listdir(tmp_folder):
            source_path = os.path.join(tmp_folder, file)
            target_path = os.path.join(self.directory.full_path, self.config.extra_type, file)

            file_hash = tools.hash_file(source_path)

            if any(file_hash == record['hash'] for record in self.directory.record):
                os.remove(source_path)
                continue

            case = determine_case()

            if case == 'name_in_directory':
                handle_name_in_directory()
            elif case == 'hash_in_directory':
                handle_hash_in_directory()
            else:
                copy_file()

                if self.config.extra_type in self.directory.subdirectories:
                    self.directory.subdirectories[self.config.extra_type][file] = file_hash
                else:
                    self.directory.subdirectories = {self.config.extra_type: {file: file_hash}}

                record_file()
    def move_videos(self, downloaded_videos_meta, tmp_folder):

        def copy_file():
            if not os.path.isdir(os.path.split(target_path)[0]):
                os.mkdir(os.path.split(target_path)[0])
            shutil.move(source_path, target_path)

        def record_file():
            vid_id = 'unknown'
            for meta in downloaded_videos_meta:
                if meta['title'] + '.' + meta['ext'] == file:
                    vid_id = meta['id']
                    break

            self.directory.record.append(
                {'hash': file_hash,
                 'file_path': os.path.join(self.directory.full_path, self.config.extra_type, file),
                 'file_name': file,
                 'youtube_video_id': vid_id,
                 'config_type': self.config.extra_type})

        def determine_case():
            for content_file, content_file_hash in self.directory.content.items():
                if content_file == file:
                    return 'name_in_directory'

                if file_hash == content_file_hash:
                    return 'hash_in_directory'

            for sub_content in self.directory.subdirectories.values():
                for content_file, content_file_hash in sub_content.items():
                    if content_file == file:
                        return 'name_in_directory'
                    if file_hash == content_file_hash:
                        return 'hash_in_directory'

            return ''

        def handle_name_in_directory():
            if self.config.force:
                copy_file()
                record_file()
                self.directory.subdirectories[self.config.extra_type][file] = file_hash
            else:
                os.remove(source_path)

        def handle_hash_in_directory():
            if self.config.force:
                copy_file()
                record_file()
                if self.config.extra_type in self.directory.subdirectories:
                    self.directory.subdirectories[self.config.extra_type] = {file: file_hash}
                else:
                    self.directory.subdirectories = {self.config.extra_type: {file: file_hash}}
            else:
                os.remove(source_path)

        for file in os.listdir(tmp_folder):
            source_path = os.path.join(tmp_folder, file)
            target_path = os.path.join(self.directory.full_path, self.config.extra_type, file)

            file_hash = tools.hash_file(source_path)

            if any(file_hash == record['hash'] for record in self.directory.record):
                os.remove(source_path)
                continue

            case = determine_case()

            if case == 'name_in_directory':
                handle_name_in_directory()
            elif case == 'hash_in_directory':
                handle_hash_in_directory()
            else:
                copy_file()

                if self.config.extra_type in self.directory.subdirectories:
                    self.directory.subdirectories[self.config.extra_type][file] = file_hash
                else:
                    self.directory.subdirectories = {self.config.extra_type: {file: file_hash}}

                record_file()