#!/usr/bin/env python3

# http://inamidst.com/saxo/
# Created by Sean B. Palmer

import json
import re

import saxo

regex_results = re.compile(r"(?i)([0-9,]+) results?")

@saxo.pipe
def gc(arg):
    if not arg:
        return "Count the number of results for a phrase on Google"

    query = {"hl": "en", "q": arg}
    page = saxo.request("https://www.google.com/search", query=query)

    if "No results found for" in page["text"]:
        return "0"
    elif "did not match any documents" in page["text"]:
        return "0"

    for result in regex_results.findall(page["text"]):
        return result
