Laurel Link
Laurel Link

Reputation: 305

How to constantly update time variables in a Python Script?

I have an if statement below that I want to execute at exactly 11:45 am every single day. The problem is, when I run my Python script, result.tm_min and result.tm_hour are static, holding whatever time it was when I started the script in th first place. I need some way for these values to change in real-time with the clock. So when the time changes from 11:44 to 11:45, result.tm_min also changes from 44 to 45, allowing for the below if statement to execute. If I could get any help with this, that would be awesome.

I'm currently using the time and datetime libraries for this.

if result.tm_hour == 11:
        if result.tm_min == 45:

            post_number = random.randint(1, 5)
            noun_number = random.randint(1, noun_expand_count)
            verb_number = random.randint(1, verb_expand_count)

            noun_file = open("nouns.txt", "r")
            get_noun_line = noun_file.readlines()
            new_noun = get_noun_line[noun_number].strip()
            noun_file.close()

            verb_file = open("verbs.txt", "r")
            get_verb_line = verb_file.readlines()
            new_verb = get_verb_line[verb_number].strip()
            verb_file.close()

            post_file = open("things_to_do.txt", "r")
            get_post_line = post_file.readlines()
            new_post = get_post_line[post_number].strip()
            post_file.close

            message = "@joerogan Hello Joe, today's top two priorities are to:", new_post, new_verb, new_noun
            print(message)
            #api.update_status(message)

Edit: Okay, I did a pip install for the schedule module, tried to rewrite some code, but I'm not getting any output, at all.

def post():
post_number = random.randint(1, 5)
noun_number = random.randint(1, noun_expand_count)
verb_number = random.randint(1, verb_expand_count)

noun_file = open("nouns.txt", "r")
get_noun_line = noun_file.readlines()
new_noun = get_noun_line[noun_number].strip()
noun_file.close()

verb_file = open("verbs.txt", "r")
get_verb_line = verb_file.readlines()
new_verb = get_verb_line[verb_number].strip()
verb_file.close()

post_file = open("things_to_do.txt", "r")
get_post_line = post_file.readlines()
new_post = get_post_line[post_number].strip()
post_file.close

message = "@joerogan Hello Joe, today's top two priorities are to:", new_post, new_verb, new_noun
print(message)
#api.update_status(message)
return

class MyStreamListener(tweepy.StreamListener):
    def on_status(self, status):
        global noun_expand_count, verb_expand_count
        status = status._json['text']

        schedule.every().minute.do(post)

Upvotes: 0

Views: 1565

Answers (1)

John Gordon
John Gordon

Reputation: 33275

Recalculate the current time immediately before checking:

current = datetime.now()
if current.hour == 11 and current.minute == 45:
    # annoy Joe Rogan

However, as others have commented, it might be better to use a purpose-built task scheduling system such as cron.

Upvotes: 1

Related Questions