def get_url_if_newer()

in scripts/committee_info.py [0:0]


def get_url_if_newer(url, dir, name):
    path=join(dir,name)
    fileTime = file_mtime(path)
    check = join(dir,".checked_"+name)
    if fileTime >= 0:
        checkTime = file_mtime(check)
        now = time.time()
        if checkTime > (now - INTERVAL):
            print("Recently checked %d\n%d\n%d, skip check" % (INTERVAL, checkTime, now))
            return
        else:
            print("Not recently checked\n%d\n%d" % (checkTime, now))
    else:
        print("Not found %s" % name)

    sinceTime = mod_date(fileTime)
    headers = {"If-Modified-Since" : sinceTime}

    req = urllib.request.Request(url, headers=headers)
    try:
        response = urllib.request.urlopen(req)
        lastMod = response.headers['Last-Modified']
        lastModT = calendar.timegm(time.strptime(lastMod, HTTP_TIME_FORMAT))
        outFile = path + ".tmp"
        with open(outFile,'wb') as f:
            f.write(response.read())
            f.close()

        # store the last mod time as the time of the file
        os.utime(outFile, times=(lastModT, lastModT))
        os.rename(outFile, path) # seems to preserve file mod time
        print("Downloaded new version of %s " % path)
    except urllib.error.HTTPError as err:
        if not err.code == 304:
            raise
        else:
            print("Cached copy of %s is up to date" % path)

    with open(check,'a'):
        os.utime(check, None) # touch the marker file