Skip to content

Commit

Permalink
feat: don't overwrite existing content by default
Browse files Browse the repository at this point in the history
  • Loading branch information
vladiliescu committed Jan 7, 2025
1 parent 5ef6e47 commit fb3407f
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 2 deletions.
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -33,8 +33,10 @@ uv run -q <download-path>/grabit.py [OPTIONS] URL
- `--fallback-title TEXT`: Fallback title if no title is found. Use `{date}` for the current date (default: `Untitled {date}`).
- `--use-readability-js / --no-use-readability-js`: Use Readability.js for processing pages. Disabling it will result in **some** processing courtesy of [ReadabiliPy](https://github.com/alan-turing-institute/ReadabiliPy), but it doesn't look so great to be honest (requires Node.js, default: `enabled`).
- `--create-domain-subdir / --no-create-domain-subdir`: Save the resulting files in a subdirectory named after the domain. Useful when saving a **lot** of bookmarks in the same Obsidian vault (default: `enabled`).
- `--overwrite / --no-overwrite`: Overwrite existing files.(default: `disabled`).
- `-f, --format [md|stdout.md|html|raw.html]`: Output format(s) to save the content in. Most useful are `md`, which saves the content to a Markdown file, and `stdout.md` which simply outputs the raw content so you can pipe it to something else, like the clipboard or Simon Willison's [llm cli](https://github.com/simonw/llm). Can be specified multiple times (default: `md`).


### Examples

- **Save a web page as Markdown with the default options:**
Expand Down
21 changes: 19 additions & 2 deletions grabit.py
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,12 @@ def should_output_file(output_formats):
help="Save the resulting file(s) in a subdirectory named after the domain.",
show_default=True,
)
@click.option(
"--overwrite/--no-overwrite",
default=False,
help="Overwrite existing files if they already exist.",
show_default=True,
)
@click.option(
"-f",
"--format",
Expand All @@ -113,6 +119,7 @@ def save(
fallback_title: str,
create_domain_subdir: bool,
output_formats: list[str],
overwrite: bool,
):
"""
Download an URL, convert it to Markdown with specified options, and save it to a file.
Expand Down Expand Up @@ -158,7 +165,7 @@ def save(

if should_output_file([fmt]):
# output_dir and safe_title are only defined if we're saving to a file
write_to_file(content, output_dir, safe_title, fmt)
write_to_file(content, output_dir, safe_title, fmt, overwrite)
else:
click.echo(content)

Expand Down Expand Up @@ -195,9 +202,19 @@ def try_add_yaml_frontmatter(markdown_content, yaml_frontmatter, title, url):
return markdown_content


def write_to_file(markdown_content, output_dir, safe_title, extension):
def write_to_file(
markdown_content: str,
output_dir: str,
safe_title: str,
extension: str,
overwrite: bool,
):
output_file = Path(output_dir) / f"{safe_title}.{extension}"

if not overwrite and output_file.exists():
click.echo(f"File {output_file} already exists. Use --overwrite to replace it.")
return

try:
with open(output_file, "w", encoding="utf-8") as f:
f.write(markdown_content)
Expand Down

0 comments on commit fb3407f

Please sign in to comment.