Skip to content

Commit

Permalink
Works fine, wrote the tests
Browse files Browse the repository at this point in the history
  • Loading branch information
olech2412 committed Aug 23, 2022
1 parent 01bafeb commit 5855043
Show file tree
Hide file tree
Showing 5 changed files with 62 additions and 97 deletions.
55 changes: 4 additions & 51 deletions essensGetter.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,14 +18,8 @@ def give_me_everything():
return data


# Fetches the prices from the website
def fetch_prices():
data = soup.find_all(class_="meals__price")
return data


# Fetches the category from the food
def fetch_food_as_lists():
def fetch_food_from_website():
html_class_meals = soup.find_all(class_="meals")[0].__getattribute__("contents")
food_categorys = soup.find_all(class_="title-prim")

Expand All @@ -52,53 +46,13 @@ def fetch_food_as_lists():
return list_of_all_meals



# Fetches the names from the food
def fetch_food():
data = soup.find_all(class_="meals__name")
list_of_food = list()
beilagen = list()
for x in range(len(data)):
try:
list_of_food.append(data[x].__getattribute__("contents")[0])
if data[x].findNext(class_="u-list-bare").__getattribute__("contents")[0] is not None:
try:
for i in range(len(data[x].findNext(class_="u-list-bare").__getattribute__("contents"))):
if data[x].findNext(class_="u-list-bare").__getattribute__("contents")[i] != "\n":
beilagen.append(
data[x].findNext(class_="u-list-bare").__getattribute__("contents")[i].get_text())
if i + 1 == len(data[x].findNext(class_="u-list-bare").__getattribute__("contents")):
if (len(beilagen) == 1):
list_of_food.append(beilagen[0])
elif (len(beilagen) > 1):
beilagen = ", ".join(beilagen)
list_of_food.append(beilagen)
else:
list_of_food.append("")
beilagen = list()
except Exception as e:
logging.warning("Warn: " + str(e) + " in " + str(data[x]))
print("Warn: " + str(e) + " in " + str(data[x]))
beilagen.append("")
else:
logging.warning("No beilagen found for meal: " + data[x].get_text())
print("No beilagen found for meal: " + data[x].get_text())
list_of_food.append("")
except AttributeError as attribute_error:
logging.warning("AttributeError: " + str(attribute_error) + " in " + str(data[x]))
print("AttributeError: " + str(attribute_error) + " in " + str(data[x]))
list_of_food.append("Keine Beilagen")

return list_of_food


# don't do anything on weekends
if calendar.day_name[datetime.date.today().weekday()] == "Saturday" \
or calendar.day_name[datetime.date.today().weekday()] == "Sunday":
logging.info("Weekend -> no call on website and no other operations")
print("Weekend -> no call on website and no other operations")
else:
url = "https://www.studentenwerk-leipzig.de/mensen-cafeterien/speiseplan?location=140&date=2022-08-26&criteria=&meal_type=all" # URL
url = "https://www.studentenwerk-leipzig.de/mensen-cafeterien/speiseplan?location=140" # URL
session = HTMLSession() # Initialize HTML Session
response = session.get(url) # call the URL

Expand All @@ -109,6 +63,5 @@ def fetch_food():

# give_me_everything() # Important to know which property's you can extract
# convert the HTML List to usable data
food = format_meals_from_list(fetch_food_as_lists())
foodprice = format_food_price(fetch_prices()) # call the function to convert the HTML Stuff to usable data
send_Email(food=fetch_food(), foodcategory=fetch_food_as_lists(), foodprice=foodprice)
meals = format_meals_from_list(fetch_food_from_website())
send_Email(meals)
26 changes: 3 additions & 23 deletions tests/Test_essensGetter.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,29 +8,9 @@ def test_give_me_everything(self):
self.assertIsNot(essensGetter.give_me_everything(), None)
self.assertIsNot(essensGetter.give_me_everything(), [])

def test_fetch_prices(self):
self.assertIsNot(essensGetter.fetch_prices(), None)
self.assertIsNot(essensGetter.fetch_prices(), [])
self.assertGreater(len(essensGetter.fetch_prices()), 0)

if len(essensGetter.fetch_prices()) == 1:
self.assertGreater(len(essensGetter.fetch_food()), 1)
self.assertLess(len(essensGetter.fetch_food()), 3)
self.assertEqual(len(essensGetter.fetch_food_as_lists()), 1)
elif len(essensGetter.fetch_prices()) == 2:
self.assertGreater(len(essensGetter.fetch_food()), 2)
self.assertLess(len(essensGetter.fetch_food()), 5)
self.assertEqual(len(essensGetter.fetch_food_as_lists()), 2)

def test_fetch_food_category(self):
self.assertIsNot(essensGetter.fetch_food_as_lists(), None)
self.assertIsNot(essensGetter.fetch_food_as_lists(), [])
self.assertGreater(len(essensGetter.fetch_food_as_lists()), 0)

def test_fetch_food(self):
self.assertIsNot(essensGetter.fetch_food(), None)
self.assertIsNot(essensGetter.fetch_food(), [])
self.assertGreater(len(essensGetter.fetch_food()), 0)
def test_fetch_food_from_website(self):
self.assertIsNot(essensGetter.fetch_food_from_website(), None)
self.assertIsNot(essensGetter.fetch_food_from_website(), [])


if __name__ == '__main__':
Expand Down
37 changes: 33 additions & 4 deletions tests/Test_formatting.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,41 @@
import utils.formatting as formatting
import essensGetter


class Test_formatting(unittest.TestCase):

