#!/usr/bin/env python3

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

import json
import re
import urllib.parse
import saxo

r_url = re.compile(r'(?ims)class="r".*?<a href="(.*?)"')

@saxo.pipe
def g(arg):
    if not arg:
        return "Search for a phrase on Google"

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

    for url in r_url.findall(page["text"]):
        if url.startswith("/"):
           continue
        return urllib.parse.unquote(url)
    return "No result for %s" % arg
