Use this to search through the query params of any URL.
You can run this script directly, or save it as an alias in your bash or zsh config:
kvp_parse() {
~/location/of/script/urlrepl.py
}| #!/usr/bin/env python3 | |
| import urllib.parse | |
| import re | |
| import sys | |
| import subprocess | |
| class URLSearcher: | |
| def __init__(self): | |
| self.params = {} | |
| self.raw_url = "" | |
| def load_url(self, url): | |
| self.raw_url = url.strip() | |
| self.params = {} | |
| if '?' in url: | |
| query = url.split('?', 1)[1] | |
| parsed = urllib.parse.parse_qs(query, keep_blank_values=True) | |
| for key, values in parsed.items(): | |
| for i, value in enumerate(values): | |
| param_key = f"{key}[{i}]" if len(values) > 1 else key | |
| self.params[param_key] = urllib.parse.unquote_plus(value) | |
| print(f"Loaded {len(self.params)} parameters") | |
| def search(self, term): | |
| if not self.params: | |
| print("No URL loaded. Use 'url <your_url>' first.") | |
| return | |
| # ANSI color codes | |
| HIGHLIGHT = '\033[93m\033[1m' # Yellow + Bold | |
| RESET = '\033[0m' | |
| SEPARATOR = '\033[90m' + '─' * 60 + RESET # Gray separator line | |
| matches = [] | |
| for key, value in self.params.items(): | |
| if re.search(term, key, re.IGNORECASE) or re.search(term, value, re.IGNORECASE): | |
| # Highlight matches in both key and value | |
| highlighted_key = re.sub(f'({re.escape(term)})', f'{HIGHLIGHT}\\1{RESET}', key, flags=re.IGNORECASE) | |
| highlighted_value = re.sub(f'({re.escape(term)})', f'{HIGHLIGHT}\\1{RESET}', value, flags=re.IGNORECASE) | |
| matches.append(f"{highlighted_key}={highlighted_value}") | |
| print(SEPARATOR) | |
| if matches: | |
| print(f"Found {len(matches)} match(es) for '{term}':") | |
| for i, match in enumerate(matches): | |
| print(match) | |
| if i < len(matches) - 1: # Don't print separator after last match | |
| print('\033[90m' + ' ·' + RESET) # Gray dot separator | |
| else: | |
| print(f"No matches found for '{term}'") | |
| print(SEPARATOR) | |
| def list_all(self): | |
| if not self.params: | |
| print("No URL loaded.") | |
| return | |
| for key, value in self.params.items(): | |
| print(f"{key}={value}") | |
| def main(): | |
| searcher = URLSearcher() | |
| print("URL Search REPL") | |
| print("Commands:") | |
| print(" url <url> | u <url> - Load a new URL") | |
| print(" paste | p - Load URL from clipboard") | |
| print(" search <term> | s - Search for term in keys/values") | |
| print(" list | l - Show all parameters") | |
| print(" quit | q - Exit") | |
| print() | |
| while True: | |
| try: | |
| cmd = input("urlsearch> ").strip() | |
| if cmd.lower() in ['quit', 'exit', 'q']: | |
| break | |
| elif cmd.startswith('url ') or cmd.startswith('u '): | |
| url_part = cmd[4:] if cmd.startswith('url ') else cmd[2:] | |
| searcher.load_url(url_part) | |
| elif cmd in ['paste', 'p']: | |
| try: | |
| clipboard_url = subprocess.check_output(['pbpaste'], text=True).strip() | |
| searcher.load_url(clipboard_url) | |
| except subprocess.CalledProcessError: | |
| print("Error reading clipboard") | |
| elif cmd.startswith('search ') or cmd.startswith('s '): | |
| search_term = cmd[7:] if cmd.startswith('search ') else cmd[2:] | |
| searcher.search(search_term) | |
| elif cmd in ['list', 'l']: | |
| searcher.list_all() | |
| elif cmd in ['help', 'h']: | |
| print("Commands: url|u <url>, paste|p, search|s <term>, list|l, quit|q") | |
| else: | |
| print("Unknown command. Type 'help' for commands.") | |
| except KeyboardInterrupt: | |
| print("\nGoodbye!") | |
| break | |
| except EOFError: | |
| break | |
| if __name__ == "__main__": | |
| main() |