aboutsummaryrefslogtreecommitdiffstats
path: root/bot/seasons/christmas/hanukkah_embed.py
blob: 85d916754645d5ba5706be395d0c7c2db2fe918b (plain) (blame)
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
import datetime
import logging

import requests
from discord import Embed
from discord.ext import commands

from bot.constants import Colours


log = logging.getLogger(__name__)


class HanukkahEmbed(commands.Cog):
    """A cog that returns information about Hanukkah festival."""
    def __init__(self, bot):
        self.bot = bot
        self.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")
        self.hanukkah_dates = self.get_hanukkah_dates()
        self.hanukkah_days = []
        self.hanukkah_months = []
        self.hanukkah_years = []

    def get_hanukkah_dates(self):
        """Gets the dates for hanukkah festival."""
        hanukkah_dates = []
        r = requests.get(self.url)
        json_data = r.json()
        festivals = json_data['items']
        for festival in festivals:
            if festival['title'].startswith('Chanukah'):
                date = festival['date']
                hanukkah_dates.append(date)
        return hanukkah_dates

    @commands.command(name='hanukkah', aliases=['chanukah'])
    async def hanukkah_festival(self, ctx):
        """Tells you about the Hanukkah Festivaltime of festival, festival day, etc)."""
        self.hanukkah_dates_split()

        hanukkah_start_day = int(self.hanukkah_days[0])
        hanukkah_start_month = int(self.hanukkah_months[0])
        hanukkah_start_year = int(self.hanukkah_years[0])
        hanukkah_end_day = int(self.hanukkah_days[8])
        hanukkah_end_month = int(self.hanukkah_months[8])
        hanukkah_end_year = int(self.hanukkah_years[8])

        hanukkah_start = datetime.date(hanukkah_start_year, hanukkah_start_month, hanukkah_start_day)
        hanukkah_end = datetime.date(hanukkah_end_year, hanukkah_end_month, hanukkah_end_day)
        today = datetime.date.today()
        # today = datetime.date(2019, 12, 24) (for testing)
        day = str(today.day)
        month = str(today.month)
        year = str(today.year)
        embed = Embed()
        embed.title = 'Hanukkah Embed'
        embed.colour = Colours.blue
        if day in self.hanukkah_days and month in self.hanukkah_months and year in self.hanukkah_years:
            if int(day) == hanukkah_start_day:
                now = datetime.datetime.utcnow()
                now = str(now)
                hours = int(now[11:13]) + 4  # using only hours
                hanukkah_start_hour = 18
                if hours < hanukkah_start_hour:
                    embed.description = (f"Hanukkah hasnt started yet, "
                                         f"it will start in about {hanukkah_start_hour-hours} hour/s.")
                    return await ctx.send(embed=embed)
                elif hours > hanukkah_start_hour:
                    embed.description = (f'It is the starting day of Hanukkah ! '
                                         f'Its been {hours-hanukkah_start_hour} hours hanukkah started !')
                    return await ctx.send(embed=embed)
            festival_day = self.hanukkah_days.index(day)
            number_suffixes = ['st', 'nd', 'rd', 'th']
            suffix = ''
            if int(festival_day) == 1:
                suffix = number_suffixes[0]
            if int(festival_day) == 2:
                suffix = number_suffixes[1]
            if int(festival_day) == 3:
                suffix = number_suffixes[2]
            if int(festival_day) > 3:
                suffix = number_suffixes[3]
            message = ''
            for _ in range(1, festival_day + 1):
                message += ':menorah:'
            embed.description = f'It is the {festival_day}{suffix} day of Hanukkah ! \n {message}'
            await ctx.send(embed=embed)
        else:
            if today < hanukkah_start:
                festival_starting_month = hanukkah_start.strftime('%B')
                embed.description = (f"Hanukkah has not started yet. "
                                     f"Hanukkah will start at sundown on {hanukkah_start_day}th "
                                     f"of {festival_starting_month}.")
            else:
                festival_end_month = hanukkah_end.strftime('%B')
                embed.description = (f"Looks like you missed Hanukkah !"
                                     f"Hanukkah ended on {hanukkah_end_day}th of {festival_end_month}.")

            await ctx.send(embed=embed)

    def hanukkah_dates_split(self):
        """We are splitting the dates for hanukkah into days, months and years."""
        for date in self.hanukkah_dates:
            self.hanukkah_days.append(date[8:10])
            self.hanukkah_months.append(date[5:7])
            self.hanukkah_years.append(date[0:4])


def setup(bot):
    bot.add_cog(HanukkahEmbed(bot))
    log.info("Hanukkah embed cog loaded")