Skip to content

Commit

Permalink
Merge pull request #7 from wbfw3131/development
Browse files Browse the repository at this point in the history
Several new features
  • Loading branch information
wbfw3131 authored Jan 3, 2023
2 parents 531b29d + 1ef38cf commit 656ccee
Show file tree
Hide file tree
Showing 4 changed files with 61 additions and 21 deletions.
20 changes: 18 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -19,11 +19,27 @@ DISCORD_WEBHOOK_URL = https://discord.com/api/webhooks/...
## Usage

After setup, you can run [lunchfinder.py](lunchfinder.py) and [webhook.py](webhook.py) by themselves and they will ask for the proper arguments. [webhook.py](webhook.py) also accepts command-line arguments for school names and webhook URLs. Here are some examples of valid calls:
After setup, you can run [lunchfinder.py](lunchfinder.py) and [webhook.py](webhook.py) by themselves and they will ask for the proper arguments.

Dates need to be formatted as `MM/DD/YYYY`. The strings `"today"` and `"tomorrow"` can also be passed in as dates and will be processed correctly.

[webhook.py](webhook.py) also accepts command-line arguments for school names, webhook URLs, dates, and menu types.

> Default variable values:
>
> * Date: "today"
> * Menu: Lunch
>
> These can be omited from command line args, however, you still need to specify a school name (and a webhook URL if you haven't added it to the environment). The variable order doesn't matter.

Here are some examples of valid command line calls:
```shell
python webhook.py Cool High School #defaults to environment URL

python webhook.py Nice Middle School https://discord.com/api/webhooks/... #uses URL passed in
python webhook.py 3/14/2015 Nice Middle School https://discord.com/api/webhooks/...

python webhook.py https://discord.com/api/... Breakfast tomorrow East High School
```


Expand Down
4 changes: 3 additions & 1 deletion lunchfinder.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@


def dayLunch(day: str = "today", schoolStr: str | School = School("Provo High"), menu: MenuTypes | str = MenuTypes.LUNCH) -> Message:
"""Takes in a date string in MM/DD/YYYY format (or "today")
"""Takes in a date string in MM/DD/YYYY format (or "today" or "tomorrow")
Returns a string concatenated with all the items for lunch (except milks)"""

Expand All @@ -24,6 +24,8 @@ def dayLunch(day: str = "today", schoolStr: str | School = School("Provo High"),
if day.lower() == "today":
date = datetime.date.today()
#print(date)
elif day.lower() == "tomorrow":
date = datetime.date.today() + datetime.timedelta(days=1)
else:
date = datetime.datetime.strptime(day, '%m/%d/%Y').date()

Expand Down
3 changes: 2 additions & 1 deletion schools.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
{"name": "Dixon Middle", "siteCode1": 549, "siteCode2": 366, "iconURL": "https://dixon.provo.edu/wp-content/themes/dms-child/assets/images/favicon.png"},
{"name": "Timpview High", "siteCode1": 550, "siteCode2": 369, "iconURL": "https://timpview.provo.edu/wp-content/themes/timpview-child/assets/images/favicon.png"},
{"name": "Centennial Middle", "siteCode1": 549, "siteCode2": 365, "iconURL": "https://centennial.provo.edu/wp-content/themes/alyeska-child/favicon.png"},
{"name": "Westridge Elementary", "siteCode1": 3639, "siteCode2": 1095, "iconURL": "https://westridge.provo.edu/wp-content/themes/westridge-child/assets/images/header-logo.png"}
{"name": "Westridge Elementary", "siteCode1": 3639, "siteCode2": 1095, "iconURL": "https://westridge.provo.edu/wp-content/themes/westridge-child/assets/images/header-logo.png"},
{"name": "Rock Canyon Elementary", "siteCode1": 3639, "siteCode2": 1090, "iconURL": "https://rockcanyon.provo.edu/wp-content/themes/rockCanyon-child/assets/images/header-logo.png"}
]
}
55 changes: 38 additions & 17 deletions webhook.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,9 @@
from menuTypes import MenuTypes
from discord_webhook import DiscordWebhook
from urllib.parse import urlparse
import os, sys
from re import search
from os import abort, getenv
from sys import argv
import dotenv

#greenBulldogURL = "https://instructure-uploads.s3.amazonaws.com/account_17190000000000001/attachments/998339/Green%20Collar%20Bulldog.png"
Expand All @@ -20,13 +22,13 @@
#westridgeWrongColorsIconURL = "https://westridge.provo.edu/wp-content/themes/westridge-child/assets/images/favicon.png"
#westridgeWildcatURL = "https://westridge.provo.edu/wp-content/themes/westridge-child/assets/images/header-logo.png"

def main(schoolName: str, webhookURL: str | None, menu: str | MenuTypes = MenuTypes.LUNCH):
def main(schoolName: str, webhookURL: str | None, menu: str | MenuTypes = MenuTypes.LUNCH, date: str = "today"):

school = School(schoolName)
message = dayLunch(schoolStr=school, menu=menu)
message = dayLunch(schoolStr=school, menu=menu, day=date)
if not message.hasFood:
print("There is no lunch today, will not send a message")
os.abort()
print("There is no lunch that day, will not send a message")
abort()

#check to see if webhookURL is a valid Discord webhook link
if type(webhookURL) == str:
Expand Down Expand Up @@ -58,7 +60,7 @@ def getEnvVar() -> str:
else:
print("You have a .env file, but there's nothing defined inside")

envVar = os.getenv("DISCORD_WEBHOOK_URL")
envVar = getenv("DISCORD_WEBHOOK_URL")
if envVar == None:
raise ValueError("Could not find an environment variable for the webhook URL")
return envVar
Expand All @@ -68,22 +70,41 @@ def getEnvVar() -> str:

if __name__ == "__main__":
webhookLink = None
menuType = None
sys.argv.pop(0)
if len(sys.argv) == 0:

# defaults
menuType = MenuTypes.LUNCH
date = "today"

#remove unneeded arg
argv.pop(0)

if len(argv) == 0:
schoolStr = input("What is the name of the school you want to know the lunch of? ")
else:
testURL = urlparse(sys.argv[-1])
if testURL.netloc=="discord.com" and testURL.path.startswith("/api/webhooks/"):
webhookLink = sys.argv.pop(-1)
for arg in argv.copy():

# check if date
matchTest = search("today|tomorrow|(.{2}|.{1})\/(.{2}|.{1})\/(.{4}|.{2})", arg.lower())
if matchTest:
date = arg[matchTest.start():matchTest.end()]
argv.remove(arg)
continue

# check if webhook link
testURL = urlparse(arg)
if testURL.netloc=="discord.com" and testURL.path.startswith("/api/webhooks/"):
webhookLink = arg
argv.remove(arg)
continue

for index in (0, -1):
arg = sys.argv[index]
# check if menu
for menu in MenuTypes:
if arg.lower().find(menu.name.lower()) != -1:
menuType = sys.argv.pop(index)
menuType = menu
argv.remove(arg)
break

schoolStr = " ".join(sys.argv[0:])
# make school from remaining args
schoolStr = " ".join(argv[0:])

main(schoolStr, webhookLink, menuType)
main(schoolStr, webhookLink, menuType, date)

0 comments on commit 656ccee

Please sign in to comment.