I am running a script that is intended to run for x amount of time and during that time it is checking a website to see if the stock status for various products has updated to 'in stock'. My goal is to send an email with the outcome in the body of the email with the whole output from the function below (not just what is in stock) def first_attempt() anytime any product is in stock. For example, as long as at least 1 product is in stock, I want that to be the event that triggers the email that then takes the whole output, listing which product is in stock and which are out of stock. Also, I don't want this to stop the loop.
What I need help with:
- How do I write the next function that says as long as there is at least 1 instance of where output contains 'available', do something? Basically, I need to understand how to write, or I think write a 'while true' or 'if function contains' then do this:
- How do I grab the entire output/print from the
def first_attempt()function as the body of the email?
I realize I need to store it as a variable and then I can pass it as a parameter within the SMTP setup like {}, but I am blanking on how to set it up with the initial pass through.
I am not looking for help writing the SMTP email sections.
t_end = time.time() + 60 * 5
def first_attempt():
now = datetime.now()
date_time = now.strftime("%H:%M:%S")
if "product 1" in r.text:
print('product 1 available', date_time)
else:
print('product 1 not in stock', date_time)
if "product 2" in r.text:
print('product 2 available', date_time)
else:
print('product 2 not in stock', date_time)
if "product 3" in r.text:
print('product 3 available', date_time)
else:
print('product 3 not in stock', date_time)
driver.refresh()
time.sleep(30)
while time.time() < t_end:
first_attempt()