#!/usr/bin/env python3 import sys import time import shlex import subprocess import contextlib from typing import Sequence import click def openurl(url: str, wait: float) -> None: u = url.strip() if not u: return subprocess.run(shlex.split(f'openurl "{u}"')) time.sleep(wait) @click.command() @click.option( "-w", "--wait", default=0, type=float, help="Wait time between opening urls" ) @click.argument("LINK", nargs=-1, required=False) def main(wait: float, link: Sequence[str]) -> None: """ uses my underlying openurl script Examples: \b openlinks <<< "https://" openlinks "https://" "http://" echo "https://..." | openlinks cat file_with_links.txt | openlinks If no links are given when called, i.e.: $ openlinks You can input/paste links; they'll be opened when you hit enter/return EOF (Ctrl+D), Ctrl+C to quit """ for lnk in link: openurl(lnk, wait) if len(link) > 0: sys.exit(0) # open anything piped/as input while True: try: user_input = input() for line in user_input.splitlines(): openurl(line, wait) except (EOFError, KeyboardInterrupt): sys.exit(0) if __name__ == "__main__": with contextlib.suppress(KeyboardInterrupt): main()