Skip to content

weather bot #1

@genekogan

Description

@genekogan

Let's make a weather bot as another program. Some notes in this notebook...

We can use OpenWeatherMap.

This does the basics, getting wind, rain, and temperature forecast. Now to connect it to the bot.

import os
from datetime import timezone, datetime
from datetime import timedelta
from pyowm import OWM

def utc_to_local(utc_dt):
    return utc_dt.replace(tzinfo=timezone.utc).astimezone(tz=None)

api_key = 'MY_API_KEY'

owm = OWM(api_key)
obs = owm.weather_at_coords(-0.107331,51.503614)  

today = datetime.today().date()
tomorrow = today + timedelta(days=1)

fc = owm.three_hours_forecast_at_coords(40.744206, -74.000630)   # new york as an example

weather = fc.get_forecast()
forecast = [{'time': utc_to_local(datetime.utcfromtimestamp(w.get_reference_time())),
             'temperature': w.get_temperature('fahrenheit')['temp'],
             'wind': w.get_wind('miles_hour')['speed'], 
             'rain': None if w.get_rain()=={} else w.get_rain()['3h'],
             'heat_index': w.get_heat_index(),
             'status': w.get_detailed_status()} 
            for w in weather.get_weathers()]

today_forecast = [f for f in forecast if f['time'].date() == today]
tomorrow_forecast = [f for f in forecast if f['time'].date() == tomorrow]

print(today_forecast)

Metadata

Metadata

Assignees

No one assigned

    Labels

    enhancementNew feature or request

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions