Skip to content

Commit

Permalink
feat(cli):add quality and translation type selection
Browse files Browse the repository at this point in the history
  • Loading branch information
Benex254 committed Jun 28, 2024
1 parent 84b8bd9 commit 1ffb122
Show file tree
Hide file tree
Showing 36 changed files with 674 additions and 402 deletions.
11 changes: 7 additions & 4 deletions .pre-commit-config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,10 @@ repos:
- id: black
name: black
language_version: python3.10
- repo: https://github.com/PyCQA/bandit
rev: 1.7.9 # Update me!
hooks:
- id: bandit
# ------ TODO: re-add this -----
# - repo: https://github.com/PyCQA/bandit
# rev: 1.7.9 # Update me!
# hooks:
# - id: bandit
# args: ["-c", "pyproject.toml"]
# additional_dependencies: ["bandit[toml]"]
1 change: 1 addition & 0 deletions fa
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
#! /usr/bin/bash
cd $HOME/code/python/kivy_apps/FastAnime
poetry install
clear
poetry run fastanime $*
Expand Down
15 changes: 15 additions & 0 deletions fastanime/FastAnime/config.ini
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
[DEFAULT]
server =
continue_from_history = False
quality = 0
auto_next = True
sort_by = search match
downloads_dir = /home/benxl-85/Videos/FastAnime
translation_type = sub

[stream]

[general]

[anilist]

32 changes: 32 additions & 0 deletions fastanime/Utility/add_desktop_entry.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
import os
import shutil

from pyshortcuts import make_shortcut

from .. import ASSETS_DIR, PLATFORM


def create_desktop_shortcut():
app = "_ -m fastanime --gui"

logo = os.path.join(ASSETS_DIR, "logo.png")
if PLATFORM == "Windows":
logo = os.path.join(ASSETS_DIR, "logo.ico")
if fastanime := shutil.which("fastanime"):
app = f"{fastanime} --gui"
make_shortcut(
app,
name="FastAnime",
description="Download and watch anime",
terminal=False,
icon=logo,
executable=fastanime,
)
else:
make_shortcut(
app,
name="FastAnime",
description="Download and watch anime",
terminal=False,
icon=logo,
)
2 changes: 2 additions & 0 deletions fastanime/Utility/data.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@
}


anilist_sort_normalizer = {"search match": "SEARCH_MATCH"}

