diff --git a/mod_birthdayreminder.py b/mod_birthdayreminder.py index 63c60a8be232aec66ed1bba5d6f62a71ba1db365..b12abe0eedbfafacac4d9473793afd92be74729e 100644 --- a/mod_birthdayreminder.py +++ b/mod_birthdayreminder.py @@ -17,20 +17,19 @@ class Birthday: def __repr__(self): return f'{self.__class__.__name__}: {self.PERSON} {self.DATE}' - + # ist nur python 2 => muss für __lt__ als python 3 neugeschrieben werden - #def __cmp__(self, other): + # def __cmp__(self, other): # if hasattr(other, 'DATE') and hasattr(other, 'PERSON'): # splittedDateSelf = self.DATE.split('.') # splittedDateOther = other.DATE.split('.') # comp = splittedDateSelf[1].__cmp__(splittedDateOther[1]) # compare month # if comp == 0: # comp = splittedDateSelf[0].__cmp__(splittedDateOther[0]) # compare day - ## if comp == 0: + # if comp == 0: # comp = self.PERSON.__cmp__(other.PERSON) # compare name - # + # # return comp - def get_congratulation(self): return f"Alles Gute {self.PERSON} ❤" @@ -80,7 +79,8 @@ class ModuleBirthdayReminder: if dm == birthday.DATE: for group in self.GROUPS: if group["NAME"] == key: - self.send(birthday.get_congratulation(), group["ID"]) + self.send( + birthday.get_congratulation(), group["ID"]) break def get_next_birthday(self, groupInfo): @@ -93,7 +93,8 @@ class ModuleBirthdayReminder: # Dieses Jahr birthday_string = f"{birthday.DATE}.{date.today().strftime('%Y')}" birthday_dt = datetime.strptime(birthday_string, format) - delta_temp = (birthday_dt - today).days + + delta_temp = (birthday_dt - today).days + 1 # Damit er bei 47h nicht 1 Tag sagt if 0 < delta_temp < delta: next_birthday = birthday delta = delta_temp @@ -101,20 +102,25 @@ class ModuleBirthdayReminder: # Nächstes Jahr (Für den Jahresüberlauf) next_year = date.today() + relativedelta(years=1) birthday_next_year_string = f"{birthday.DATE}.{next_year.strftime('%Y')}" - birthday_next_year = datetime.strptime(birthday_next_year_string, format) - delta_temp = (birthday_next_year - today).days + birthday_next_year = datetime.strptime( + birthday_next_year_string, format) + delta_temp = (birthday_next_year - today).days + 1 # Damit er bei 47h nicht 1 Tag sagt if delta_temp < delta: next_birthday = birthday delta = delta_temp message = "Niemand hat hier Geburtstag! Wir sind in einer Zeitschleife gefangen." + if next_birthday: - message = f"Als nächstes hat {next_birthday.PERSON} am {next_birthday.DATE} Geburtstag (noch {delta} Tage)" + if delta == 1: + message = f"Morgen hat {next_birthday.PERSON} Geburtstag!🎉" + else: + message = f"Als nächstes hat {next_birthday.PERSON} am {next_birthday.DATE} Geburtstag (noch {delta} Tage)" return message - def get_birthdaylist(self,groupInfo ): + def get_birthdaylist(self, groupInfo): message = "Alle Geburtstage:\n" liste = self.birthday_list[groupInfo["NAME"]] - #for birthday in liste.sort(): + # for birthday in liste.sort(): for birthday in liste: message += f"{birthday.PERSON}, {birthday.DATE}\n" @@ -123,20 +129,24 @@ class ModuleBirthdayReminder: def interpret_birthday_line(self, line): splitted = line.strip().split(",") if len(splitted) != 2: - raise SyntaxError("Invalid birthday syntax! Needs to be: person, date(Day.Month) eg.: Karl, 02.09") + raise SyntaxError( + "Invalid birthday syntax! Needs to be: person, date(Day.Month) eg.: Karl, 02.09") person = splitted[0].strip() if len(person) == 0: - raise SyntaxError("No name given! Needs to be: person, date(Day.Month) eg.: Karl, 02.09") + raise SyntaxError( + "No name given! Needs to be: person, date(Day.Month) eg.: Karl, 02.09") splittedDate = splitted[1].strip().split(".") if len(splitted) != 2: - raise SyntaxError("Invalid date! Needs to be: person, date(Day.Month) eg.: Karl, 02.09") + raise SyntaxError( + "Invalid date! Needs to be: person, date(Day.Month) eg.: Karl, 02.09") day = splittedDate[0].strip() month = splittedDate[1].strip() if len(day) > 2 or len(month) > 2 or len(day) == 0 or len(month) == 0: - raise SyntaxError("Invalid date! Needs to be: person, date(Day.Month) eg.: Karl, 02.09") + raise SyntaxError( + "Invalid date! Needs to be: person, date(Day.Month) eg.: Karl, 02.09") if len(day) == 1: day = f"0{day}" if len(month) == 1: @@ -150,12 +160,13 @@ class ModuleBirthdayReminder: try: birthday = self.interpret_birthday_line(message) - if birthday in self.birthday_list[groupInfo["NAME"]]: + if birthday in self.birthday_list[groupInfo["NAME"]]: return "Birthday is already saved" self.birthday_list[groupInfo["NAME"]].append(birthday) except (SyntaxError, ValueError) as exc: - self.log.warning(f"{self.tag}Error while interpreting birthday line:{message}; Error: {exc}") + self.log.warning( + f"{self.tag}Error while interpreting birthday line:{message}; Error: {exc}") return f"{exc}" try: @@ -163,5 +174,6 @@ class ModuleBirthdayReminder: f.write(f"{birthday.get_csv_line()}\n") return "Successfully added birthday" except IOError as exc: - self.log.error(f"{self.tag}Adding new birthday'{birthday.get_csv_line()}' to file failed: {exc}") + self.log.error( + f"{self.tag}Adding new birthday'{birthday.get_csv_line()}' to file failed: {exc}") return "Saving birthday failed"