From 1291d55ab0432ca472e831eb3c41f08cf5a06e4a Mon Sep 17 00:00:00 2001 From: Benex254 Date: Fri, 16 Aug 2024 11:38:18 +0300 Subject: [PATCH] feat(downloads command): add previews --- fastanime/cli/commands/downloads.py | 98 +++++++++++++++++++++++++++-- fastanime/cli/interfaces/utils.py | 80 +---------------------- fastanime/cli/utils/scripts.py | 78 +++++++++++++++++++++++ 3 files changed, 172 insertions(+), 84 deletions(-) create mode 100644 fastanime/cli/utils/scripts.py diff --git a/fastanime/cli/commands/downloads.py b/fastanime/cli/commands/downloads.py index 7a392ad..5d9ad8e 100644 --- a/fastanime/cli/commands/downloads.py +++ b/fastanime/cli/commands/downloads.py @@ -12,6 +12,7 @@ if TYPE_CHECKING: @click.option("--path", "-p", help="print the downloads folder and exit", is_flag=True) @click.pass_obj def downloads(config: "Config", path: bool): + import logging import os from ...cli.utils.mpv import run_mpv @@ -20,6 +21,8 @@ def downloads(config: "Config", path: bool): from ..utils.tools import exit_app from ..utils.utils import fuzzy_inquirer + logger = logging.getLogger(__name__) + USER_VIDEOS_DIR = config.downloads_dir if path: print(USER_VIDEOS_DIR) @@ -27,17 +30,102 @@ def downloads(config: "Config", path: bool): if not os.path.exists(USER_VIDEOS_DIR): print("Downloads directory specified does not exist") return - playlists = os.listdir(USER_VIDEOS_DIR) - playlists.append("Exit") + anime_downloads = os.listdir(USER_VIDEOS_DIR) + anime_downloads.append("Exit") + + def create_thumbnails(video_path, anime_title, downloads_thumbnail_cache_dir): + import os + import shutil + import subprocess + + FFMPEG_THUMBNAILER = shutil.which("ffmpegthumbnailer") + if not FFMPEG_THUMBNAILER: + return + + out = os.path.join(downloads_thumbnail_cache_dir, anime_title) + completed_process = subprocess.run( + [FFMPEG_THUMBNAILER, "-i", video_path, "-o", out], stderr=subprocess.PIPE + ) + if completed_process.returncode == 0: + logger.info(f"Success in creating {anime_title} thumbnail") + else: + logger.warn(f"Failed in creating {anime_title} thumbnail") + + def get_previews(workers=None): + import concurrent.futures + import shutil + from pathlib import Path + + if not shutil.which("ffmpegthumbnailer"): + print("ffmpegthumbnailer not found") + logger.error("ffmpegthumbnailer not found") + return + + from ...constants import APP_CACHE_DIR + from ..utils.scripts import fzf_preview + + downloads_thumbnail_cache_dir = os.path.join(APP_CACHE_DIR, "video_thumbnails") + Path(downloads_thumbnail_cache_dir).mkdir(parents=True, exist_ok=True) + # use concurrency to download the images as fast as possible + with concurrent.futures.ThreadPoolExecutor(max_workers=workers) as executor: + # load the jobs + future_to_url = {} + for anime_title in anime_downloads: + anime_path = os.path.join(USER_VIDEOS_DIR, anime_title) + if not os.path.isdir(anime_path): + continue + playlist = os.listdir(anime_path) + if playlist: + # actual link to download image from + video_path = os.path.join(anime_path, playlist[0]) + future_to_url[ + executor.submit( + create_thumbnails, + video_path, + anime_title, + downloads_thumbnail_cache_dir, + ) + ] = anime_title + + # execute the jobs + for future in concurrent.futures.as_completed(future_to_url): + url = future_to_url[future] + try: + future.result() + except Exception as e: + logger.error("%r generated an exception: %s" % (url, e)) + os.environ["SHELL"] = shutil.which("bash") or "bash" + preview = """ + %s + if [ -s %s/{} ]; then fzf-preview %s/{} + else echo Loading... + fi + """ % ( + fzf_preview, + downloads_thumbnail_cache_dir, + downloads_thumbnail_cache_dir, + ) + return preview def stream(): if config.use_fzf: - playlist_name = fzf.run(playlists, "Enter Playlist Name", "Downloads") + if not config.preview: + playlist_name = fzf.run( + anime_downloads, + "Enter Playlist Name", + ) + else: + preview = get_previews() + playlist_name = fzf.run( + anime_downloads, + "Enter Playlist Name", + preview=preview, + ) elif config.use_rofi: - playlist_name = Rofi.run(playlists, "Enter Playlist Name") + playlist_name = Rofi.run(anime_downloads, "Enter Playlist Name") else: playlist_name = fuzzy_inquirer( - playlists, + anime_downloads, "Enter Playlist Name: ", ) if playlist_name == "Exit": diff --git a/fastanime/cli/interfaces/utils.py b/fastanime/cli/interfaces/utils.py index 436e310..1f031c9 100644 --- a/fastanime/cli/interfaces/utils.py +++ b/fastanime/cli/interfaces/utils.py @@ -12,89 +12,11 @@ from yt_dlp.utils import clean_html from ...constants import APP_CACHE_DIR from ...libs.anilist.types import AnilistBaseMediaDataSchema from ...Utility import anilist_data_helper +from ..utils.scripts import fzf_preview from ..utils.utils import get_true_fg logger = logging.getLogger(__name__) -# this script was written by the fzf devs as an example on how to preview images -# its only here for convinience -fzf_preview = r""" -# -# The purpose of this script is to demonstrate how to preview a file or an -# image in the preview window of fzf. -# -# Dependencies: -# - https://github.com/sharkdp/bat -# - https://github.com/hpjansson/chafa -# - https://iterm2.com/utilities/imgcat -fzf-preview(){ - if [[ $# -ne 1 ]]; then - >&2 echo "usage: $0 FILENAME" - exit 1 - fi - - file=${1/#\~\//$HOME/} - type=$(file --dereference --mime -- "$file") - - if [[ ! $type =~ image/ ]]; then - if [[ $type =~ =binary ]]; then - file "$1" - exit - fi - - # Sometimes bat is installed as batcat. - if command -v batcat > /dev/null; then - batname="batcat" - elif command -v bat > /dev/null; then - batname="bat" - else - cat "$1" - exit - fi - - ${batname} --style="${BAT_STYLE:-numbers}" --color=always --pager=never -- "$file" - exit - fi - - dim=${FZF_PREVIEW_COLUMNS}x${FZF_PREVIEW_LINES} - if [[ $dim = x ]]; then - dim=$(stty size < /dev/tty | awk '{print $2 "x" $1}') - elif ! [[ $KITTY_WINDOW_ID ]] && (( FZF_PREVIEW_TOP + FZF_PREVIEW_LINES == $(stty size < /dev/tty | awk '{print $1}') )); then - # Avoid scrolling issue when the Sixel image touches the bottom of the screen - # * https://github.com/junegunn/fzf/issues/2544 - dim=${FZF_PREVIEW_COLUMNS}x$((FZF_PREVIEW_LINES - 1)) - fi - -# 1. Use kitty icat on kitty terminal - if [[ $KITTY_WINDOW_ID ]]; then - # 1. 'memory' is the fastest option but if you want the image to be scrollable, - # you have to use 'stream'. - # - # 2. The last line of the output is the ANSI reset code without newline. - # This confuses fzf and makes it render scroll offset indicator. - # So we remove the last line and append the reset code to its previous line. - kitty icat --clear --transfer-mode=memory --unicode-placeholder --stdin=no --place="$dim@0x0" "$file" | sed '$d' | sed $'$s/$/\e[m/' - -# 2. Use chafa with Sixel output - elif command -v chafa > /dev/null; then - chafa -f sixel -s "$dim" "$file" - # Add a new line character so that fzf can display multiple images in the preview window - echo - -# 3. If chafa is not found but imgcat is available, use it on iTerm2 - elif command -v imgcat > /dev/null; then - # NOTE: We should use https://iterm2.com/utilities/it2check to check if the - # user is running iTerm2. But for the sake of simplicity, we just assume - # that's the case here. - imgcat -W "${dim%%x*}" -H "${dim##*x}" "$file" - -# 4. Cannot find any suitable method to preview the image - else - file "$file" - fi -} -""" - # ---- aniskip intergration ---- def aniskip(mal_id: int, episode: str): diff --git a/fastanime/cli/utils/scripts.py b/fastanime/cli/utils/scripts.py new file mode 100644 index 0000000..7151038 --- /dev/null +++ b/fastanime/cli/utils/scripts.py @@ -0,0 +1,78 @@ +# this script was written by the fzf devs as an example on how to preview images +# its only here for convinience +fzf_preview = r""" +# +# The purpose of this script is to demonstrate how to preview a file or an +# image in the preview window of fzf. +# +# Dependencies: +# - https://github.com/sharkdp/bat +# - https://github.com/hpjansson/chafa +# - https://iterm2.com/utilities/imgcat +fzf-preview(){ + if [[ $# -ne 1 ]]; then + >&2 echo "usage: $0 FILENAME" + exit 1 + fi + + file=${1/#\~\//$HOME/} + type=$(file --dereference --mime -- "$file") + + if [[ ! $type =~ image/ ]]; then + if [[ $type =~ =binary ]]; then + file "$1" + exit + fi + + # Sometimes bat is installed as batcat. + if command -v batcat > /dev/null; then + batname="batcat" + elif command -v bat > /dev/null; then + batname="bat" + else + cat "$1" + exit + fi + + ${batname} --style="${BAT_STYLE:-numbers}" --color=always --pager=never -- "$file" + exit + fi + + dim=${FZF_PREVIEW_COLUMNS}x${FZF_PREVIEW_LINES} + if [[ $dim = x ]]; then + dim=$(stty size < /dev/tty | awk '{print $2 "x" $1}') + elif ! [[ $KITTY_WINDOW_ID ]] && (( FZF_PREVIEW_TOP + FZF_PREVIEW_LINES == $(stty size < /dev/tty | awk '{print $1}') )); then + # Avoid scrolling issue when the Sixel image touches the bottom of the screen + # * https://github.com/junegunn/fzf/issues/2544 + dim=${FZF_PREVIEW_COLUMNS}x$((FZF_PREVIEW_LINES - 1)) + fi + +# 1. Use kitty icat on kitty terminal + if [[ $KITTY_WINDOW_ID ]]; then + # 1. 'memory' is the fastest option but if you want the image to be scrollable, + # you have to use 'stream'. + # + # 2. The last line of the output is the ANSI reset code without newline. + # This confuses fzf and makes it render scroll offset indicator. + # So we remove the last line and append the reset code to its previous line. + kitty icat --clear --transfer-mode=memory --unicode-placeholder --stdin=no --place="$dim@0x0" "$file" | sed '$d' | sed $'$s/$/\e[m/' + +# 2. Use chafa with Sixel output + elif command -v chafa > /dev/null; then + chafa -f sixel -s "$dim" "$file" + # Add a new line character so that fzf can display multiple images in the preview window + echo + +# 3. If chafa is not found but imgcat is available, use it on iTerm2 + elif command -v imgcat > /dev/null; then + # NOTE: We should use https://iterm2.com/utilities/it2check to check if the + # user is running iTerm2. But for the sake of simplicity, we just assume + # that's the case here. + imgcat -W "${dim%%x*}" -H "${dim##*x}" "$file" + +# 4. Cannot find any suitable method to preview the image + else + file "$file" + fi +} +"""