config: add download_path

This commit is contained in:
dece 2021-05-09 23:02:56 +02:00
parent 056616b130
commit 6ceb75b84c
4 changed files with 10 additions and 5 deletions

View file

@ -77,6 +77,7 @@ Here are the available options:
|----------------------------|--------------|----------------|---------------------------------------| |----------------------------|--------------|----------------|---------------------------------------|
| `connect_timeout` | int | 10 | Seconds before connection times out. | | `connect_timeout` | int | 10 | Seconds before connection times out. |
| `text_width` | int | 80 | Rendered line length. | | `text_width` | int | 80 | Rendered line length. |
| `download_path` | string | | Download path. |
| `source_editor` | string list | `["vi"]` | Command to use for editing sources. | | `source_editor` | string list | `["vi"]` | Command to use for editing sources. |
| `command_editor` | string list | `["vi"]` | Command to use for editing CLI input. | | `command_editor` | string list | `["vi"]` | Command to use for editing CLI input. |
| `history_limit` | int | 1000 | Maximum entries in history. | | `history_limit` | int | 1000 | Maximum entries in history. |

View file

@ -175,7 +175,8 @@ def _handle_successful_response(browser: Browser, response: Response, url: str):
text = response.content.decode("utf-8", errors="replace") text = response.content.decode("utf-8", errors="replace")
page = Page.from_text(text) page = Page.from_text(text)
else: 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, # If a page has been produced, load it. Else if a file has been retrieved,
# download it. # download it.
@ -200,16 +201,18 @@ def _handle_successful_response(browser: Browser, response: Response, url: str):
return False 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.""" """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) url_parts = url.rsplit("/", maxsplit=1)
if url_parts: if url_parts:
filename = url_parts[-1] filename = url_parts[-1]
else: else:
filename = url.split("://")[1] if "://" in url else url filename = url.split("://")[1] if "://" in url else url
filename = filename.replace("/", "_") filename = filename.replace("/", "_")
return download_dir / filename return download_path / filename
def _handle_input_request(browser: Browser, from_url: str, message: str =None): def _handle_input_request(browser: Browser, from_url: str, message: str =None):

View file

@ -7,6 +7,7 @@ import os.path
DEFAULT_CONFIG = { DEFAULT_CONFIG = {
"connect_timeout": 10, "connect_timeout": 10,
"text_width": 80, "text_width": 80,
"download_path": "",
"source_editor": ["vi"], "source_editor": ["vi"],
"command_editor": ["vi"], "command_editor": ["vi"],
"history_limit": 1000, "history_limit": 1000,

View file

@ -29,7 +29,7 @@ def get_user_data_path() -> Path:
@lru_cache(None) @lru_cache(None)
def get_downloads_path() -> Path: 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"))) xdg_config_path = Path(getenv("XDG_CONFIG_HOME", expanduser("~/.config")))
download_path = "" download_path = ""
try: try: