Commit f1a6981264
Unsigned
Layout: unified · split
select_anime.py +120 −99
| @@ -15,107 +15,128 @@ def load_anime_data(filename): | |||
| 15 | anime_id, anime_name = parts | 15 | anime_id, anime_name = parts |
| 16 | anime_list[anime_name] = anime_id | 16 | anime_list[anime_name] = anime_id |
| 17 | return anime_list | 17 | return anime_list |
| 18 | anime_dict = load_anime_data("scripts/tmp/anime_list") | ||
| 19 | 18 | ||
| 20 | def main(stdscr): | 19 | def extract_anime_info(json_data): |
| 21 | # Clear screen | 20 | anime_info_list = [] |
| 22 | curses.curs_set(1) | 21 | anime_info_dict = {} |
| 23 | stdscr.clear() | 22 | |
| 24 | stdscr.refresh() | 23 | # Traverse the JSON structure to get to the list of entries |
| 25 | 24 | for list_entry in json_data['data']['MediaListCollection']['lists']: | |
| 26 | # Initial values | 25 | for entry in list_entry['entries']: |
| 27 | current_input = "" | 26 | anime = entry['media'] |
| 28 | filtered_list = list(anime_dict.keys()) | 27 | |
| 29 | selected_index = 0 | 28 | anime_info = { |
| 30 | scroll_position = 0 # Position to start displaying the list from | 29 | 'id': anime['id'], |
| 30 | 'romaji_name': anime['title']['romaji'], | ||
| 31 | 'english_name': anime['title']['english'], | ||
| 32 | 'status': entry['status'] | ||
| 33 | } | ||
| 34 | |||
| 35 | anime_info_dict[anime['title']['english']] = str(anime['id']) # {anime_name: anime_id, ...} | ||
| 36 | anime_info_list.append(anime_info) # [{"id": anime_id, "romaji_name": anime_name_romnaji, "english_name": anime_name_english, status: anime_status}, ...] | ||
| 37 | |||
| 38 | return [anime_info_dict, anime_info_list] | ||
| 39 | |||
| 40 | def select_anime(anime_list): | ||
| 41 | def main(stdscr): | ||
| 42 | # Clear screen | ||
| 43 | curses.curs_set(1) | ||
| 44 | stdscr.clear() | ||
| 45 | stdscr.refresh() | ||
| 46 | |||
| 47 | # Initial values | ||
| 48 | current_input = "" | ||
| 49 | filtered_list = list(anime_list.keys()) | ||
| 50 | selected_index = 0 | ||
| 51 | scroll_position = 0 # Position to start displaying the list from | ||
| 52 | |||
| 53 | try: | ||
| 54 | while True: | ||
| 55 | stdscr.clear() | ||
| 56 | |||
| 57 | # Get terminal dimensions | ||
| 58 | height, width = stdscr.getmaxyx() | ||
| 59 | list_height = height - 2 # Leave space for input and status lines | ||
| 60 | |||
| 61 | # Handle edge case where terminal window is too small | ||
| 62 | if height < 5 or width < len("Search Anime: ") + len(current_input): | ||
| 63 | stdscr.addstr(0, 0, "Terminal window too small!") | ||
| 64 | stdscr.refresh() | ||
| 65 | continue | ||
| 66 | |||
| 67 | # Display search prompt | ||
| 68 | stdscr.addstr(0, 0, "Search Anime: " + current_input) | ||
| 69 | |||
| 70 | # Calculate scrolling parameters | ||
| 71 | if selected_index < scroll_position: | ||
| 72 | scroll_position = selected_index | ||
| 73 | elif selected_index >= scroll_position + list_height: | ||
| 74 | scroll_position = selected_index - list_height + 1 | ||
| 75 | |||
| 76 | # Display filtered results with highlighting and scrolling | ||
| 77 | for idx in range(scroll_position, min(len(filtered_list), scroll_position + list_height)): | ||
| 78 | anime = filtered_list[idx] | ||
| 79 | display_idx = idx - scroll_position + 1 | ||
| 80 | if idx == selected_index: | ||
| 81 | stdscr.addstr(display_idx, 0, anime, curses.A_REVERSE) | ||
| 82 | else: | ||
| 83 | stdscr.addstr(display_idx, 0, anime) | ||
| 84 | |||
| 85 | # Get user input | ||
| 86 | key = stdscr.getch() | ||
| 87 | |||
| 88 | # Handle key inputs | ||
| 89 | if key in [curses.KEY_BACKSPACE, 127, 8]: | ||
| 90 | current_input = current_input[:-1] | ||
| 91 | selected_index = 0 | ||
| 92 | elif key == curses.KEY_DOWN: | ||
| 93 | if selected_index < len(filtered_list) - 1: | ||
| 94 | selected_index += 1 | ||
| 95 | elif key == curses.KEY_UP: | ||
| 96 | if selected_index > 0: | ||
| 97 | selected_index -= 1 | ||
| 98 | elif key == curses.KEY_ENTER or key in [10, 13]: # Enter key | ||
| 99 | if filtered_list: | ||
| 100 | selected_anime = filtered_list[selected_index] | ||
| 101 | selected_id = anime_list[selected_anime] | ||
| 102 | break | ||
| 103 | elif key == ord('q'): # Exit if 'q' is pressed | ||
| 104 | return | ||
| 105 | else: | ||
| 106 | try: | ||
| 107 | current_input += chr(key) | ||
| 108 | selected_index = 0 | ||
| 109 | except: | ||
| 110 | pass | ||
| 31 | 111 | ||
| 32 | try: | 112 | # Filter the anime list based on current input |
| 33 | while True: | 113 | filtered_list = [anime for anime in anime_list.keys() if current_input.lower() in anime.lower()] |
| 34 | stdscr.clear() | ||
| 35 | 114 | ||
| 36 | # Get terminal dimensions | 115 | # Ensure selected index is within bounds after filtering |
| 37 | height, width = stdscr.getmaxyx() | 116 | if selected_index >= len(filtered_list): |
| 38 | list_height = height - 2 # Leave space for input and status lines | 117 | selected_index = len(filtered_list) - 1 |
| 39 | 118 | ||
| 40 | # Handle edge case where terminal window is too small | 119 | # Refresh screen |
| 41 | if height < 5 or width < len("Search Anime: ") + len(current_input): | ||
| 42 | stdscr.addstr(0, 0, "Terminal window too small!") | ||
| 43 | stdscr.refresh() | 120 | stdscr.refresh() |
| 44 | continue | 121 | |
| 45 | 122 | except Exception as e: | |
| 46 | # Display search prompt | 123 | # If an exception occurs, print it after exiting curses |
| 47 | stdscr.addstr(0, 0, "Search Anime: " + current_input) | 124 | curses.endwin() |
| 48 | 125 | print(f"An error occurred: {e}") | |
| 49 | # Calculate scrolling parameters | 126 | finally: |
| 50 | if selected_index < scroll_position: | 127 | # Ensure curses always exits cleanly |
| 51 | scroll_position = selected_index | 128 | curses.endwin() |
| 52 | elif selected_index >= scroll_position + list_height: | 129 | |
| 53 | scroll_position = selected_index - list_height + 1 | 130 | # Print selected anime and its ID after exiting curses mode |
| 54 | 131 | print(f"Selected Anime: {selected_anime}, ID: {selected_id}") | |
| 55 | # Display filtered results with highlighting and scrolling | 132 | with open("scripts/tmp/anime", "w") as anime_name: |
| 56 | for idx in range(scroll_position, min(len(filtered_list), scroll_position + list_height)): | 133 | anime_name.write(selected_anime) |
| 57 | anime = filtered_list[idx] | 134 | |
| 58 | display_idx = idx - scroll_position + 1 | 135 | with open("scripts/tmp/id", "w") as id: |
| 59 | if idx == selected_index: | 136 | id.write(selected_id) |
| 60 | stdscr.addstr(display_idx, 0, anime, curses.A_REVERSE) | 137 | |
| 61 | else: | 138 | # Initialize curses |
| 62 | stdscr.addstr(display_idx, 0, anime) | 139 | try: |
| 63 | 140 | curses.wrapper(main) | |
| 64 | # Get user input | 141 | except: |
| 65 | key = stdscr.getch() | 142 | pass |
| 66 | |||
| 67 | # Handle key inputs | ||
| 68 | if key in [curses.KEY_BACKSPACE, 127, 8]: | ||
| 69 | current_input = current_input[:-1] | ||
| 70 | selected_index = 0 | ||
| 71 | elif key == curses.KEY_DOWN: | ||
| 72 | if selected_index < len(filtered_list) - 1: | ||
| 73 | selected_index += 1 | ||
| 74 | elif key == curses.KEY_UP: | ||
| 75 | if selected_index > 0: | ||
| 76 | selected_index -= 1 | ||
| 77 | elif key == curses.KEY_ENTER or key in [10, 13]: # Enter key | ||
| 78 | if filtered_list: | ||
| 79 | selected_anime = filtered_list[selected_index] | ||
| 80 | selected_id = anime_dict[selected_anime] | ||
| 81 | break | ||
| 82 | elif key == ord('q'): # Exit if 'q' is pressed | ||
| 83 | return | ||
| 84 | else: | ||
| 85 | try: | ||
| 86 | current_input += chr(key) | ||
| 87 | selected_index = 0 | ||
| 88 | except: | ||
| 89 | pass | ||
| 90 | |||
| 91 | # Filter the anime list based on current input | ||
| 92 | filtered_list = [anime for anime in anime_dict.keys() if current_input.lower() in anime.lower()] | ||
| 93 | |||
| 94 | # Ensure selected index is within bounds after filtering | ||
| 95 | if selected_index >= len(filtered_list): | ||
| 96 | selected_index = len(filtered_list) - 1 | ||
| 97 | |||
| 98 | # Refresh screen | ||
| 99 | stdscr.refresh() | ||
| 100 | |||
| 101 | except Exception as e: | ||
| 102 | # If an exception occurs, print it after exiting curses | ||
| 103 | curses.endwin() | ||
| 104 | print(f"An error occurred: {e}") | ||
| 105 | finally: | ||
| 106 | # Ensure curses always exits cleanly | ||
| 107 | curses.endwin() | ||
| 108 | |||
| 109 | # Print selected anime and its ID after exiting curses mode | ||
| 110 | print(f"Selected Anime: {selected_anime}, ID: {selected_id}") | ||
| 111 | with open("scripts/tmp/anime", "w") as anime_name: | ||
| 112 | anime_name.write(selected_anime) | ||
| 113 | |||
| 114 | with open("scripts/tmp/id", "w") as id: | ||
| 115 | id.write(selected_id) | ||
| 116 | |||
| 117 | # Initialize curses | ||
| 118 | try: | ||
| 119 | curses.wrapper(main) | ||
| 120 | except: | ||
| 121 | pass | ||