Created
August 3, 2023 16:05
-
-
Save drowsy-probius/bfd7dae78e1b3ddfdbd9179af6296c3a to your computer and use it in GitHub Desktop.
Python3 script that reminds you how many days passed since the given date. (No 3rd party library dependencies)
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env python3 | |
import time | |
from datetime import datetime, timezone, timedelta | |
import requests | |
import traceback | |
#################################################### | |
# SET THOSE VALUES | |
MENTION = "@here" | |
START_DATE_STRING = "2023-01-01" | |
DISCORD_WEBHOOK = "your-discord-webhook-url" | |
ALERT_UNTIL_BEFORE = 7 | |
SPECIAL_DATE_MESSAGES = { | |
"05-21": { | |
"match": "5/21 is your birthday", | |
"before": "{} days left until your birtuday(5/21)", | |
}, | |
} | |
#################################################### | |
TIMEZONE = timezone(timedelta(hours=9)) | |
START_DATE = datetime.strptime( | |
START_DATE_STRING, "%Y-%m-%d" | |
).astimezone(TIMEZONE) | |
def retry_n_times(func, sleep_secs=5, n=3): | |
tries = 0 | |
while True: | |
try: | |
return func() | |
except Exception as e: | |
if tries < n: | |
tries += 1 | |
time.sleep(sleep_secs) | |
else: | |
raise e | |
if __name__ == "__main__": | |
MESSAGE = "" | |
DEFAULT_MESSAGE = "" | |
ADDITIONAL_MESSAGE = "" | |
today = datetime.now(tz=TIMEZONE) | |
day_diff = (today - START_DATE).days + 1 | |
DEFAULT_MESSAGE = f"It has been {day_diff} days passed since {START_DATE_STRING}." | |
for date_key, date_message in SPECIAL_DATE_MESSAGES.items(): | |
date_key_date = datetime.strptime(f"{today.year}-{date_key}", "%Y-%m-%d").astimezone(TIMEZONE) | |
day_diff = (date_key_date - today).days + 1 | |
if date_key_date.month == today.month and \ | |
date_key_date.day == today.day: | |
ADDITIONAL_MESSAGE += f"\n- {date_message['match']}" | |
elif 0 < day_diff <= ALERT_UNTIL_BEFORE: | |
ADDITIONAL_MESSAGE += f"\n- {date_message['before'].format(day_diff)}" | |
def sender(): | |
MESSAGE = (f"{MENTION}\n" if len(ADDITIONAL_MESSAGE) > 0 else "") \ | |
+ DEFAULT_MESSAGE \ | |
+ ADDITIONAL_MESSAGE | |
data = { | |
"content": MESSAGE | |
} | |
res = requests.post(DISCORD_WEBHOOK, json=data) | |
res.raise_for_status() | |
try: | |
retry_n_times(sender) | |
except Exception as e: | |
traceback.print_exc() | |
print(MESSAGE) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Crontab example