#!/usr/bin/env python3

# Copyright 2012-4, Sean B. Palmer
# Source: http://inamidst.com/saxo/

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
