-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.py
46 lines (34 loc) · 1.34 KB
/
main.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
#!/usr/bin/env python
# -*- coding: utf-8 -*-
import requests
import xmltodict
import datetime
from slack import Slack
from os import environ as env
from os.path import join, dirname
from dotenv import load_dotenv
# Load enviroment variables from .env
load_dotenv(join(dirname(__file__), '.env'))
url = 'http://www.yr.no/place/' + env.get('YR_PLACE') + '/forecast_hour_by_hour.xml'
response = requests.get(url).text
parsedXmlResponse = xmltodict.parse(response)
times = parsedXmlResponse['weatherdata']['forecast']['tabular']['time']
def convertToTimestamp(hour):
tomorrow = datetime.date.today() + datetime.timedelta(days=1)
return str(tomorrow) + hour
hours = ['T04:00:00', 'T05:00:00', 'T06:00:00', 'T07:00:00']
interval = map(convertToTimestamp, hours)
temperatures = []
for time in times:
if time['@from'] in interval:
temperatures.append(time['temperature']['@value'])
def belowFreezingPoint(temperature):
if int(temperature) <= 0:
return int(temperature)
# If the temperature is less than or equal to 0 degrees celcius, trigger notification
degreesBelowFreezing = filter(belowFreezingPoint, temperatures)
slack = Slack(env.get('SLACK_API_TOKEN'), env.get('SLACK_CHANNEL'))
if len(degreesBelowFreezing):
slack.send_message(str(min(degreesBelowFreezing)))
elif env.get('DEBUG', False):
slack.send_message('-100')