forked from mendhak/waveshare-epaper-display
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathutility.py
75 lines (54 loc) · 1.88 KB
/
utility.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
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
import codecs
import logging
import os
import time
import requests
import json
from astral import LocationInfo
from astral.sun import sun
import datetime
import pytz
logging.basicConfig(level=logging.INFO)
# utilize a template svg as a base for output of values
def update_svg(template_svg_filename, output_svg_filename, output_dict):
#replace tags with values in SVG
output = codecs.open(template_svg_filename, 'r', encoding='utf-8').read()
for output_key in output_dict:
logging.debug("update_svg() - {} -> {}".format(output_key, output_dict[output_key]))
output = output.replace(output_key, output_dict[output_key])
logging.debug("update_svg() - Write to SVG {}".format(output_svg_filename))
codecs.open(output_svg_filename, 'w', encoding='utf-8').write(output)
# Is the response file older than the TTL?
def is_stale(filepath, ttl):
verdict = True
if (os.path.isfile(filepath)):
verdict = time.time() - os.path.getmtime(filepath) > ttl
logging.debug(
"is_stale({}) - {}"
.format(filepath, str(verdict)))
return verdict
# Make HTTP Request or get response from cached file
def get_response_data(url):
response_json = False
try:
response_data = requests.get(url).text
response_json = json.loads(response_data)
except Exception as error:
logging.error(error)
raise
return response_json
# Is it daytime?
def is_daytime(location_lat, location_long):
# adjust icon for sunrise and sunset
dt = datetime.datetime.now(pytz.utc)
city = LocationInfo(location_lat, location_long)
s = sun(city.observer, date=dt)
verdict = False
if dt > s['sunset'] or dt < s['sunrise']:
verdict = False
else:
verdict = True
logging.debug(
"is_daytime({}{}) - {}"
.format(str(location_lat), str(location_long), str(verdict)))
return verdict