-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
geocheck.py
68 lines (56 loc) · 1.8 KB
/
geocheck.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
from geopy.geocoders import Nominatim # pip3 install geopy
import datetime
from suntime import Sun # pip3 install suntime
def location_check(location):
result = {}
try:
geolocator = Nominatim(user_agent="geocheck")
details = geolocator.geocode(location)
lat = details.latitude
long = details.longitude
result['success'] = True
result['latitude'] = lat
result['longitude'] = long
result['location'] = location
except:
# If any error occured, return false
result['success'] = False
return(result)
def get_sunrise_sunset(lat, long):
result = {}
try:
sun = Sun(lat, long)
# Get today's sunrise and sunset
today_sr = sun.get_local_sunrise_time()
today_ss = sun.get_local_sunset_time()
result['success'] = True
result['latitude'] = lat
result['longitude'] = long
result['sunrise'] = today_sr.time().strftime('%H:%M')
result['sunset'] = today_ss.time().strftime('%H:%M')
result['lastupdate'] = datetime.datetime.now().strftime('%Y-%m-%d')
except:
# If any error occured, return false
result['success'] = False
return(result)
def night_or_day(lat, long):
sun = Sun(lat, long)
# Get today's sunrise and sunset in UTC
today_sr = sun.get_local_sunrise_time()
today_ss = sun.get_local_sunset_time()
now = datetime.datetime.now(today_sr.tzinfo)
if (now.time() > today_sr.time()) and (now.time() < today_ss.time()):
return('day')
elif (now.time() < today_sr.time()) or (now.time() > today_ss.time()):
return('night')
else:
return(None)
def is_current(lastcheck):
now = datetime.datetime.now()
lc = datetime.datetime.strptime(lastcheck, '%Y-%m-%d')
delta = datetime.timedelta(5)
check = now - delta
if(lc.date() > check.date()):
return(True) # Current sunrise/sunset checked in last 5 days
else:
return(False) # Current sunrise/sunset is older than 5 days