From 6ceb75b84cac478cb18547ba8c85b89d8fa5de5a Mon Sep 17 00:00:00 2001 From: dece Date: Sun, 9 May 2021 23:02:56 +0200 Subject: [PATCH] config: add download_path --- README.md | 1 + bebop/browser/gemini.py | 11 +++++++---- bebop/config.py | 1 + bebop/fs.py | 2 +- 4 files changed, 10 insertions(+), 5 deletions(-) diff --git a/README.md b/README.md index fbf13b3..fc01b70 100644 --- a/README.md +++ b/README.md @@ -77,6 +77,7 @@ Here are the available options: |----------------------------|--------------|----------------|---------------------------------------| | `connect_timeout` | int | 10 | Seconds before connection times out. | | `text_width` | int | 80 | Rendered line length. | +| `download_path` | string | | Download path. | | `source_editor` | string list | `["vi"]` | Command to use for editing sources. | | `command_editor` | string list | `["vi"]` | Command to use for editing CLI input. | | `history_limit` | int | 1000 | Maximum entries in history. | diff --git a/bebop/browser/gemini.py b/bebop/browser/gemini.py index 9917cc0..8257a12 100644 --- a/bebop/browser/gemini.py +++ b/bebop/browser/gemini.py @@ -175,7 +175,8 @@ def _handle_successful_response(browser: Browser, response: Response, url: str): text = response.content.decode("utf-8", errors="replace") page = Page.from_text(text) else: - filepath = _get_download_path(url) + download_dir = browser.config["download_path"] + filepath = _get_download_path(url, download_dir=download_dir) # If a page has been produced, load it. Else if a file has been retrieved, # download it. @@ -200,16 +201,18 @@ def _handle_successful_response(browser: Browser, response: Response, url: str): return False -def _get_download_path(url: str) -> Path: +def _get_download_path(url: str, download_dir: Optional[str] =None) -> Path: """Try to find the best download file path possible from this URL.""" - download_dir = get_downloads_path() + download_path = Path(download_dir) if download_dir else get_downloads_path() + if not download_path.exists(): + download_path.mkdir(parents=True) url_parts = url.rsplit("/", maxsplit=1) if url_parts: filename = url_parts[-1] else: filename = url.split("://")[1] if "://" in url else url filename = filename.replace("/", "_") - return download_dir / filename + return download_path / filename def _handle_input_request(browser: Browser, from_url: str, message: str =None): diff --git a/bebop/config.py b/bebop/config.py index fe97638..35a09d6 100644 --- a/bebop/config.py +++ b/bebop/config.py @@ -7,6 +7,7 @@ import os.path DEFAULT_CONFIG = { "connect_timeout": 10, "text_width": 80, + "download_path": "", "source_editor": ["vi"], "command_editor": ["vi"], "history_limit": 1000, diff --git a/bebop/fs.py b/bebop/fs.py index 3bb41a8..0650224 100644 --- a/bebop/fs.py +++ b/bebop/fs.py @@ -29,7 +29,7 @@ def get_user_data_path() -> Path: @lru_cache(None) def get_downloads_path() -> Path: - """Return the user downloads directory path.""" + """Return the user downloads directory path (fallbacks to home dir).""" xdg_config_path = Path(getenv("XDG_CONFIG_HOME", expanduser("~/.config"))) download_path = "" try: