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
|
import datetime
import logging
from discord import Embed
from discord.ext import commands
from bot.bot import Bot
from bot.constants import Colours, Month
from bot.utils.decorators import in_month
log = logging.getLogger(__name__)
HEBCAL_URL = (
"https://www.hebcal.com/hebcal/?v=1&cfg=json&maj=on&min=on&mod=on&nx=on&"
"year=now&month=x&ss=on&mf=on&c=on&geo=geoname&geonameid=3448439&m=50&s=on"
)
class HanukkahEmbed(commands.Cog):
"""A cog that returns information about Hanukkah festival."""
def __init__(self, bot: Bot):
self.bot = bot
self.hanukkah_dates: list[datetime.date] = []
def _parse_time_to_datetime(self, date: list[str]) -> datetime.datetime:
"""Format the times provided by the api to datetime forms."""
try:
return datetime.datetime.strptime(date, "%Y-%m-%dT%H:%M:%S%z")
except ValueError:
# there is a possibility of an event not having a time, just a day
# to catch this, we try again without time information
return datetime.datetime.strptime(date, "%Y-%m-%d")
async def fetch_hanukkah_dates(self) -> list[datetime.date]:
"""Gets the dates for hanukkah festival."""
# clear the datetime objects to prevent a memory link
self.hanukkah_dates = []
async with self.bot.http_session.get(HEBCAL_URL) as response:
json_data = await response.json()
festivals = json_data["items"]
for festival in festivals:
if festival["title"].startswith("Chanukah"):
date = festival["date"]
self.hanukkah_dates.append(self._parse_time_to_datetime(date).date())
return self.hanukkah_dates
@in_month(Month.NOVEMBER, Month.DECEMBER)
@commands.command(name="hanukkah", aliases=("chanukah",))
async def hanukkah_festival(self, ctx: commands.Context) -> None:
"""Tells you about the Hanukkah Festivaltime of festival, festival day, etc)."""
hanukkah_dates = await self.fetch_hanukkah_dates()
start_day = hanukkah_dates[0]
end_day = hanukkah_dates[-1]
today = datetime.date.today()
embed = Embed(title="Hanukkah", colour=Colours.blue)
if start_day <= today <= end_day:
if start_day == today:
now = datetime.datetime.utcnow()
hours = now.hour + 4 # using only hours
hanukkah_start_hour = 18
if hours < hanukkah_start_hour:
embed.description = (
"Hanukkah hasnt started yet, "
f"it will start in about {hanukkah_start_hour - hours} hour/s."
)
await ctx.send(embed=embed)
return
elif hours > hanukkah_start_hour:
embed.description = (
"It is the starting day of Hanukkah! "
f"Its been {hours - hanukkah_start_hour} hours hanukkah started!"
)
await ctx.send(embed=embed)
return
festival_day = hanukkah_dates.index(today)
number_suffixes = ["st", "nd", "rd", "th"]
suffix = number_suffixes[festival_day - 1 if festival_day <= 3 else 3]
message = ":menorah:" * festival_day
embed.description = (
f"It is the {festival_day}{suffix} day of Hanukkah!\n{message}"
)
elif today < start_day:
format_start = start_day.strftime("%d of %B")
embed.description = (
"Hanukkah has not started yet. "
f"Hanukkah will start at sundown on {format_start}."
)
else:
format_end = end_day.strftime("%d of %B")
embed.description = (
"Looks like you missed Hanukkah! "
f"Hanukkah ended on {format_end}."
)
await ctx.send(embed=embed)
async def setup(bot: Bot) -> None:
"""Load the Hanukkah Embed Cog."""
await bot.add_cog(HanukkahEmbed(bot))
|