Skip to content

Commit

Permalink
feat, docs: --clear-cache should also clear cache dir, Config() s…
Browse files Browse the repository at this point in the history
…hould store cache files in the system's cache directory instead of temp, change some stuff in `mov-cli --help` messages and version bump.
  • Loading branch information
THEGOLDENPRO committed Jun 14, 2024
1 parent 56d145e commit 4016024
Show file tree
Hide file tree
Showing 4 changed files with 44 additions and 22 deletions.
2 changes: 1 addition & 1 deletion mov_cli/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,4 +5,4 @@
from .scraper import *
from .download import *

__version__ = "4.4alpha5-1"
__version__ = "4.4alpha6"
10 changes: 3 additions & 7 deletions mov_cli/cache.py
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand All @@ -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__()

Expand Down Expand Up @@ -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:

Expand Down
25 changes: 13 additions & 12 deletions mov_cli/cli/__main__.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
...

import typer
import shutil
import logging
from pathlib import Path

Expand All @@ -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",)

Expand All @@ -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,
Expand All @@ -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}")

Expand Down
29 changes: 27 additions & 2 deletions mov_cli/utils/paths.py
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down Expand Up @@ -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
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

0 comments on commit 4016024

Please sign in to comment.