0

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:

  1. 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:
  2. 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()

1 Answer 1

2

Unless I am understanding your question wrong this is what you are trying to do:

t_end = time.time() + 60 * 5

def first_attempt():
    now = datetime.now()
    return now.strftime("%H:%M:%S")

def sendEmail(date_time, content, product1, product2, product3):
    pass
    # send your email with the info you want

while time.time() < t_end:
    date_time = first_attempt()

    if "product 1" in r.text:
        print('product 1 available', date_time)
        product1 = [True, 'product 1 available']
    else:
        print('product 1 not in stock', date_time)
        product1 = [False, 'product 1 not in stock']

    if "product 2" in r.text:
        print('product 2 available', date_time)
        product2 = [True, 'product 2 available']
    else:
        print('product 2 not in stock', date_time)
        product2 = [False, 'product 2 not in stock']

    if "product 3" in r.text:
        print('product 3 available', date_time)
        product3 = [True, 'product 3 available']
    else:
        print('product 3 not in stock', date_time)
        product3 = [False, 'product 3 not in stock']

    if product1[0] or product2[0] or product3[0]:
        # you can choose to pass whatever info you want in order to construct
        # your email - here I pass the time, the content text, as well as boolean
        # values (True/False) for each product telling us if it is available or not
        sendEmail(date_time, r.text, product1, product2, product3)
        
    driver.refresh()
    time.sleep(30)

As you said in your question you can pass each product as a variable and then if any is True send the email with the parameters as all of the product variables. If you want to send the content.text simply set the product# variables equal to r.text or None and pass those.

I hope this answers your question.

Sign up to request clarification or add additional context in comments.

4 Comments

Great, thank you! I will try later today and mark as correct once i can get to work!
Timothee, this answer is great, I appreciate your help! The only other question I have is, for the final if statement that tests if anything is true, how would you pass along the outcome of the original if statement's print output's? Currently we are testing if anything is true and if so then sending an email with the boolean outcome, but how would you include the previous string output for each if statement per product? Thanks again!
If I understand correctly you want to pass in the string that you are printing to the email as well? If that is the case you can simply set the product# variables equal to a list (or other datatype like a tuple) that contains both the boolean value and the string. I updated my code above to reflect this. Product# variables are now lists where index 0 is the boolean value and index 1 is the string printed in the if statements.
thank you so much! I appreciate your help and time!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.