Commit f1a6981264
Unsigned
Layout: unified · split
select_anime.py +120 −99
| @@ -15,107 +15,128 @@ def load_anime_data(filename): | ||
| 15 | 15 | anime_id, anime_name = parts |
| 16 | 16 | anime_list[anime_name] = anime_id |
| 17 | 17 | return anime_list |
| 18 | anime_dict = load_anime_data("scripts/tmp/anime_list") | |
| 19 | 18 | |
| 20 | def main(stdscr): | |
| 21 | # Clear screen | |
| 22 | curses.curs_set(1) | |
| 23 | stdscr.clear() | |
| 24 | stdscr.refresh() | |
| 25 | ||
| 26 | # Initial values | |
| 27 | current_input = "" | |
| 28 | filtered_list = list(anime_dict.keys()) | |
| 29 | selected_index = 0 | |
| 30 | scroll_position = 0 # Position to start displaying the list from | |
| 19 | def extract_anime_info(json_data): | |
| 20 | anime_info_list = [] | |
| 21 | anime_info_dict = {} | |
| 22 | ||
| 23 | # Traverse the JSON structure to get to the list of entries | |
| 24 | for list_entry in json_data['data']['MediaListCollection']['lists']: | |
| 25 | for entry in list_entry['entries']: | |
| 26 | anime = entry['media'] | |
| 27 | ||
| 28 | anime_info = { | |
| 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: | |
| 33 | while True: | |
| 34 | stdscr.clear() | |
| 112 | # Filter the anime list based on current input | |
| 113 | filtered_list = [anime for anime in anime_list.keys() if current_input.lower() in anime.lower()] | |
| 35 | 114 | |
| 36 | # Get terminal dimensions | |
| 37 | height, width = stdscr.getmaxyx() | |
| 38 | list_height = height - 2 # Leave space for input and status lines | |
| 115 | # Ensure selected index is within bounds after filtering | |
| 116 | if selected_index >= len(filtered_list): | |
| 117 | selected_index = len(filtered_list) - 1 | |
| 39 | 118 | |
| 40 | # Handle edge case where terminal window is too small | |
| 41 | if height < 5 or width < len("Search Anime: ") + len(current_input): | |
| 42 | stdscr.addstr(0, 0, "Terminal window too small!") | |
| 119 | # Refresh screen | |
| 43 | 120 | stdscr.refresh() |
| 44 | continue | |
| 45 | ||
| 46 | # Display search prompt | |
| 47 | stdscr.addstr(0, 0, "Search Anime: " + current_input) | |
| 48 | ||
| 49 | # Calculate scrolling parameters | |
| 50 | if selected_index < scroll_position: | |
| 51 | scroll_position = selected_index | |
| 52 | elif selected_index >= scroll_position + list_height: | |
| 53 | scroll_position = selected_index - list_height + 1 | |
| 54 | ||
| 55 | # Display filtered results with highlighting and scrolling | |
| 56 | for idx in range(scroll_position, min(len(filtered_list), scroll_position + list_height)): | |
| 57 | anime = filtered_list[idx] | |
| 58 | display_idx = idx - scroll_position + 1 | |
| 59 | if idx == selected_index: | |
| 60 | stdscr.addstr(display_idx, 0, anime, curses.A_REVERSE) | |
| 61 | else: | |
| 62 | stdscr.addstr(display_idx, 0, anime) | |
| 63 | ||
| 64 | # Get user input | |
| 65 | key = stdscr.getch() | |
| 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 | |
| 121 | ||
| 122 | except Exception as e: | |
| 123 | # If an exception occurs, print it after exiting curses | |
| 124 | curses.endwin() | |
| 125 | print(f"An error occurred: {e}") | |
| 126 | finally: | |
| 127 | # Ensure curses always exits cleanly | |
| 128 | curses.endwin() | |
| 129 | ||
| 130 | # Print selected anime and its ID after exiting curses mode | |
| 131 | print(f"Selected Anime: {selected_anime}, ID: {selected_id}") | |
| 132 | with open("scripts/tmp/anime", "w") as anime_name: | |
| 133 | anime_name.write(selected_anime) | |
| 134 | ||
| 135 | with open("scripts/tmp/id", "w") as id: | |
| 136 | id.write(selected_id) | |
| 137 | ||
| 138 | # Initialize curses | |
| 139 | try: | |
| 140 | curses.wrapper(main) | |
| 141 | except: | |
| 142 | pass | |