-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathzmanimapi.py
executable file
·245 lines (226 loc) · 9.29 KB
/
zmanimapi.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
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
#!/usr/bin/env -S python3 -u
import datetime
import pytz
from timezonefinder import TimezoneFinder
from convertdate import hebrew
import ephem
import json
import argparse
tf = TimezoneFinder()
def jewish_holiday(date, chagdays=1):
if (chagdays != 1 and chagdays != 2):
raise ValueError("chagdays must be 1 or 2")
if (date[1] == 7 and date[2] < 3):
# Rosh Hashanah
return True
if (date[1] == 7 and date[2] == 10):
# Yom Kippur
return True
if (date[1] == 7 and date[2] == 15):
# Sukkot, day 1
return True
if (date[1] == 7 and date[2] == 22):
# Shemini Atzeret
return True
if (date[1] == 1 and date[2] == 15):
# Pesach, day 1
return True
if (date[1] == 1 and date[2] == 21):
# Pesach, day 7
return True
if (date[1] == 3 and date[2] == 6):
# Shavuot
return True
if chagdays == 2:
if (date[1] == 7 and date[2] == 16):
# Sukkot, day 2
return True
if (date[1] == 7 and date[2] == 23):
# Simchat Torah
return True
if (date[1] == 1 and date[2] == 16):
# Pesach, day 2
return True
if (date[1] == 1 and date[2] == 22):
# Pesach, day 8
return True
if (date[1] == 3 and date[2] == 7):
# Shavuot, day 2
return True
return False
def hebrew_monthname(thedate):
months = {True: ("", "Nisan", "Iyar", "Sivan", "Tammuz", "Av", "Elul", "Tishrei", "Heshvan", "Kislev", "Tevet", "Shevat", "Adar1", "Adar2"), False: ("", "Nisan", "Iyar", "Sivan", "Tammuz", "Av", "Elul", "Tishrei", "Heshvan", "Kislev", "Tevet", "Shevat", "Adar")}
return months[hebrew.leap(thedate[0])][thedate[1]]
def hanukkah_day(date):
if (date[1] == 9 and date[2] > 24):
return date[2] - 24
if (date[1] == 10 and date[2] < 4 and (hebrew.year_days(date[0]) % 10 == 3)):
# This year, the 3rd of Tevet is still Hanukkah
return date[2] + 5
if (date[1] == 10 and date[2] < 3 and (hebrew.year_days(date[0]) % 10 != 3)):
return date[2] + 6
return 0
def omer_day(thedate):
if hebrew_monthname(thedate) == "Nisan":
return max(thedate[2]-15, 0)
elif hebrew_monthname(thedate) == "Iyar":
return thedate[2]+15
elif hebrew_monthname(thedate) == "Sivan" and thedate[2] < 6:
return thedate[2] + 44
return 0
def do_the_things(lat, lon, chagdays=2, offset=0):
tzname = tf.timezone_at(lng=lon, lat=lat)
try:
tz = pytz.timezone(tzname)
except pytz.exceptions.UnknownTimeZoneError:
tz = pytz.timezone('UTC')
now = datetime.datetime.now(tz=tz)
if offset is not None:
now += datetime.timedelta(minutes=offset)
noon = tz.localize(datetime.datetime(year=now.year, month=now.month, day=now.day, hour=12, minute=30))
today = now.date()
tomorrow = today + datetime.timedelta(days=1)
# Get Hebrew calendar dates
hebtoday = hebrew.from_gregorian(today.year, today.month, today.day)
hebtomorrow = hebrew.from_gregorian(tomorrow.year, tomorrow.month, tomorrow.day)
hebmonthtoday = hebrew_monthname(hebtoday)
hebmonthtomorrow = hebrew_monthname(hebtomorrow)
omertoday = omer_day(hebtoday)
omertonight = omer_day(hebtomorrow)
hanukkahtoday = hanukkah_day(hebtoday)
hanukkahtonight = hanukkah_day(hebtomorrow)
# Set up ephem info to determine sunset and nightfall
herenow = ephem.Observer()
herenow.lat, herenow.lon = lat*ephem.pi/180, lon*ephem.pi/180
herenow.date = ephem.Date(now.astimezone(pytz.utc))
herenoon = ephem.Observer()
herenoon.lat, herenoon.lon = lat*ephem.pi/180, lon*ephem.pi/180
herenoon.date = ephem.Date(noon.astimezone(pytz.utc))
sun = ephem.Sun()
# Determine "set" and "dark" for today (may be in the past)
try:
todayrise_eph = herenoon.previous_rising(sun)
todayrise = pytz.utc.localize(todayrise_eph.datetime()).astimezone(tz)
todayrise_txt = todayrise.isoformat(timespec='seconds')
tonightset_eph = herenoon.next_setting(sun)
tonightset = pytz.utc.localize(tonightset_eph.datetime()).astimezone(tz)
tonightset_txt = tonightset.isoformat(timespec='seconds')
except ephem.NeverUpError:
todayrise_txt = 'downallday'
tonightset_txt = 'downallday'
except ephem.AlwaysUpError:
todayrise_txt = 'upallday'
tonightset_txt = 'upallday'
oldhorizon = herenoon.horizon
oldpressure = herenoon.pressure
herenoon.pressure = 0
# All horizon math is from top of sun disk
# We need to take into account sun's radius, averaging .266 degrees
herenoon.horizon = "-8.233" # middle of sun 8.5 deg
try:
tonightdark_eph = herenoon.next_setting(sun)
tonightdark = pytz.utc.localize(tonightdark_eph.datetime()).astimezone(tz)
tonightdark_txt = tonightdark.isoformat(timespec='seconds')
except ephem.NeverUpError:
tonightdark_txt = 'alwaysdark'
except ephem.AlwaysUpError:
tonightdark_txt = 'none'
herenoon.horizon = oldhorizon
herenoon.pressure = oldpressure
# Status of sun, handle no-rise/no-set cases first
if todayrise_txt == "upallday":
sunnow = "up"
elif (todayrise_txt == 'downallday' and (tonightdark == 'alwaysdark' or tonightdark < now)):
sunnow = "down"
elif todayrise_txt == 'downallday':
sunnow = "twilight"
# normal cases
elif todayrise > now:
sunnow = "notyetup"
elif tonightset > now:
sunnow = "up"
elif (tonightdark_txt == 'none' or tonightdark > now):
sunnow = "twilight"
else:
sunnow = "down"
# Is it Shabbat or a holiday?
shabbat_or_holiday_today = (today.isoweekday() == 6)
if (jewish_holiday(date=hebtoday, chagdays=chagdays)):
shabbat_or_holiday_today = True
shabbat_or_holiday_tonight = (today.isoweekday() == 5)
if (jewish_holiday(date=hebtomorrow, chagdays=chagdays)):
shabbat_or_holiday_tonight = True
# Combine hebdate logic and shabbat/holiday logic with sun up/down logic
if (sunnow == "notyetup" or sunnow == "up"):
shabbat_or_holiday_now = shabbat_or_holiday_today
hebrew_date_now = "{} {}, {}".format(hebtoday[2], hebmonthtoday, hebtoday[0])
omernow = omertoday
hanukkahnow = hanukkahtoday
elif (sunnow == "down"):
shabbat_or_holiday_now = shabbat_or_holiday_tonight
hebrew_date_now = "{} {}, {}".format(hebtomorrow[2], hebmonthtomorrow, hebtomorrow[0])
omernow = omertonight
hanukkahnow = hanukkahtonight
elif (sunnow == "twilight"):
shabbat_or_holiday_now = (shabbat_or_holiday_today or shabbat_or_holiday_tonight)
hebrew_date_now = "indeterminate"
if omertoday > 0 and omertonight > 0:
omernow = omertoday + 0.5
else:
omernow = 0
if hanukkahtoday > 0 and hanukkahtonight > 0:
hanukkahnow = hanukkahtoday + 0.5
else:
hanukkahnow = 0
else:
raise ValueError("How is the sun not up or down or twilight?")
# time for output
to_return = {}
to_return["results"] = {
"now": now.astimezone(tz).isoformat(timespec='seconds'),
"sunrise": todayrise_txt,
"sunset": tonightset_txt,
"jewish_twilight_end": tonightdark_txt,
"sun_now": sunnow,
"hebrew_date_today": "{} {}, {}".format(hebtoday[2], hebmonthtoday, hebtoday[0]),
"hebrew_date_tonight": "{} {}, {}".format(hebtomorrow[2], hebmonthtomorrow, hebtomorrow[0]),
"hebrew_date_now": hebrew_date_now,
"shabbat_or_yom_tov_today": shabbat_or_holiday_today,
"shabbat_or_yom_tov_tonight": shabbat_or_holiday_tonight,
"shabbat_or_yom_tov_now": shabbat_or_holiday_now,
}
if omertoday > 0:
to_return["results"]["omer_today"] = omertoday
if omertonight > 0:
to_return["results"]["omer_tonight"] = omertonight
if omernow > 0:
to_return["results"]["omer_now"] = omernow
if hanukkahtoday > 0:
to_return["results"]["hanukkah_today"] = hanukkahtoday
if hanukkahtonight > 0:
to_return["results"]["hanukkah_tonight"] = hanukkahtonight
if hanukkahnow > 0:
to_return["results"]["hanukkah_now"] = hanukkahnow
return json.dumps(to_return)
def lambda_handler(event, context):
# Syntax: https://github.com/donnieprakoso/boilerplate-aws-lambda-proxy-python3/blob/master/main.py
query = event['queryStringParameters']
chagdays = None
if "chagdays" in query:
chagdays = int(query['chagdays'])
offset = None
if "offset" in query:
offset = int(query['offset'])
output = do_the_things(lat=float(query['lat']), lon=float(query['lon']), chagdays=chagdays, offset=offset)
return {
'statusCode': 200,
'body': output
}
if __name__ == "__main__":
parser = argparse.ArgumentParser()
parser.add_argument("-t", "--lat", help="latitude", type=float)
parser.add_argument("-n", "--lon", help="longitude", type=float)
parser.add_argument("-c", "--chagdays", help="how many days of chag", type=int, choices=[1, 2], default=2)
parser.add_argument("-o", "--offset", help="calculate this many minutes from now", type=int)
args = parser.parse_args()
print(do_the_things(lat=args.lat, lon=args.lon, chagdays=args.chagdays, offset=args.offset))