Files
nowblinkie/res/pdweather.py

62 lines
1.4 KiB
Python
Executable File

#!/usr/bin/env python3
#
# Akbar Rahman <hi@alv.cx>
#
import sys
from datetime import datetime as dt
import requests
def get_args():
""" Get command line arguments """
import argparse
parser = argparse.ArgumentParser()
return parser.parse_args()
def main(args):
""" Entry point for script """
resp = requests.get("https://api.open-meteo.com/v1/forecast?latitude=53.35&longitude=-1.8333&hourly=temperature_2m,rain,cloud_cover&timezone=GMT&forecast_days=1").json()
now = dt.now()
closest_past_date = None
for idx, datetime_string in enumerate(resp['hourly']['time']):
date = dt.fromisoformat(datetime_string)
if closest_past_date is None:
closest_past_date = (idx, date)
continue
if date > now: # date > now means date is in future, ignore
continue
if (now - date) < (now - closest_past_date[1]):
closest_past_date = (idx, date)
continue
cur_weather = {}
for key in resp['hourly'].keys():
cur_weather[key] = resp['hourly'][key][closest_past_date[0]]
output = "sunny in peaks"
if cur_weather['cloud_cover'] > 40:
output = "cloudy in peaks"
if cur_weather['rain'] > 0.5:
output = "raining in peaks"
print(output, end='')
return 0
if __name__ == '__main__':
try:
sys.exit(main(get_args()))
except KeyboardInterrupt:
sys.exit(0)