Time limiting updates (python)

After working with a very limited weather API (10 calls per minute) from wunderground (sign up here). I wanted to add some code into my weather app so it wouldn’t call the API if it didn’t need to.

Here is an example, you call the API for the current weather it saves that to a text file and the time it was pulled at to another text file. If you want to check the weather again in 10 seconds it checks the text file where you save the time to to see what time it was pulled and compares that to the time delta 10 minuets from the last time the weather was pulled.  The weather wouldn’t have changes so it pulls the data from the text file instead and saves you going over the API calls. I set it to not pull the weather data until 10 mins has passed since first pull or if you set a new city it will pull that new data. let’s have a look at the code in this example

from time import strftime
from datetime import timedelta, datetime
import os

# Reads the file
read_it = open ('test.txt', 'r')
reading = read_it.read()
# loads the last_time_run
time_last_run = datetime.strptime(reading, '%Y-%m-%d %H:%M:%S')

# Gets the time for when you this script
now = strftime('%Y-%m-%d %H:%M:%S')

# Checks to see if the file has anything in it and
lenthlist = os.stat("test.txt").st_size

# If the file doesn't have anything in it, it write the 'now' time to the file
if lenthlist <=0:
    write_it = open ('test.txt', 'w')
# If it does have something in the file it carry's on as normal
    # Print the 'now' time so we can see to debug
    print "now %s" % now
    print "time_last_run %s" % time_last_run

    # Adds the time delta this can be determined by changing the number or changing it to seconds if you want a shorter time
    next_update = time_last_run + timedelta(minutes=10)
    # Prints it out for debugging
    print "next_update %s" % next_update

    # This is where the magic happens, if the next_update is equal to or higher than the 'now' time it will not update
    if now <= str(next_update):
        print "\nNot updating"

    # But if the next_update is less or equal to the 'now' time it will update
    elif now >= str(next_update):
        print "\nUpdating"
        write_it = open ('test.txt', 'w')
    # Incase something goes wrong
    print "something went wrong"

What I struggled with was trying to write the time format to file and reading back as the time format. I found the strptime function was the answer to my problem.

# Reads the file
read_it = open ('test.txt', 'r')
reading = read_it.read()
# loads the last_time_run
time_last_run = datetime.strptime(reading, '%Y-%m-%d %H:%M:%S')

What it does is read a string as a time format, this allows me to compare the last_checked time on the file against the time delta and seeing if has passed.

If you want to test out the code copy it into your favourite text editor then save it. next create a .txt file called test.txt this is where the time will be stored.


Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s