-
Notifications
You must be signed in to change notification settings - Fork 64
/
Copy pathSunriseSunset.py
45 lines (37 loc) · 1.31 KB
/
SunriseSunset.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
import requests, json, datetime, os
def cronner(msg):
f = open("/var/spool/cron/crontab/<username>","w")
f.write(msg)
f.close()
Times = None
try:
Times = requests.get(url="https://api.sunrise-sunset.org/json?lat=47.4979&lng=19.0402&date=today")
except:
raise SystemExit
if Times:
Sunrise = None
Sunset = None
try:
Sunrise = json.loads(Times.text)['results']['sunrise']
Sunset = json.loads(Times.text)['results']['sunset']
except:
print("Could not parse the json so the execution is aborted!")
raise SystemExit
try:
Sunrise_hour = Sunrise.split(':')[0]
Sunrise_minute = Sunrise.split(':')[1]
Sunset_hour = Sunset.split(':')[0]
Sunset_minute = Sunset.split(':')[1]
except:
print("There is something wrong with the format, aborting!")
raise SystemExit
msg = 'PATH:/usr/local/bin;/usr/bin:/home/<username>/Scripts/\n'
msg += '%s %s */1 * * /usr/bin/python /home/<username>/Scripts/Surveillance.py\n' % (Sunrise_minute, Sunrise_hour)
msg += '%s %s */1 * * /usr/bin/python /home/<username>/Scripts/KillSurveillance.sh\n' % (Sunset_minute, int(Sunset_hour) + 14)
try:
cronner(msg)
os.system('crontab -u <username> /var/spool/cron/crontab/<username>')
except:
raise SystemExit
else:
print("Execution aborted as there was an error to query the api!")