gemini: move downloads helper to separate module

This commit is contained in:
dece 2021-06-11 18:05:19 +02:00
parent 1a221d30b8
commit c5e419752c
2 changed files with 23 additions and 17 deletions

View file

@ -1,12 +1,12 @@
"""Gemini-related features of the browser.""" """Gemini-related features of the browser."""
import logging import logging
from pathlib import Path
from typing import Optional from typing import Optional
from bebop.browser.browser import Browser from bebop.browser.browser import Browser
from bebop.command_line import CommandLine from bebop.command_line import CommandLine
from bebop.fs import get_downloads_path, get_identities_list_path from bebop.downloads import get_download_path
from bebop.fs import get_identities_list_path
from bebop.identity import ( from bebop.identity import (
ClientCertificateException, create_certificate, get_cert_and_key, ClientCertificateException, create_certificate, get_cert_and_key,
get_identities_for_url, load_identities, save_identities get_identities_for_url, load_identities, save_identities
@ -230,7 +230,7 @@ def _handle_successful_response(browser: Browser, response: Response, url: str):
page.encoding = encoding page.encoding = encoding
else: else:
download_dir = browser.config["download_path"] download_dir = browser.config["download_path"]
filepath = _get_download_path(url, download_dir=download_dir) 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.
@ -254,20 +254,6 @@ def _handle_successful_response(browser: Browser, response: Response, url: str):
return None return None
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_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_path / filename
def _handle_input_request( def _handle_input_request(
browser: Browser, browser: Browser,
from_url: str, from_url: str,

20
bebop/downloads.py Normal file
View file

@ -0,0 +1,20 @@
"""Downloads management."""
from pathlib import Path
from typing import Optional
from bebop.fs import get_downloads_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_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_path / filename