-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.py
122 lines (89 loc) · 3.46 KB
/
main.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
# Importing libraries
from urllib.request import Request, urlopen
import json
import datetime
import json
import discord
from discord import SyncWebhook
# The title of the embed message
# You can use 2 variables:
# dayEnglish is Monday
# dayFinnish is Maanantain
title = "dayEnglish food"
# Subtitles
lunchTitle = "Lunch"
vegLunchTitle = "Vegetarian lunch"
# See the github page for more info
customerID = 12345
# See the github page for more info
kitchenID = 1
# I haven't played around with this
language = "fi"
# You can have multiple webhooks the bot sends the embed to
webhooks = ["WEBHOOK URL HERE"]
# These items in the menu will be not be sent with the embed
ignoreItems = ["Aterialisät", "Tarkista aina sisältötiedot linjastosta!", "tai kysy henkilökunnalta"]
try:
# Gets the names of the days.
now = datetime.datetime.now()
dayEnglish = str(now.strftime("%A"))
if dayEnglish == "Saturday" or dayEnglish == "Sunday":
exit()
if dayEnglish == "Monday":
dayFinnish = "Maanantain"
elif dayEnglish == "Tuesday":
dayFinnish = "Tiistain"
elif dayEnglish == "Wednesday":
dayFinnish = "Keskiviikon"
elif dayEnglish == "Thursday":
dayFinnish = "Torstain"
elif dayEnglish == "Friday":
dayFinnish = "Perjantain"
# Date to specify the time frame in the request
urlDate = str(now.strftime("%Y%m%d"))
# Requests the JSON page
req = Request(
url="https://fi.jamix.cloud/apps/menuservice/rest/haku/menu/" + str(customerID) + "/" + str(kitchenID) + "?lang=" + str(language) + "&date=" + urlDate + "&date2=" + urlDate,
headers={'User-Agent': 'Mozilla/5.0'}
)
# Getting the JSON and the correct path for the menu items
webpage = urlopen(req)
pageJson = json.loads(webpage.read())
path = pageJson[0]["menuTypes"][0]["menus"][0]["days"][0]["mealoptions"]
# Adding menu items to lunch menus
lunchMenu = []
vegLunchMenu = []
loopCounting = 0
for mealOption in path:
loopCounting = loopCounting + 1
for mealOptionSub in mealOption["menuItems"]:
if mealOptionSub["name"] in ignoreItems:
continue
if loopCounting == 1:
lunchMenu.append(mealOptionSub["name"])
else:
vegLunchMenu.append(mealOptionSub["name"])
# Removing duplicates in the vegetarian menu
# V This can be removed if you want duplicate items V
for item in lunchMenu:
if item in vegLunchMenu:
vegLunchMenu.remove(item)
# ^ This can be removed if you want duplicate items ^
# This part sends the embeds to the webhooks
for webhook in webhooks:
# Titles
embed=discord.Embed(title=" ")
embed.set_author(name=title.replace("dayEnglish",dayEnglish).replace("dayFinnish",dayFinnish))
# Add commas and spaces
lunchMenu2 = ", ".join(lunchMenu)
vegLunchMenu2 = ", ".join(vegLunchMenu)
#
embed.add_field(name=lunchTitle, value=lunchMenu2, inline=False)
if len(vegLunchMenu) != 0:
embed.add_field(name=vegLunchTitle, value=vegLunchMenu2, inline=False)
# Send embed to webhook
webhook = SyncWebhook.from_url(webhook)
webhook.send(embed=embed)
# Error handling
except Exception as e:
print("Something went wrong: " + str(e))