From 40160246da23ec311b9446d1fd9b6a85400f9a11 Mon Sep 17 00:00:00 2001 From: Goldy <66202304+THEGOLDENPRO@users.noreply.github.com> Date: Fri, 14 Jun 2024 18:44:39 +0100 Subject: [PATCH] feat, docs: `--clear-cache` should also clear cache dir, `Config()` should store cache files in the system's cache directory instead of temp, change some stuff in `mov-cli --help` messages and version bump. --- mov_cli/__init__.py | 2 +- mov_cli/cache.py | 10 +++------- mov_cli/cli/__main__.py | 25 +++++++++++++------------ mov_cli/utils/paths.py | 29 +++++++++++++++++++++++++++-- 4 files changed, 44 insertions(+), 22 deletions(-) diff --git a/mov_cli/__init__.py b/mov_cli/__init__.py index 2da80716..00ab759a 100644 --- a/mov_cli/__init__.py +++ b/mov_cli/__init__.py @@ -5,4 +5,4 @@ from .scraper import * from .download import * -__version__ = "4.4alpha5-1" \ No newline at end of file +__version__ = "4.4alpha6" \ No newline at end of file diff --git a/mov_cli/cache.py b/mov_cli/cache.py index ed09e937..603606c7 100644 --- a/mov_cli/cache.py +++ b/mov_cli/cache.py @@ -20,7 +20,7 @@ class BasicCacheData(TypedDict): from devgoldyutils import LoggerAdapter, Colours from .logger import mov_cli_logger -from .utils import get_temp_directory +from .utils import get_temp_directory, get_cache_directory __all__ = ( "Cache", @@ -35,9 +35,9 @@ class Cache(): def __init__(self, platform: SUPPORTED_PLATFORMS, section: Optional[str] = None) -> None: self.section = section - self.temp_dir = get_temp_directory(platform) + cache_dir = get_cache_directory(platform) - self._basic_cache_file_path = self.temp_dir.joinpath("osaka_cache") # ◔_◔ https://static.wikia.nocookie.net/parody/images/f/fd/Osaka.png/revision/latest + self._basic_cache_file_path = cache_dir.joinpath("osaka_cache") # ◔_◔ https://static.wikia.nocookie.net/parody/images/f/fd/Osaka.png/revision/latest super().__init__() @@ -154,10 +154,6 @@ def clear_all_cache(self) -> None: def delete_cache_file(self) -> None: logger.info(f"Deleting basic cache file ({self._basic_cache_file_path.name})...") self._basic_cache_file_path.unlink(True) - - def delete_entire_folder(self) -> None: - logger.info("Deleting mov-cli's temp folder...") - shutil.rmtree(self.temp_dir) def __get_cache_file(self, mode: str) -> TextIOWrapper: diff --git a/mov_cli/cli/__main__.py b/mov_cli/cli/__main__.py index 77237224..eaaa337d 100644 --- a/mov_cli/cli/__main__.py +++ b/mov_cli/cli/__main__.py @@ -5,6 +5,7 @@ ... import typer +import shutil import logging from pathlib import Path @@ -21,8 +22,7 @@ from ..media import MetadataType from ..logger import mov_cli_logger from ..http_client import HTTPClient -from ..utils import hide_ip, what_platform -from .. import Cache +from ..utils import hide_ip, get_temp_directory, what_platform, get_cache_directory __all__ = ("mov_cli",) @@ -32,22 +32,23 @@ def mov_cli( query: Optional[List[str]] = typer.Argument(None, help = "A film, tv show or anime you would like to Query."), debug: Optional[bool] = typer.Option(None, help = "Enable extra logging details. Useful for bug reporting."), player: Optional[str] = typer.Option(None, "--player", "-p", help = "Player you would like to stream with. E.g. mpv, vlc"), - scraper: Optional[str] = typer.Option(None, "--scraper", "-s", help = "Scraper you would like to scrape with. E.g. remote_stream, sflix"), + scraper: Optional[str] = typer.Option(None, "--scraper", "-s", help = "Scraper you would like to scrape with. E.g. test, youtube, jellyplex"), fzf: Optional[bool] = typer.Option(None, help = "Toggle fzf on/off for all user selection prompts."), preview: Optional[bool] = typer.Option(None, help = "Toggle fzf's preview (image preview, etc) on/off for fzf prompts."), episode: Optional[str] = typer.Option(None, "--episode", "-ep", help = "Episode and season you wanna scrape. E.g. {episode}:{season} like -> 26:3"), auto_select: Optional[int] = typer.Option(None, "--choice", "-c", help = "Auto select the search results. E.g. Setting it to 1 with query 'nyan cat' will pick " \ "the first nyan cat video to show up in search results." - ), - limit: Optional[int] = typer.Option(None, "--limit", "-l", help = "Specify the maximum number of results"), + ), + limit: Optional[int] = typer.Option(None, "--limit", "-l", help = "Specify the maximum number of results"), version: bool = typer.Option(False, "--version", help = "Display what version mov-cli is currently on."), edit: bool = typer.Option(False, "--edit", "-e", help = "Opens the mov-cli config with your respective editor."), - download: bool = typer.Option(False, "--download", "-d", help = "Downloads the media instead of playing."), - list_plugins: bool = typer.Option(False, "--list-plugins", "-lp", help = "Prints all configured plugins and their scrapers."), - clear_cache: bool = typer.Option(False, "--clear-cache", help = "Clears the mov-cli's temp folder.") + download: bool = typer.Option(False, "--download", "-d", help = "Downloads the media instead of playing."), + list_plugins: bool = typer.Option(False, "--list-plugins", "-lp", help = "Prints all configured plugins and their scrapers."), + clear_cache: bool = typer.Option(False, "--no-cache", "--clear-cache", help = "Clears ALL cache stored by mov-cli, including the temp directory cache.") ): config = Config() + platform = what_platform() config = set_cli_config( config, @@ -61,11 +62,11 @@ def mov_cli( if config.debug: mov_cli_logger.setLevel(logging.DEBUG) - + if clear_cache: - Cache( - what_platform() - ).delete_entire_folder() + mov_cli_logger.info("Clearing cache...") + shutil.rmtree(get_temp_directory(platform)) + shutil.rmtree(get_cache_directory(platform)) mov_cli_logger.debug(f"Config -> {config.data}") diff --git a/mov_cli/utils/paths.py b/mov_cli/utils/paths.py index 142718a3..1281c5b4 100644 --- a/mov_cli/utils/paths.py +++ b/mov_cli/utils/paths.py @@ -9,7 +9,8 @@ __all__ = ( "get_appdata_directory", - "get_temp_directory" + "get_temp_directory", + "get_cache_directory" ) def get_appdata_directory(platform: SUPPORTED_PLATFORMS) -> Path: @@ -68,4 +69,28 @@ def get_temp_directory(platform: SUPPORTED_PLATFORMS) -> Path: temp_directory = temp_directory.joinpath("mov-cli-temp") temp_directory.mkdir(exist_ok = True) - return temp_directory \ No newline at end of file + return temp_directory + +def get_cache_directory(platform: SUPPORTED_PLATFORMS) -> Path: + """ + Returns the cache directory where mov-cli can dump longer lived cache. + """ + cache_directory = None + + if platform == "Windows": + cache_directory = Path(os.getenv("LOCALAPPDATA")) + + elif platform == "Darwin": # NOTE: Path maybe incorrect + user_profile = Path.home() + cache_directory = user_profile.joinpath("Library", "Caches") + + elif platform == "Linux" or platform == "Android" or platform == "iOS": + user_profile = Path(os.getenv("HOME")) + cache_directory = user_profile.joinpath(".cache") + + cache_directory.mkdir(exist_ok = True) # on android the .cache file may not exist. + + cache_directory = cache_directory.joinpath("mov-cli") + cache_directory.mkdir(exist_ok = True) + + return cache_directory \ No newline at end of file