diff --git a/fastanime/Utility/utils.py b/fastanime/Utility/utils.py index c904563..20b6868 100644 --- a/fastanime/Utility/utils.py +++ b/fastanime/Utility/utils.py @@ -11,6 +11,13 @@ if TYPE_CHECKING: logger = logging.getLogger(__name__) +def sort_by_episode_number(filename: str): + import re + + match = re.search(r"\d+", filename) + return int(match.group()) if match else 0 + + def anime_title_percentage_match( possible_user_requested_anime_title: str, anime: "AnilistBaseMediaDataSchema" ) -> float: diff --git a/fastanime/cli/commands/downloads.py b/fastanime/cli/commands/downloads.py index 587dd82..7e2f778 100644 --- a/fastanime/cli/commands/downloads.py +++ b/fastanime/cli/commands/downloads.py @@ -27,6 +27,7 @@ def downloads(config: "Config", path: bool, view_episodes, ffmpegthumbnailer_see from ...cli.utils.mpv import run_mpv from ...libs.fzf import fzf from ...libs.rofi import Rofi + from ...Utility.utils import sort_by_episode_number from ..utils.tools import exit_app from ..utils.utils import fuzzy_inquirer @@ -39,7 +40,9 @@ def downloads(config: "Config", path: bool, view_episodes, ffmpegthumbnailer_see if not os.path.exists(USER_VIDEOS_DIR): print("Downloads directory specified does not exist") return - anime_downloads = sorted(os.listdir(USER_VIDEOS_DIR)) + anime_downloads = sorted( + os.listdir(USER_VIDEOS_DIR), + ) anime_downloads.append("Exit") def create_thumbnails(video_path, anime_title, downloads_thumbnail_cache_dir): @@ -99,7 +102,9 @@ def downloads(config: "Config", path: bool, view_episodes, ffmpegthumbnailer_see anime_path = os.path.join(USER_VIDEOS_DIR, anime_title) if not os.path.isdir(anime_path): continue - playlist = sorted(os.listdir(anime_path)) + playlist = sorted( + os.listdir(anime_path), + ) if playlist: # actual link to download image from video_path = os.path.join(anime_path, playlist[0]) @@ -166,7 +171,9 @@ def downloads(config: "Config", path: bool, view_episodes, ffmpegthumbnailer_see # anime_playlist_path = os.path.join(USER_VIDEOS_DIR, anime_playlist_path) if not os.path.isdir(anime_playlist_path): return - anime_episodes = sorted(os.listdir(anime_playlist_path)) + anime_episodes = sorted( + os.listdir(anime_playlist_path), key=sort_by_episode_number + ) with concurrent.futures.ThreadPoolExecutor(max_workers=workers) as executor: # load the jobs future_to_url = {} @@ -223,7 +230,9 @@ def downloads(config: "Config", path: bool, view_episodes, ffmpegthumbnailer_see print(anime_playlist_path, "is not dir") exit_app(1) return - episodes = sorted(os.listdir(anime_playlist_path)) + episodes = sorted( + os.listdir(anime_playlist_path), key=sort_by_episode_number + ) downloaded_episodes = [*episodes, "Back"] if config.use_fzf: if not config.preview: