Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

weather bot #1

Open
genekogan opened this issue Dec 25, 2020 · 0 comments
Open

weather bot #1

genekogan opened this issue Dec 25, 2020 · 0 comments
Labels
enhancement New feature or request

Comments

@genekogan
Copy link
Member

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)
@genekogan genekogan added the enhancement New feature or request label Dec 25, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

No branches or pull requests

1 participant