Skip to content
This repository has been archived by the owner on Mar 5, 2022. It is now read-only.

Print content of results to terminal or listen to it

Mischievous Meerkat edited this page Jun 24, 2019 · 2 revisions

If for whatever reason you don't want to use a browser, GUI or textual (e.g. to read the news with option -N), googler still got you covered due to the flexibility of --url-handler. Here is an example.

Below is a quick content extraction script using the excellent newspaper library. Let's call it dump-content. It dumps extracted content directly to your terminal, or invokes a pager (less) if the content is too long. It may take multiple URLs.

#!/usr/bin/env python3

import argparse
import subprocess

import newspaper


def main():
    parser = argparse.ArgumentParser()
    parser.add_argument('urls', metavar='URL', nargs='+')
    args = parser.parse_args()

    for url in args.urls:
        article = newspaper.Article(url)
        article.download()
        article.parse()
        content = url + '\n\n' + article.text + '\n'
        lines = content.count('\n')
        if lines <= 48:
            print(content)
        else:
            # Use pager for long-form content. Of course, detection
            # criteria could be a lot more sophisticated. This is just a
            # proof of concept.
            p = subprocess.Popen(['less'], stdin=subprocess.PIPE)
            p.communicate(content.encode('utf-8'))
            p.wait()


if __name__ == '__main__':
    main()

You need to install newspaper via PyPI: pip install newspaper3k.

Now invoke googler with this script:

$ googler --url-handler /path/to/dump-content google

screenshot

To listen to news in the terminal directly, try the following (needs pico2wave to convert text to speech):

#!/usr/bin/env python3

import argparse
import os
import subprocess
import tempfile

import newspaper


def main():
    parser = argparse.ArgumentParser()
    parser.add_argument('urls', metavar='URL', nargs='+')
    args = parser.parse_args()

    for url in args.urls:
        article = newspaper.Article(url)
        article.download()
        article.parse()
        fd, path = tempfile.mkstemp(suffix='.wav')
        os.close(fd)
        subprocess.check_call(['pico2wave', '-w', path, '-l', 'en-US', article.text])
        subprocess.check_call(['mpv', path])
        os.remove(path)


if __name__ == '__main__':
    main()