1

I'm trying to implement a python script that will compare the last modified dates of a local and remotely hosted file.

If the remote file is newer it should: - delete the local file - download the remote file with the last modified date intact

The closest answer I've found to this is Last Modified of file downloaded does not match its HTTP header, however I believe this downloads the whole file, so doesn't save much resource/time

What I'd like to do is just review the remote file's headers rather than download the whole file which I believe should be quicker.

Here's my current code, which is very messy and noobish (see string replace etc) I'm sure there's a better/quicker way - what can you suggest?

    remote_source = 'http://example.com/somefile.xml'
    local_source = 'path/to/myfile.xml'
    if path.exists(local_source):
        local_source_last_modified = os.path.getmtime(local_source)
        local_source_last_modified = datetime.datetime.fromtimestamp(local_source_last_modified).strftime('(%Y, %m, %d, %H, %M, %S)')
        conn = urllib.urlopen(remote_source)
        remote_source_last_modified = conn.info().getdate('last-modified')
        remote_source_last_modified = str(remote_source_last_modified)
        remote_source_last_modified = remote_source_last_modified.replace(", 0, 1, 0)", ")")
        if local_source_last_modified < remote_source_last_modified:
            pass
        else:
            headers = urlretrieve(remote_source, local_source)[1]
            lmStr = headers.getheader("Last-Modified")
            remote_source_last_modified = mktime(strptime(lmStr, "%a, %d %b %Y %H:%M:%S GMT"))
            os.utime(local_source, (remote_source_last_modified, remote_source_last_modified))
    else:
        headers = urlretrieve(remote_source, local_source)[1]
        lmStr = headers.getheader("Last-Modified")
        remote_source_last_modified = mktime(strptime(lmStr, "%a, %d %b %Y %H:%M:%S GMT"))
        os.utime(local_source, (remote_source_last_modified, remote_source_last_modified))
Community
  • 1
  • 1
user1358120
  • 193
  • 1
  • 4
  • 18

1 Answers1

2

Just in case anybody reads this, here's what I ended up with:

def syncCheck(file_path):
    remote_source = 'http://example.com/' + os.path.basename(file_path)
    local_source = file_path

    headers = {'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/54.0.2840.99 Safari/537.36'}
    response = requests.head(remote_source, headers = headers)
    remote_source_last_modified = response.headers["last-modified"]
    remote_source_last_modified = time.mktime(datetime.datetime.strptime(remote_source_last_modified[:-4], "%a, %d %b %Y %H:%M:%S").timetuple())

    try:
        if os.path.exists(local_source):
            local_source_last_modified = os.path.getmtime(local_source)
            if local_source_last_modified == remote_source_last_modified:
                break
            else:
                try:
                    os.remove(local_source)
                except:
                    break
                urlretrieve(remote_source, local_source)
                os.utime(local_source, (remote_source_last_modified, remote_source_last_modified))

    else:
        urlretrieve(remote_source, local_source)
        os.utime(local_source, (remote_source_last_modified, remote_source_last_modified))

    except HTTPError, e:
        print("HTTP Error: " + str(e.fp.read()))
    except URLError, e:
        print("URL Error: " + str(e.reason))
user1358120
  • 193
  • 1
  • 4
  • 18