Skip to content

Instantly share code, notes, and snippets.

@mralext20
Created March 5, 2021 19:02
Show Gist options
  • Save mralext20/2e774d5ba403ac31d27d77bcdbf38a8a to your computer and use it in GitHub Desktop.
Save mralext20/2e774d5ba403ac31d27d77bcdbf38a8a to your computer and use it in GitHub Desktop.
School cafe discord poster
from datetime import date as Date
import requests as req
import json
from tokens import discord_alex # a discord token
from tokens import post_discord # a method to post to a discord channel a string
def get_items(menu, category):
ret_list = []
for i in menu[category]:
ret_list.append(i["MenuItemDescription"])
return ret_list
def send_menu(channel, token, school_id, meal="Lunch", date=None):
if date is None:
today = Date.today().strftime("%x")
else:
today = date
url = 'https://apis.schoolcafe.com/api/CalendarView/GetDailyMenuitems'
params = {'MealType': meal,
'SchoolId': school_id,
'ServingDate': today,
'ServingLine': 'Line 1'}
res = req.get(url, params=params).text
if res == "{}":
return
menu = json.loads(res)
# Remove Milk and condiments
for i in ("MILK", "CONDIMENTS"):
menu.pop(i, None)
# loop to take the items in a category and return a strong independent string
pass
things = {k: get_items(menu, k) for k in[i for i in menu]}
out = []
for key, value in things.items():
out.append(key + '\n')
out.extend(["\t {} \n".format(x) for x in value])
ret = "".join(out)
del out
for i, i2 in [["ENTREE", "**Entrees:**"], ["VEGETABLE", "**Vegetables:**"], ["FRUIT", "**Fruit:**"],
["BREAD/GRAIN", "**Bread:**"], ["MEAT/MEAT ALT", "**Yogurt:**"], ["WG", "White Grain"],
["Fruit, PEAR", "Pear"], ["Yogurt, Assorted", "Assorted"]]:
ret = ret.replace(i, i2)
post_discord(token, channel, ret)
return ret
if __name__ == "__main__":
CHS = '680af517-3702-46ec-9a11-f1c3de2025cb'
send_menu(345494549881421825, discord_alex, CHS, "Lunch") # CHS high lunch
send_menu(439101868103434241, discord_alex, CHS, "Breakfast") # CHS Breakfast
discord=""
schoolCafe="Bearer undefined"
discord_alex=""
discord_musicbot=""
def post_discord(token,channel, str):
""" postes to a channel a message """
import requests
# this import is inside here so you can import the tokens from above without needing to have requests in your enviroment
return requests.post(f'https://discordapp.com/api/v7/channels/{channel}/messages',
json={"content":str},
headers={"User-Agent":"Alexs python scripts",
"Authorization":token})
@ABUCKY0
Copy link

ABUCKY0 commented Apr 16, 2022

How did you find your School ID number

@AllPurposeBen
Copy link

AllPurposeBen commented Aug 10, 2022

@ABUCKY0 I was able to find mine by logging into SchoolCafe, pulling up the calendar view and then opening the browser inspector (in my case in Safari) and looking in Storage -> Session Storage -> www.schoolcafe.com and at the key "students". The value is a json object that as the key "SchoolID" and a value like: xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx

The value for "ServingLine" was different for me that what's hardcoded. You can see it in the web calendar view, mine defaults to "Regular" but I just use "All Lines" generically.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment