This repository has been archived by the owner on Dec 11, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
process.py
126 lines (100 loc) · 3.86 KB
/
process.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
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
from typing import Any, Tuple
import pendulum
import requests
from bs4 import BeautifulSoup
import config
from crontabber import CronTabber
URL = 'https://api.wolframalpha.com/v2/query'
def check_sunset() -> Tuple[int, int]:
"""Check sunset time using the Wolfram|Alpha API.
Returns:
Tuple[int, int]: (hour, minute) of the sunset
Raises:
KeyError: if local sunset could not be determined from Wolfram|Alpha
and failsafe was not configured
"""
try:
page = requests.get(
f'{URL}?input=sunset&appid={config.APPID}'
'&assumption=CalendarEventName'
)
soup = BeautifulSoup(page.text, 'lxml')
for pod in soup.find_all('pod'):
if pod['id'] == 'Result':
text = pod.plaintext.text
time = text.split('\n')[0]
h_m, period = time.split()[:2]
hour, minute = [int(d) for d in h_m.split(':')]
if period.startswith('p'):
hour += 12
message = f'Sunset will be at {hour:02}:{minute:02}'
config.LOGGER.info(message)
return (hour, minute)
else:
raise ValueError(
'Could not determine local sunset from Wolfram|Alpha.')
except (AttributeError, KeyError, TypeError, ValueError) as e:
config.LOGGER.warn('Encountered an error. Using failsafe...')
config.LOGGER.warn(e)
try:
failsafe = config.CONF['failsafe']
failsafe_time = f"{failsafe['hour']:02}:{failsafe['minute']:02}"
config.LOGGER.info(
f"Sunset emulated at {failsafe_time}."
)
return (failsafe['hour'], failsafe['minute'])
except KeyError as e:
config.LOGGER.error('Failsafe was not configured. Exiting...')
raise e
def adjust_time(hour: int, minute: int) -> Tuple[int, int]:
"""Adjust time from sunset using offset in config.
Returns:
Tuple[int, int]: (hour, minute) of the adjusted time
"""
today = pendulum.today().at(hour, minute)
today = today.add(hours=config.OFFSET_H, minutes=config.OFFSET_M)
hour = today.hour
minute = today.minute
message = f'Scripts will run at around {hour:02}:{minute:02}'
config.LOGGER.info(message)
return (today.hour, today.minute)
def is_shutdown_time_int(hour: Any, minute: Any) -> bool:
"""Check if shutdown time are numbers.
Args:
hour (Any): hour according to user config
minute (Any): minute according to user config
Returns:
bool: True if both are numbers
"""
return type(hour) is int and type(minute) is int
def main() -> None:
"""Run the main script."""
hour, minute = check_sunset()
try:
if not (config.SCRIPTS['root'] and config.SCRIPTS['switch_on']):
raise ValueError
except (KeyError, ValueError) as e:
config.LOGGER.error('Your configuration is malformed. More info:')
config.LOGGER.error(e)
config.LOGGER.info('Exiting...')
raise config.InvalidConfigError from e
hour, minute = adjust_time(hour, minute)
crontab = CronTabber()
crontab.new('switch_on', hour, minute)
shutdown = config.CONF['shutdown']
try:
if not shutdown['enabled']:
raise config.ShutdownDisabled
if not (shutdown['remove'] and config.SCRIPTS['switch_off']):
raise ValueError
if not is_shutdown_time_int(shutdown['hour'], shutdown['minute']):
raise ValueError
except (ValueError, config.ShutdownDisabled) as e:
config.LOGGER.info('Shutdown is disabled. More info:')
config.LOGGER.info(e)
else:
crontab.new('switch_off', shutdown['hour'], shutdown['minute'])
finally:
config.LOGGER.info('Completed Sunsetter.')
if __name__ == '__main__':
main()