limbo/plugins/weather.py
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?lat={:.2f}&lon={:.2f}&units={}&appid={}".format(
            lat, lon, unit, OPENWEATHER_API_KEY
        )
    ).json()
    if forecast["cod"] != "200":
        raise KeyError("Invalid OpenWeatherMap key")

    # the dates are in UTC, but we want them in local time. convert them
    utc_offset = forecast["city"]["timezone"]
    forecasts = sorted(
        (
            datetime.fromtimestamp(cast["dt"] + utc_offset).strftime("%Y-%m-%d"),
            int(round(cast["main"]["temp_max"])),
            ICONMAP.get(cast["weather"][0]["icon"], ":question:"),
        )
        for cast in forecast["list"]
    )

    # for each day's forecasts, pick the one with the max temperature and
    # create a weather message
    days = groupby(forecasts, lambda x: x[0])
    messages = []
    for dt, forecasts in days:
        dayname = datetime.strptime(dt, "%Y-%m-%d").strftime("%A")
        high, icon = max((cast[1], cast[2]) for cast in forecasts)

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

    return title, messages[:4]
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
Returns a title and a list of forecasts.

The title describes the location for the forecast
The list of forecasts is a list of dictionaries in slack attachment fields
format
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 The list of forecasts is a list of dictionaries in slack attachment fields format
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?lat={:.2f}&lon={:.2f}&units={}&appid={}".format(
            lat, lon, unit, OPENWEATHER_API_KEY
        )
    ).json()
    if forecast["cod"] != "200":
        raise KeyError("Invalid OpenWeatherMap key")
    utc_offset = forecast["city"]["timezone"]
    forecasts = sorted(
        (
            datetime.fromtimestamp(cast["dt"] + utc_offset).strftime("%Y-%m-%d"),
            int(round(cast["main"]["temp_max"])),
            ICONMAP.get(cast["weather"][0]["icon"], ":question:"),
        )
        for cast in forecast["list"]
    )
    days = groupby(forecasts, lambda x: x[0])
    messages = []
    for dt, forecasts in days:
        dayname = datetime.strptime(dt, "%Y-%m-%d").strftime("%A")
        high, icon = max((cast[1], cast[2]) for cast in forecasts)
        messages.append(
            {
                "title": dayname,
                "value": u"{} {}°{}".format(icon, high, unit_abbrev),
                "short": True,
            }
        )
    return title, messages[:4]
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?lat={:.2f}&lon={:.2f}&units={}&appid={}" . format ( lat , lon , unit , OPENWEATHER_API_KEY ) ) . json ( ) if forecast [ "cod" ] != "200" : raise KeyError ( "Invalid OpenWeatherMap key" ) utc_offset = forecast [ "city" ] [ "timezone" ] forecasts = sorted ( ( datetime . fromtimestamp ( cast [ "dt" ] + utc_offset ) . strftime ( "%Y-%m-%d" ) , int ( round ( cast [ "main" ] [ "temp_max" ] ) ) , ICONMAP . get ( cast [ "weather" ] [ 0 ] [ "icon" ] , ":question:" ) , ) for cast in forecast [ "list" ] ) days = groupby ( forecasts , lambda x : x [ 0 ] ) messages = [ ] for dt , forecasts in days : dayname = datetime . strptime ( dt , "%Y-%m-%d" ) . strftime ( "%A" ) high , icon = max ( ( cast [ 1 ] , cast [ 2 ] ) for cast in forecasts ) messages . append ( { "title" : dayname , "value" : u"{} {}°{}". f ormat( i con, igh, nit_abbrev) , "short" : True , } ) return title , messages [ : 4 ]
Get the weather for a place given by searchterm
Returns a title and a list of forecasts.
Get the weather for a place given by searchterm Returns a title and a list of forecasts .
"""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)
    """ # the dates are in UTC, but we want them in local time. convert them # for each day's forecasts, pick the one with the max temperature and # create a weather message
searchterm