-
-
Save doabell/7fc3d73a512df692b05aa92ee1ad6055 to your computer and use it in GitHub Desktop.
Fun script to list 100 idols and VAs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| # /// script | |
| # dependencies = [ | |
| # "requests", | |
| # "rich", | |
| # ] | |
| # /// | |
| # Usage: | |
| # Install `uv`: https://docs.astral.sh/uv/getting-started/installation/ | |
| # curl -LsSf https://astral.sh/uv/install.sh | sh | |
| # uv run list-idols-and-vas.py | |
| import time | |
| from datetime import timedelta | |
| import requests | |
| from rich.console import Console | |
| from rich.prompt import Prompt, IntPrompt | |
| console = Console() | |
| def search_wikipedia(query): | |
| url = f"https://ja.wikipedia.org/w/rest.php/v1/search/title?q={query}&limit=9" | |
| try: | |
| response = requests.get(url, timeout=10) | |
| response.raise_for_status() | |
| return response.json().get("pages", []) | |
| except Exception as e: | |
| console.print(f"Error accessing Wikipedia API: {e}", style="bold red") | |
| return [] | |
| def format_list(results, list_type="ol"): | |
| formatted_list = "" | |
| if list_type == "ol": | |
| for index, item in enumerate(results, start=1): | |
| formatted_list += f"{index}. {item}\n" | |
| else: | |
| for item in results: | |
| formatted_list += f"- {item}\n" | |
| return formatted_list | |
| def get_valid_int_input(prompt, default=None, min_val=None, max_val=None): | |
| while True: | |
| try: | |
| # Use Rich's IntPrompt for better UI | |
| if default is not None: | |
| value = IntPrompt.ask(prompt, default=default, console=console) | |
| else: | |
| value = IntPrompt.ask(prompt, console=console) | |
| if min_val is not None and value < min_val: | |
| console.print(f"Please enter a number >= {min_val}", style="bold red") | |
| continue | |
| if max_val is not None and value > max_val: | |
| console.print(f"Please enter a number <= {max_val}", style="bold red") | |
| continue | |
| return value | |
| except ValueError: | |
| console.print("Please enter a valid number", style="bold red") | |
| def main(): | |
| try: | |
| console.print("Wikipedia List Generator", style="bold blue") | |
| size = get_valid_int_input("[cyan]Size of list?[/cyan]", default=100, min_val=1) | |
| collected_list = [] | |
| start_time = None # Initialize start_time variable | |
| i = 0 # Counter for actual items processed | |
| while i < size: | |
| try: | |
| query = Prompt.ask( | |
| f"[bold]({i + 1} of {size})[/bold] Input title (00 to finish)", | |
| default="", | |
| show_default=False, | |
| ) | |
| except Exception as e: | |
| console.print(f"[bold red]Error:[/bold red] [red]{e}[/red]") | |
| continue | |
| if not query: | |
| console.print("[yellow]Empty input, please try again.[/yellow]") | |
| continue | |
| if query == "00": | |
| console.print("[yellow]Finishing collection process[/yellow]") | |
| break | |
| # Start timing after first input title | |
| if start_time is None: | |
| start_time = time.time() | |
| console.print(f"Searching for: [italic cyan]{query}[/italic cyan]") | |
| search_results = search_wikipedia(query) | |
| if not search_results: | |
| console.print("[yellow]No results found[/yellow]") | |
| continue | |
| # If only one result, check for repeats and add it if unique | |
| if len(search_results) == 1: | |
| title = search_results[0]["title"] | |
| if title in collected_list: | |
| console.print( | |
| "[bold red]Warning:[/bold red] [red]Repeat detected! " | |
| f"'{title}' is already in your list.[/red]" | |
| ) | |
| continue | |
| collected_list.append(title) | |
| console.print( | |
| f"[bold green]Success:[/bold green] [green]Added: {title}[/green]" | |
| ) | |
| i += 1 # Increment counter only when an item is added | |
| continue | |
| console.print("\n[bold]Search Results:[/bold]") | |
| titles = [] | |
| for index, res in enumerate(search_results, start=1): | |
| title = res["title"] | |
| titles.append(title) | |
| console.print(f"[cyan]{index}.[/cyan] {title}") | |
| choice_prompt = "[yellow]Choose a result[/yellow]" | |
| choice = get_valid_int_input( | |
| choice_prompt, default=1, min_val=0, max_val=len(titles) | |
| ) | |
| if choice > 0: | |
| selected_title = titles[choice - 1] | |
| if selected_title in collected_list: | |
| console.print( | |
| "[bold yellow]Warning:[/bold yellow] Repeat detected! " | |
| f"'{selected_title}' is already in your list." | |
| ) | |
| else: | |
| collected_list.append(selected_title) | |
| console.print( | |
| f"[bold green]Success:[/bold green] [green]Added: {selected_title}[/green]" | |
| ) | |
| i += 1 # Increment counter only when an item is added | |
| # If choice is 0, do not increment i (skip without counting) | |
| except KeyboardInterrupt: | |
| console.print("[yellow]Operation cancelled by user.[/yellow]") | |
| except Exception as e: | |
| console.print( | |
| f"[bold red]Error:[/bold red] [red]An unexpected error occurred: {e}[/red]" | |
| ) | |
| finally: | |
| if start_time is not None: # Only calculate elapsed time if timing was started | |
| end_time = time.time() | |
| elapsed_time = end_time - start_time | |
| formatted_time = str(timedelta(seconds=int(elapsed_time))) | |
| console.print( | |
| f"[cyan]Total time elapsed: [bold]{formatted_time}[/bold] (HH:MM:SS)[/cyan]" | |
| ) | |
| console.print( | |
| f"[cyan]Items collected: [bold]{len(collected_list)}[/bold][/cyan]" | |
| ) | |
| if collected_list: | |
| list_format = Prompt.ask( | |
| "[cyan]List format?[/cyan] (0 for ul, 1 for ol)", | |
| choices=["0", "1"], | |
| default="1", | |
| ) | |
| list_type = "ul" if list_format == "0" else "ol" | |
| console.print( | |
| "\n[bold green]Collected List in Markdown format:[/bold green]" | |
| ) | |
| markdown_text = format_list(collected_list, list_type) | |
| console.print(f"[green]{markdown_text}[/green]") | |
| else: | |
| console.print("[yellow]No items collected.[/yellow]") | |
| if __name__ == "__main__": | |
| main() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment