diff --git a/src/initialize.py b/src/initialize.py index 3d184ee..a599084 100644 --- a/src/initialize.py +++ b/src/initialize.py @@ -28,3 +28,4 @@ def initialize(): install_via_pip("requests") install_via_pip("PyYAML") install_via_pip("inquirer") + install_via_pip("tqdm") diff --git a/src/setup/apps/notesnook.py b/src/setup/apps/notesnook.py index 26abd40..f871d4f 100644 --- a/src/setup/apps/notesnook.py +++ b/src/setup/apps/notesnook.py @@ -1,24 +1,12 @@ -import requests -from pathlib import Path - -from src.setup.system import appimagelauncher - +from src.util import appimage_install name = "Notesnook" def setup(): - """FOSS video editing utility""" + """FOSS Note taking utility""" - download_path = f"{Path.home()}/Applications/notesnook_linux_x86_64.AppImage" - download_url = ( - "https://notesnook.com/releases/linux/notesnook_linux_x86_64.AppImage" - ) - - appimagelauncher.setup() - - print(f"Downloading AppImage file to {download_path}") - Path(download_path).mkdir(parents=True, exist_ok=True) - open(download_path, "wb").write( - requests.get(download_url, allow_redirects=True).content + appimage_install( + "https://notesnook.com/releases/linux/notesnook_linux_x86_64.AppImage", + "notesnook_linux_x86_64", ) diff --git a/src/setup/system/appimagelauncher.py b/src/setup/system/appimagelauncher.py deleted file mode 100644 index d902104..0000000 --- a/src/setup/system/appimagelauncher.py +++ /dev/null @@ -1,9 +0,0 @@ -from src.util import paru_install - -name = "AppImage Launcher" - - -def setup(): - """Manages AppImage files""" - - paru_install("appimagelauncher") diff --git a/src/util.py b/src/util.py index b2d78d4..5ab14c0 100644 --- a/src/util.py +++ b/src/util.py @@ -1,10 +1,13 @@ from importlib.machinery import SourceFileLoader -from os import system, makedirs, popen -from os.path import dirname +from os import system, makedirs, popen, remove +from os.path import dirname, exists +from tqdm.auto import tqdm +from pathlib import Path import requests import zipfile +import shutil -from src.log import error +from src.log import error, log import src.constants @@ -46,6 +49,35 @@ def flatpak_install(packages: str) -> None: system(f"flatpak install -y {packages}") +def appimage_install(file_url: str, file_name: str) -> None: + """ + Install app by downloading .AppImage file to ~/Applications + """ + + download_path = f"{Path.home()}/Applications/{file_name}.AppImage" + + # install AppImageLauncher if it's not installed already + if system("command -v AppImageLauncher &> /dev/null"): + paru_install("appimagelauncher") + + log(f" Downloading AppImage file to {download_path}") + log(f" URL: {file_url}") + + Path(download_path).parent.mkdir(parents=True, exist_ok=True) + + if exists(download_path): + remove(download_path) + + with requests.get(file_url, stream=True) as r: + total_length = int(r.headers.get("Content-Length")) + + # show progress bar + with tqdm.wrapattr(r.raw, "read", total=total_length, desc="") as raw: + # save to file + with open(download_path, "wb") as output: + shutil.copyfileobj(raw, output) + + def smart_mkdir(path: str) -> None: """ Recursively create directories if it doesn't exist already.