(searchterm)
return title, messages
Get the weather for a place given by searchterm

    Returns a title and a list of forecasts.

    The title describes the location for the forecast (i.e. "Portland, ME USA")
    The list of forecasts is a list of dictionaries in slack attachment fields
        format (see https://api.slack.com/docs/message-attachments)
Get the weather for a place given by searchterm
Get the weather for a place given by searchterm
def weather(searchterm):
    """Get the weather for a place given by searchterm

    Returns a title and a list of forecasts.

    The title describes the location for the forecast (i.e. "Portland, ME USA")
    The list of forecasts is a list of dictionaries in slack attachment fields
        format (see https://api.slack.com/docs/message-attachments)
    """
    unit = CELSIUS if os.environ.get("WEATHER_CELSIUS") else IMPERIAL
    unit_abbrev = "f" if unit == IMPERIAL else "c"

    geo = requests.get(
        "https://api.mapbox.com/geocoding/v5/mapbox.places/{}.json?limit=1&access_token={}".format(
            quote(searchterm.encode("utf8")), MAPBOX_API_TOKEN
        )
    ).json()
    citystate = geo["features"][0]["place_name"]
    lon, lat = geo["features"][0]["center"]

    title = "Weather for {}: ".format(citystate)

    forecast = requests.get(
        "https://api.openweathermap.org/data/2.5/forecast/daily?lat={:.2f}&lon={:.2f}&cnt=4&units={}&appid={}".format(
            lat, lon, unit, OPENWEATHER_API_KEY
        )
    ).json()
    if forecast["cod"] != "200":
        raise KeyError("Invalid OpenWeatherMap key")

    messages = []
    for cast in forecast["list"]:
        # do I need to mess with tz at all, or is this accurate enough?
        dt = datetime.fromtimestamp(cast["dt"]).strftime("%A")
        high = int(round(cast["temp"]["max"]))
        icon = ICONMAP.get(cast["weather"][0]["icon"], ":question:")

        messages.append(
            {
                "title": dt,
                "value": u"{} {}°{}".format(icon, high, unit_abbrev),
                "short": True,
            }
        )

    return title, messages
def weather ( searchterm ) : unit = CELSIUS if os . environ . get ( "WEATHER_CELSIUS" ) else IMPERIAL unit_abbrev = "f" if unit == IMPERIAL else "c" geo = requests . get ( "https://api.mapbox.com/geocoding/v5/mapbox.places/{}.json?limit=1&access_token={}" . format ( quote ( searchterm . encode ( "utf8" ) ) , MAPBOX_API_TOKEN ) ) . json ( ) citystate = geo [ "features" ] [ 0 ] [ "place_name" ] lon , lat = geo [ "features" ] [ 0 ] [ "center" ] title = "Weather for {}: " . format ( citystate ) forecast = requests . get ( "https://api.openweathermap.org/data/2.5/forecast/daily?lat={:.2f}&lon={:.2f}&cnt=4&units={}&appid={}" . format ( lat , lon , unit , OPENWEATHER_API_KEY ) ) . json ( ) if forecast [ "cod" ] != "200" : raise KeyError ( "Invalid OpenWeatherMap key" ) messages = [ ] for cast in forecast [ "list" ] : # do I need to mess with tz at all, or is this accurate enough? dt = datetime . fromtimestamp ( cast [ "dt" ] ) . strftime ( "%A" ) high = int ( round ( cast [ "temp" ] [ "max" ] ) ) icon = ICONMAP . get ( cast [ "weather" ] [ 0 ] [ "icon" ] , ":question:" ) messages . append ( { "title" : dt , "value" : u"{} {}°{}". f ormat( i con, igh, nit_abbrev) , "short" : True , } ) return title , messages