-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtasks.py
50 lines (40 loc) · 1.92 KB
/
tasks.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
47
48
49
50
from twilio.rest import Client
import xml.etree.ElementTree as eltree
from invoke import task
import urllib.request
import os
import time
import datetime
import decimal
def uv_level(city):
url = 'https://uvdata.arpansa.gov.au/xml/uvvalues.xml'
tree = eltree.parse(urllib.request.urlopen(url))
root = tree.getroot()
for child in root.findall(".//location[@id='" + city + "']/index"):
current_uv = str(child.text)
current_uv_decimalised = decimal.Decimal(current_uv)
if current_uv_decimalised is None:
print("No UV detected. Either the sun has vanished or there's something wrong with this program.")
return current_uv_decimalised
def send_sms(message):
client = Client(os.environ['TWILIO_ACCOUNT_SID'], os.environ['TWILIO_AUTH_TOKEN'])
client.messages.create(to=os.environ['USER_PHONE'], from_= 'UV Notifier', body=message)
@task(help={'city': 'Name of the city to read UV levels in.'})
def notifier(ctx, city):
"""
Notify the recipient when the UV levels reach a threshold in the ☀️ morning and 🌙 afternoon.
"""
num_sms_sent = 0
dt = datetime.datetime.now()
while num_sms_sent == 0:
current_uv = uv_level(city)
print(str(current_uv), dt)
time.sleep(10)
if 3.0 < current_uv < 3.5 and dt.hour < 12:
print('🌅 It has just hit ' + str(current_uv) + ' in ' + city + '. Put on some sunscreen before going outside.')
send_sms('🌅 It has just hit ' + str(current_uv) + ' in ' + city + '. Put on some sunscreen before going outside.')
num_sms_sent = num_sms_sent + 1
if 3.0 < current_uv < 3.5 and dt.hour > 12:
print('☀️ The current UV rating in ' + city + ' is ' + str(current_uv) + '. Have fun outside.')
send_sms('☀️ The current UV rating in ' + city + ' is ' + str(current_uv) + '. Have fun outside.')
num_sms_sent = num_sms_sent + 1