def test_format_price(self):
self.assertFalse(str(formatting.format_food_price(essensGetter.fetch_food())).__contains__("<p"))
self.assertFalse(formatting.format_food_price(formatting.format_meals_from_list(essensGetter.fetch_food_from_website())[0]["price"]).__contains__("<p"))
self.assertFalse(formatting.format_food_price(formatting.format_meals_from_list(essensGetter.fetch_food_from_website())[0]["price"]).__contains__("</p"))
self.assertFalse(formatting.format_food_price(formatting.format_meals_from_list(essensGetter.fetch_food_from_website())[0]["price"]).__contains__("title-prim"))

def test_format_string(self):
self.assertTrue(str(formatting.format_string(essensGetter.fetch_food())).isascii())
self.assertTrue(str(formatting.format_string(essensGetter.fetch_food_as_lists())).isascii())
self.assertTrue(str(formatting.format_string(essensGetter.fetch_prices())).isascii())
for x in formatting.format_meals_from_list(essensGetter.fetch_food_from_website()):
for y in x["price"]:
self.assertTrue(formatting.format_string(str(y)).isascii())

self.assertTrue(formatting.format_string(str(x["category"])).isascii())
for y in x["food"]:
self.assertTrue(formatting.format_string(str(y)).isascii())
for y in x["beilagen"]:
self.assertTrue(formatting.format_string(str(y)).isascii())

if len(formatting.format_meals_from_list(essensGetter.fetch_food_from_website())) == 4:
self.assertRaises(KeyError, formatting.format_meals_from_list(essensGetter.fetch_food_from_website())[0]["additional_info"])
elif len(formatting.format_meals_from_list(essensGetter.fetch_food_from_website())) == 5:
self.assertTrue(str(formatting.format_string(formatting.format_meals_from_list(essensGetter.fetch_food_from_website())[0]["additional_info"])).isascii())

def test_format_meals_from_list(self):
for x in formatting.format_meals_from_list(essensGetter.fetch_food_from_website()):
self.assertTrue(x.__contains__("category"))
self.assertTrue(x.__contains__("food"))
self.assertTrue(x.__contains__("beilagen"))
self.assertTrue(x.__contains__("price"))
if len(x) == 5:
self.assertTrue(str(x.__contains__("additional_info")))
elif len(x) == 4:
self.assertFalse(x.__contains__("additional_info"))


if __name__ == '__main__':
unittest.main()
1 change: 1 addition & 0 deletions utils/formatting.py
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,7 @@ def format_string(nonAsciiStr):
nonAsciiStr = nonAsciiStr.replace("Ô", "O")
nonAsciiStr = nonAsciiStr.replace("Û", "U")
nonAsciiStr = nonAsciiStr.replace("€", "Euro")
nonAsciiStr = nonAsciiStr.replace("\"", "")
logging.debug("After formatting: " + str(nonAsciiStr) + "\n")
return nonAsciiStr

Expand Down
40 changes: 21 additions & 19 deletions utils/mail.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,26 +7,27 @@
format='%(asctime)s %(levelname)s - %(message)s', force=True, encoding='utf-8')


def send_Email(food, foodcategory, foodprice):




# Gericht 1
food1 = format_string(foodcategory[0]) + " (" + format_string(foodprice[0]) + ")" + ": " \
+ format_string(food[0]) + " [" + format_string(food[1]) + "]"
logging.info("Food1: " + food1)
print("Food1: " + food1)
# Gericht 2
food2 = format_string(foodcategory[1]) + " (" + format_string(foodprice[1]) + ")" \
+ ": " + format_string(food[2]) + " [" + format_string(food[3]) + "]"
logging.info("Food2: " + food2)
print("Food2: " + food2)
def send_Email(meals):
actual_meals = list()
for x in meals:
if len(x) == 5:
food_list_conv = list()

for y in x["food"]:
food_list_conv.append(format_string(y) + " - " + format_string(x["price"][x["food"].index(y)]) + "\n")

food = format_string(str(x["category"]) + " --" + str(x["additional_info"])) + "-- : \n" + "".join(
food_list_conv)
actual_meals.append(food)
elif len(x) == 4:
food = format_string(str(x["category"])) + ": " + "\n" + format_string(str(x["food"])) \
+ " [" + format_string(str(x["beilagen"])) + "]" + " - " + format_string(str(x["price"])) \
+ "\n"
actual_meals.append(food)

current_day = str(datetime.date.strftime(datetime.date.today(), "%d.%m.%Y"))

# add all Emails an Names to the list
# TODO maybe use a dictionary instead of a list
# add all Emails and Names to the list
try:
with open("utils/receivers") as file:
receivers = list()
Expand All @@ -46,7 +47,8 @@ def send_Email(food, foodcategory, foodprice):
names.append(receivers[x])

content = "Moin {}, \n \n" + "Schau dir an was es heute in der Kantine (Schoenauer Strasse) zu essen gibt: \n \n" \
+ food1 + "\n" + food2 + "\n \n" + "Bis denne," + "\n" + "dein Food-Bot - " + current_day
+ actual_meals[0] + "\n" + actual_meals[
1] + "\n \n" + "Bis denne," + "\n" + "dein Food-Bot - " + current_day

logging.info("Content: " + content)

Expand All @@ -69,7 +71,7 @@ def send_Email(food, foodcategory, foodprice):

for i in range(len(names)):
message = 'Subject: {}\n\n{}'.format(SUBJECT.format(names[i]), content.format(names[i]))
#smtpObj.sendmail(sender, emails[i], message)
smtpObj.sendmail(sender, emails[i], message)

smtpObj.quit()
logging.info("Email sent successfully to: " + str(names))
Expand Down

0 comments on commit 5855043

Please sign in to comment.