1
0
Files
witbierbot/witbierbot.py
2019-02-25 16:27:27 +01:00

46 lines
973 B
Python
Executable File

#!/usr/bin/env python3
"""
Read weather predictions for the next days, and if it's high enough,
recommend that Dennis buys witbier.
"""
import datetime
import json
import os
import sys
from collections import defaultdict
from urllib.request import urlopen
def load_weather():
url = 'https://api.openweathermap.org/data/2.5/forecast?q=Leiden' \
+ '&units=metric' \
+ '&appid=' + os.environ['API_KEY']
result = urlopen(url)
return json.load(result)
def get_maxima(weather):
days = defaultdict(float)
for day in weather['list']:
instant = datetime.datetime.fromtimestamp(day['dt'])
date = instant.date()
days[date] = max(days[date], day['main']['temp_max'])
return days
def main():
if len(sys.argv) > 1:
with open(sys.argv[1]) as f:
weather = json.load(f)
else:
weather = load_weather()
print(get_maxima(weather))
if __name__ == '__main__':
main()