|
| 1 | +import questionary |
| 2 | +from pathlib import Path |
| 3 | +from rich.console import Console |
| 4 | +from rich.table import Table |
| 5 | +from weeb_cli.i18n import i18n |
| 6 | +from weeb_cli.ui.header import show_header |
| 7 | +from weeb_cli.services.local_library import local_library |
| 8 | +from weeb_cli.config import config |
| 9 | + |
| 10 | +console = Console() |
| 11 | + |
| 12 | +def show_library_menu(): |
| 13 | + while True: |
| 14 | + console.clear() |
| 15 | + show_header(i18n.t("menu.options.library")) |
| 16 | + |
| 17 | + virtual_count = len(local_library.get_virtual_library()) |
| 18 | + indexed_count = len(local_library.get_indexed_anime()) |
| 19 | + |
| 20 | + console.print(f"[cyan]{i18n.t('library.online_library')}:[/cyan] {virtual_count} anime") |
| 21 | + console.print(f"[cyan]{i18n.t('library.local_library')}:[/cyan] {indexed_count} anime\n") |
| 22 | + |
| 23 | + opt_online = i18n.t("library.online_library") |
| 24 | + opt_local = i18n.t("library.local_library") |
| 25 | + |
| 26 | + try: |
| 27 | + choice = questionary.select( |
| 28 | + i18n.t("library.select_type"), |
| 29 | + choices=[opt_online, opt_local], |
| 30 | + pointer=">", |
| 31 | + use_shortcuts=False |
| 32 | + ).ask() |
| 33 | + |
| 34 | + if choice is None: |
| 35 | + return |
| 36 | + |
| 37 | + if choice == opt_online: |
| 38 | + show_virtual_library() |
| 39 | + elif choice == opt_local: |
| 40 | + show_local_library() |
| 41 | + |
| 42 | + except KeyboardInterrupt: |
| 43 | + return |
| 44 | + |
| 45 | +def show_virtual_library(): |
| 46 | + while True: |
| 47 | + console.clear() |
| 48 | + show_header(i18n.t("library.online_library")) |
| 49 | + |
| 50 | + library = local_library.get_virtual_library() |
| 51 | + |
| 52 | + if not library: |
| 53 | + console.print(f"[dim]{i18n.t('library.online_empty')}[/dim]") |
| 54 | + try: |
| 55 | + input(i18n.t("common.continue_key")) |
| 56 | + except KeyboardInterrupt: |
| 57 | + pass |
| 58 | + return |
| 59 | + |
| 60 | + console.print(f"[cyan]{i18n.t('library.total_anime')}:[/cyan] {len(library)}\n") |
| 61 | + |
| 62 | + choices = [] |
| 63 | + for item in library: |
| 64 | + title = item["anime_title"] |
| 65 | + provider = item["provider_name"] |
| 66 | + year = f" ({item['year']})" if item.get("year") else "" |
| 67 | + |
| 68 | + choices.append(questionary.Choice( |
| 69 | + title=f"{title}{year} - [{provider}]", |
| 70 | + value=item |
| 71 | + )) |
| 72 | + |
| 73 | + try: |
| 74 | + selected = questionary.select( |
| 75 | + i18n.t("library.select_anime"), |
| 76 | + choices=choices, |
| 77 | + pointer=">", |
| 78 | + use_shortcuts=False |
| 79 | + ).ask() |
| 80 | + |
| 81 | + if selected is None: |
| 82 | + return |
| 83 | + |
| 84 | + open_virtual_anime(selected) |
| 85 | + |
| 86 | + except KeyboardInterrupt: |
| 87 | + return |
| 88 | + |
| 89 | +def show_local_library(): |
| 90 | + from weeb_cli.commands.downloads import show_anime_episodes |
| 91 | + |
| 92 | + while True: |
| 93 | + console.clear() |
| 94 | + show_header(i18n.t("library.local_library")) |
| 95 | + |
| 96 | + indexed = local_library.get_indexed_anime() |
| 97 | + |
| 98 | + if not indexed: |
| 99 | + console.print(f"[dim]{i18n.t('library.local_empty')}[/dim]") |
| 100 | + try: |
| 101 | + input(i18n.t("common.continue_key")) |
| 102 | + except KeyboardInterrupt: |
| 103 | + pass |
| 104 | + return |
| 105 | + |
| 106 | + console.print(f"[cyan]{i18n.t('library.total_anime')}:[/cyan] {len(indexed)}\n") |
| 107 | + |
| 108 | + choices = [] |
| 109 | + for item in indexed: |
| 110 | + title = item["title"] |
| 111 | + source = item["source_name"] |
| 112 | + ep_count = item["episode_count"] |
| 113 | + available = Path(item["folder_path"]).exists() |
| 114 | + |
| 115 | + status = "✓" if available else "✗" |
| 116 | + |
| 117 | + choices.append(questionary.Choice( |
| 118 | + title=f"{status} {title} - {ep_count} {i18n.t('downloads.episode_short')} [{source}]", |
| 119 | + value=item |
| 120 | + )) |
| 121 | + |
| 122 | + try: |
| 123 | + selected = questionary.select( |
| 124 | + i18n.t("library.select_anime"), |
| 125 | + choices=choices, |
| 126 | + pointer=">", |
| 127 | + use_shortcuts=False |
| 128 | + ).ask() |
| 129 | + |
| 130 | + if selected is None: |
| 131 | + return |
| 132 | + |
| 133 | + anime_path = selected["folder_path"] |
| 134 | + if not Path(anime_path).exists(): |
| 135 | + console.print(f"[red]{i18n.t('library.local_not_available')}[/red]") |
| 136 | + try: |
| 137 | + input(i18n.t("common.continue_key")) |
| 138 | + except KeyboardInterrupt: |
| 139 | + pass |
| 140 | + continue |
| 141 | + |
| 142 | + anime_info = { |
| 143 | + "title": selected["title"], |
| 144 | + "path": anime_path, |
| 145 | + "episodes": local_library._scan_anime_folder(Path(anime_path)), |
| 146 | + "episode_count": selected["episode_count"], |
| 147 | + "source": selected["source_name"] |
| 148 | + } |
| 149 | + show_anime_episodes(anime_info) |
| 150 | + |
| 151 | + except KeyboardInterrupt: |
| 152 | + return |
| 153 | + |
| 154 | +def open_virtual_anime(item): |
| 155 | + from weeb_cli.commands.search.anime_details import show_anime_details |
| 156 | + from weeb_cli.providers.registry import get_provider |
| 157 | + |
| 158 | + anime_id = item["anime_id"] |
| 159 | + provider_name = item["provider_name"] |
| 160 | + current_provider = config.get("scraping_source", "") |
| 161 | + |
| 162 | + if current_provider != provider_name: |
| 163 | + console.print(f"[yellow]{i18n.t('library.switching_provider', provider=provider_name)}[/yellow]") |
| 164 | + config.set("scraping_source", provider_name) |
| 165 | + |
| 166 | + provider = get_provider(provider_name) |
| 167 | + if not provider: |
| 168 | + console.print(f"[red]{i18n.t('library.provider_not_found', provider=provider_name)}[/red]") |
| 169 | + try: |
| 170 | + input(i18n.t("common.continue_key")) |
| 171 | + except KeyboardInterrupt: |
| 172 | + pass |
| 173 | + return |
| 174 | + |
| 175 | + anime_data = { |
| 176 | + "id": anime_id, |
| 177 | + "slug": anime_id, |
| 178 | + "title": item["anime_title"], |
| 179 | + "name": item["anime_title"], |
| 180 | + "cover": item.get("cover_url"), |
| 181 | + "type": item.get("anime_type"), |
| 182 | + "year": item.get("year") |
| 183 | + } |
| 184 | + |
| 185 | + show_anime_details(anime_data) |
0 commit comments