From 453d91e9de92b505d6f11c0795a4d1ab48df14ae Mon Sep 17 00:00:00 2001 From: Connor Goodman Date: Tue, 29 Aug 2023 17:49:09 -0700 Subject: [PATCH] Worked on azure table integration --- birthdays.py | 14 +++++++++++++- data.json | 1 + loop.py | 33 ++++++++++++++++++++++++++++++--- main.py | 14 +++++++++----- utils.py | 15 +++++++++++++++ 5 files changed, 68 insertions(+), 9 deletions(-) create mode 100644 data.json diff --git a/birthdays.py b/birthdays.py index 0f714c4..3d54822 100644 --- a/birthdays.py +++ b/birthdays.py @@ -91,7 +91,7 @@ def userExistsJson(userId: str) -> bool: return True else: return False - + #this function sets a users birthday in the json file def setBirthdayJson(userId: str, birthday: datetime): with open('users.json', 'r') as f: @@ -113,3 +113,15 @@ def getBirthdayJson(userId: str) -> datetime: with open('users.json', 'r') as f: users = json.load(f) return datetime.strptime(users[userId], '%m/%d') + +def getAllBirthdaysWithDate(date: datetime) -> list[str]: + return getAllBirthdaysWithDateJson(date) + +def getAllBirthdaysWithDateJson(date: datetime) : + with open('users.json', 'r') as f: + users = json.load(f) + birthdays = [] + for user in users: + if (users[user] == date.strftime('%m/%d')): + birthdays.append(user) + return birthdays \ No newline at end of file diff --git a/data.json b/data.json new file mode 100644 index 0000000..280c586 --- /dev/null +++ b/data.json @@ -0,0 +1 @@ +{"currentDate": "08/29"} \ No newline at end of file diff --git a/loop.py b/loop.py index f396d83..7af4e1b 100644 --- a/loop.py +++ b/loop.py @@ -2,10 +2,37 @@ import activity import birthdays import asyncio - +import json async def everyMinute(): while True: print("looping") - - await asyncio.sleep(60) \ No newline at end of file + compareDates() + await asyncio.sleep(60) + +# open data.json and get the current date +# compare the current date to datetime now +# if the dates are different, update the currentDate in data.json +# check birthdays +# if the dates are the same, do nothing +def compareDates(): + print("comparing dates") + data = utils.openDataJSON() + currentDate = data["currentDate"] + now = utils.getCurrentDateTime() + nowDate = utils.convertDateTimeToDateString(now) + if (currentDate != nowDate): + print("updating stored date") + updateCurrentDate(nowDate) + todaysBirthdays = birthdays.getAllBirthdaysWithDate(now) + print("sending birthday messages") + sendBirthdayMessages(todaysBirthdays) + +def updateCurrentDate(nowDate: str): + data = utils.openDataJSON() + data["currentDate"] = nowDate + utils.writeDataJSON(data) + +def sendBirthdayMessages(todaysBirthdays): + for birthday in todaysBirthdays: + birthdayMessage = f"Happy Birthday {birthday}!" \ No newline at end of file diff --git a/main.py b/main.py index 43d43b5..5a754c6 100644 --- a/main.py +++ b/main.py @@ -5,19 +5,23 @@ import birthdays import utils import activity -import loop -from azure.storage.blob import BlobServiceClient, BlobClient, ContainerClient, __version__ +import loop from dotenv import load_dotenv +from azure.data.tables import TableServiceClient load_dotenv() client = discord.Client(command_prefix='?', intents=discord.Intents.all()) -connect_str = os.getenv('AZURE_BLOB_CONNECTION_STRING') +connect_str = os.getenv('AZURE_STORAGE_CONNECTION_STRING') storage_str = os.getenv('AZURE_STORAGE_ACCOUNT_NAME') -blob_service_client = BlobServiceClient.from_connection_string(connect_str) - +users_table_name = os.getenv('USERS_TABLE_NAME') +globla_data_table_name = os.getenv('GLOBAL_DATA_TABLE_NAME') +timezone = os.getenv('TIMEZONE') +table_service_client = TableServiceClient.from_connection_string(connect_str) +global_data_client = table_service_client.get_table_client(globla_data_table_name) +users_client = table_service_client.get_table_client(users_table_name) @client.event async def on_ready(): # runs on startup diff --git a/utils.py b/utils.py index 533bceb..b5e87b3 100644 --- a/utils.py +++ b/utils.py @@ -19,6 +19,8 @@ def toStrID(id:int) -> str: ############################# DateTime ########################################### +def getCurrentDateTime() -> datetime: + return datetime.now() def convertDateStringToDateTime(date: str) -> datetime: return datetime.strptime(date, '%m/%d') @@ -80,12 +82,25 @@ def openJSON() -> dict: f.close() return users +# opens JSON +def openDataJSON() -> dict: + f = open('data.json','r+') + data = json.load(f) + f.close() + return data + #writes a dict to a JSON def writeJSON(dic): f = open('users.json','r+') json.dump(dic, f) f.close() +#writes a dict to a JSON +def writeDataJSON(dic): + f = open('data.json','r+') + json.dump(dic, f) + f.close() + # checks if user is being tracked def isTracked(id:str) -> bool: jn = openJSON()