themes_available = [
"Aliceblue",
"Antiquewhite",
Expand Down
4 changes: 2 additions & 2 deletions fastanime/Utility/downloader/downloader.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@

import yt_dlp

from ... import downloads_dir
from ... import USER_DOWNLOADS_DIR
from ..show_notification import show_notification
from ..utils import sanitize_filename

Expand Down Expand Up @@ -53,7 +53,7 @@ def __init__(self):
def _download_file(self, url: str, title, custom_progress_hook, silent):
anime_title = sanitize_filename(title[0])
ydl_opts = {
"outtmpl": f"{downloads_dir}/{anime_title}/{anime_title}-episode {title[1]}.%(ext)s", # Specify the output path and template
"outtmpl": f"{USER_DOWNLOADS_DIR}/{anime_title}/{anime_title}-episode {title[1]}.%(ext)s", # Specify the output path and template
"progress_hooks": [
main_progress_hook,
custom_progress_hook,
Expand Down
67 changes: 36 additions & 31 deletions fastanime/__init__.py
Original file line number Diff line number Diff line change
@@ -1,49 +1,58 @@
import logging
import os
import sys
from platform import platform

import plyer
from rich import print
from rich.traceback import install

install()
install(show_locals=True)
# Create a logger instance
logger = logging.getLogger(__name__)

# TODO:confirm data integrity
# initiate constants
__version__ = "0.3.0"

# ----- some useful paths -----
app_dir = os.path.abspath(os.path.dirname(__file__))
data_folder = os.path.join(app_dir, "data")
configs_folder = os.path.join(app_dir, "configs")
if not os.path.exists(data_folder):
os.mkdir(data_folder)
PLATFORM = platform()
APP_NAME = "FastAnime"

# ---- app deps ----
APP_DIR = os.path.abspath(os.path.dirname(__file__))
CONFIGS_DIR = os.path.join(APP_DIR, "configs")
ASSETS_DIR = os.path.join(APP_DIR, "assets")

# ----- user configs and data -----
if PLATFORM == "windows":
APP_DATA_DIR_ = os.environ.get("LOCALAPPDATA", APP_DIR)
else:
APP_DATA_DIR_ = os.environ.get("XDG_DATA_HOME", APP_DIR)

if not APP_DATA_DIR_:
APP_DATA_DIR = os.path.join(APP_DIR, "data")
else:
APP_DATA_DIR = os.path.join(APP_DATA_DIR_, APP_NAME)

if not os.path.exists(APP_DATA_DIR):
os.mkdir(APP_DATA_DIR)

USER_DATA_PATH = os.path.join(APP_DATA_DIR, "user_data.json")
USER_CONFIG_PATH = os.path.join(APP_DATA_DIR, "config.ini")


# video dir
if vid_path := plyer.storagepath.get_videos_dir(): # type: ignore
downloads_dir = os.path.join(vid_path, "FastAnime")
if not os.path.exists(downloads_dir):
os.mkdir(downloads_dir)
USER_DOWNLOADS_DIR = os.path.join(vid_path, "FastAnime")
else:
# fallback
downloads_dir = os.path.join(app_dir, "videos")
if not os.path.exists(downloads_dir):
os.mkdir(downloads_dir)
USER_DOWNLOADS_DIR = os.path.join(APP_DIR, "videos")

user_data_path = os.path.join(data_folder, "user_data.json")
assets_folder = os.path.join(app_dir, "assets")
if not os.path.exists(USER_DOWNLOADS_DIR):
os.mkdir(USER_DOWNLOADS_DIR)


def FastAnime(gui=False, log=False):
def FastAnime(gui=False):
if "--gui" in sys.argv:
gui = True
sys.argv.remove("--gui")
if "--log" in sys.argv:
log = True
sys.argv.remove("--log")
if not log:
logger.propagate = False

else:
# Configure logging
from rich.logging import RichHandler

Expand All @@ -53,13 +62,9 @@ def FastAnime(gui=False, log=False):
datefmt="[%X]", # Use a custom date format
handlers=[RichHandler()], # Use RichHandler to format the logs
)

print(f"Hello {os.environ.get('USERNAME','User')} from the fastanime team")
if gui:
print(__name__)
from .gui.gui import run_gui
from .gui import run_gui

print("Run GUI")
run_gui()
else:
from .cli import run_cli
Expand Down
63 changes: 57 additions & 6 deletions fastanime/cli/__init__.py
Original file line number Diff line number Diff line change
@@ -1,11 +1,62 @@
import click
from rich import print

from .commands import anilist, download, search
from .. import __version__
from ..libs.anime_provider.allanime.constants import SERVERS_AVAILABLE
from ..Utility.data import anilist_sort_normalizer
from .commands.anilist import anilist
from .commands.config import configure
from .commands.download import download
from .commands.search import search
from .config import Config

commands = {"search": search, "download": download, "anilist": anilist}
commands = {
"search": search,
"download": download,
"anilist": anilist,
"config": configure,
}


@click.group(commands=commands)
def run_cli():
print("Yellow")
@click.group(commands=commands, invoke_without_command=True)
@click.version_option(__version__, "--version")
@click.option(
"-s",
"--server",
type=click.Choice(SERVERS_AVAILABLE, case_sensitive=False),
)
@click.option("-h", "--hist", type=bool)
@click.option("-q", "--quality", type=int)
@click.option("-t-t", "--translation_type")
@click.option("-a-n", "--auto-next", type=bool)
@click.option(
"-s-b",
"--sort-by",
type=click.Choice(anilist_sort_normalizer.keys()), # pyright: ignore
)
@click.option("-d", "--downloads-dir", type=click.Path())
@click.pass_context
def run_cli(
ctx: click.Context,
server,
hist,
translation_type,
quality,
auto_next,
sort_by,
downloads_dir,
):
ctx.obj = Config()
if server:
ctx.obj.server = server
if hist:
ctx.obj.continue_from_history = hist
if quality:
ctx.obj.quality = quality
if auto_next:
ctx.obj.auto_next = auto_next
if sort_by:
ctx.obj.sort_by = sort_by
if downloads_dir:
ctx.obj.downloads_dir = downloads_dir
if translation_type:
ctx.obj.translation_type = translation_type
8 changes: 5 additions & 3 deletions fastanime/cli/commands/anilist/__init__.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import click

from ...interfaces import anilist as anilist_interface
from .favourites import favourites
from .popular import popular
from .recent import recent
Expand All @@ -17,6 +18,7 @@
}


@click.group(commands=commands)
def anilist():
pass
@click.group(commands=commands, invoke_without_command=True)
@click.pass_obj
def anilist(config):
anilist_interface(config=config)
5 changes: 1 addition & 4 deletions fastanime/cli/commands/anilist/search.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
import click

from ....libs.anilist.anilist import AniList
from ...interfaces.anime_interface import anime_interface
from .utils import get_search_result


Expand All @@ -10,6 +9,4 @@
def search(title):
success, search_results = AniList.search(title)
if search_results and success:
result = get_search_result(search_results)
if result:
anime_interface(result)
get_search_result(search_results)
5 changes: 1 addition & 4 deletions fastanime/cli/commands/anilist/trending.py
Original file line number Diff line number Diff line change
@@ -1,14 +1,11 @@
import click

from ....libs.anilist.anilist import AniList
from ...interfaces.anime_interface import anime_interface
from .utils import get_search_result


@click.command()
def trending():
success, trending = AniList.get_trending()
if trending and success:
result = get_search_result(trending)
if result:
anime_interface(result)
get_search_result(trending)
8 changes: 8 additions & 0 deletions fastanime/cli/commands/config.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
import click


@click.command()
def configure():
pass

# create_desktop_shortcut()
13 changes: 11 additions & 2 deletions fastanime/cli/commands/search.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,15 @@
import click

from ..interfaces import anime_provider_


@click.command()
def search():
print("Searching")
@click.pass_obj
def search(
config,
anime_title,
):
anime_provider_(
config,
anime_title,
)
Loading

0 comments on commit 1ffb122

Please sign in to comment.