#!/usr/bin/env python3 """ # Script dedup-readwise ## Metadata **Kind**: #obsidian/ob-script **Language**: #python **Parent**:: [[♯ Readwise]], [[Obsidian Chore Scripts]], [[Executable Markdown File]] ## Synopsis Sometimes, the Readwise Official plugin appends the file content to the existing files. This script detect the duplicated content in the file and remove the old copies. ## Script ```python # """ import sys def fix(file, dry_run): with open(file, 'r') as rd: lines = rd.readlines() first_line_no = 0 if lines[0].strip() == '---': for pos, line in enumerate(lines[1:]): if line.strip() == '---': first_line_no = pos + 2 last_title_pos = -1 for pos, line in enumerate(lines): if line.startswith('![rw-book-cover|256]'): last_title_pos = pos - 2 if last_title_pos > first_line_no: content = ''.join(lines[last_title_pos:]) if not dry_run: with open(file, 'w') as wt: wt.write(content) return True if __name__ == '__main__': import os import sys from pathlib import Path dry_run = len(sys.argv) > 1 and sys.argv[1] == '-n' for root, dirs, files in os.walk('robot/Readwise Library'): try: dirs.remove('Readwise Syncs History') except ValueError: pass for name in files: file = Path(root, name) if name.endswith('.md'): if fix(file, dry_run): print(f'Fix {file}') if '-2.' in name and not name.startswith("Readwise Sync "): print(f'Dup {file}') """ # vim: ft=python ``` """