GHC 2020-10-17

2 comments.

, https://git.io/JTCk1 in jarun/googler
Okay, looked at the origin of the URL handler code, 06d77cddec34b9dcc554155ae2abb01cbd9d2c92 by @jarun. It was written to specifically deny stdin to the handler, and written in kind of a peculiar way too (could have just used `Popen` with `stdin=subprocess.DEVNULL` without opening a pipe and immediately closing it by piping nothing into it). I'm not sure why deny stdin to the handler; @jarun can you remember or think of any reason?

I think we can enable stdin for the handler if we can't come up with an objection (will give this another thought with a clearer mind tomorrow). Meanwhile, there's a `/dev/tty` trick you can use right now that allows you to exhaust stdin (fd 0), then "reopen" it for interactivity @097115. Try something like this:

```py
#!/usr/bin/env python3

import argparse
import contextlib
import re
import socket
import sys
import urllib.parse
import webbrowser


@contextlib.contextmanager
def redirect_stdin(fp):
    saved_stdin = sys.stdin
    sys.stdin = fp
    yield
    sys.stdin = saved_stdin


def resolve_domain(url):
    try:
        netloc = urllib.parse.urlparse(url).netloc
        host = re.match(r"^(.*@)?(?P<host>.*?)(:.*)?$", netloc).group("host")
        return socket.gethostbyname(host)
    except Exception:
        return None


def main():
    parser = argparse.ArgumentParser()
    parser.add_argument("urls", nargs="+")
    args = parser.parse_args()
    # Exhaust stdin.
    sys.stdin.read()
    with open("/dev/tty", "r") as tty:
        with redirect_stdin(tty):
            for url in args.urls:
                ip = resolve_domain(url)
                yn = input(f"{url} resolves to {ip if ip else 'unknown'}, open? [yN] ")
                if yn.strip().lower().startswith("y"):
                    webbrowser.open(url)


if __name__ == "__main__":
    main()
```

Sample session:

```console
$ googler --url-handler=./interactive.py --exact -n 3 googler

 1.  jarun/googler: Google from the terminal - GitHub
     https://github.com/jarun/googler
     googler is a power tool to Google (web, news, videos and site search) from the command-line. It shows the title,
     URL and abstract for each result, which can be ...

 2.  Googler - Wiktionary
     https://en.wiktionary.org/wiki/Googler
     NounEdit. Googler (plural Googlers). A full-time Google corporation employee. A regular or habitual user of the
     Google search engine.

 3.  GOOGLER | 1 Definitions of Googler - YourDictionary
     https://www.yourdictionary.com/googler
     Googler definitions. Filters (0). (1) A person who is an employee of Google. See Xoogler. 0. 0. Words near
     googler in the Dictionary. google product search ...

googler (? for help) o 1-3
https://github.com/jarun/googler resolves to 192.30.255.112, open? [yN] y
https://en.wiktionary.org/wiki/Googler resolves to 198.35.26.96, open? [yN] n
https://www.yourdictionary.com/googler resolves to 99.84.238.60, open? [yN] n
```

, https://git.io/JTchy in jarun/googler
I can take a look tomorrow.