def get_listings(loc, sigma2):
    c = flask.current_app.config
    payload = {"client_id": c["FOURSQUARE_ID"],
               "client_secret": c["FOURSQUARE_SECRET"],
               "v": "20130111",
               "section": "food",
               "limit": 50}

    ntries = 3
    for i in range(ntries):
        # Propose a new position.
        new_pos = propose_position(loc, np.sqrt(sigma2))
        payload["ll"] = "{1},{0}".format(*new_pos)

        # Submit the search on Yelp.
        r = requests.get(api_url, params=payload)
        if r.status_code != requests.codes.ok:
            try:
                send_msg(",".join(flask.current_app.config["ADMIN_EMAILS"]),
                         flask.request.url + "\n\n"
                            + json.dumps(r.json(), indent=2),
                         "4sq API request failed.")
            except:
                pass

            return []

        data = r.json()
        if "response" not in data or "groups" not in data["response"]:
            return []

        data = data["response"]["groups"]
        listings = []
        for l in data:
            listings += [FoursquareListing(d["venue"]) for d in l["items"]]

        if len(listings) > 0:
            break

    return listings
def accept(acceptid):
    # Get the current user.
    user = current_user()

    # Try to find the proposal.
    prop = Proposal.from_id(acceptid)
    if prop is None:
        return json.dumps({"message": "Unknown proposal."})

    # If not logged in, remove the proposal.
    if prop.user_id is None or user is None:
        prop.remove()
        return json.dumps({"message": "No user."})

    # Update the proposal for posterity.
    prop.update_response(1)

    # Send the email.
    fmt_args = (user, prop, gettext("Here's a goddamn map"),
                gettext("F*****g enjoy it!"))
    text = """Hey {0.fullname},

It looks like you're heading to {1.name} at {1.address}.

For more info about this restaurant: {1.url}

{2}: {1.map_link}

{3}

Sincerely,
The Lunch Robot
[email protected]

""".format(*fmt_args)

    html = """<p>Hey {0.fullname},</p>

<p>Looks like you're heading to <a href="{1.url}">{1.name}</a> for lunch
today.</p>

<p style="text-align: center;"><strong>{1.name}</strong><br>
{1.address}</p>

<p style="text-align: center;">
<a href="{1.map_link}"><img src="{1.map_url}" style="width: 640px;"></a></p>

<p>{3}</p>

<p>Sincerely,<br>
The Lunch Robot<br>
<a href="mailto:[email protected]">[email protected]</a></p>

""".format(*fmt_args)

    try:
        send_msg("{0.fullname} <{0.email}>".format(user), text,
                 "Lunch at {0}".format(prop.name), html=html)
    except Exception as e:
        print("EMAIL ERROR: ", e)
        return json.dumps({"message": "Better luck next time."})

    return json.dumps({"message": "You got it."})