2016-11-21 5 views
1

ローカルとリモートでホストされているファイルの最終更新日を比較するPythonスクリプトを実装しようとしています。Python - ローカルとリモートの2つのファイルの最終更新日を比較します。

リモートファイルはそれが必要新しい場合: - ローカルファイル を削除 - そのまま

最終更新日時とリモートファイルをダウンロードし、私はこれを見つけた最も近い答えがLast Modified of file downloaded does not match its HTTP headerである、しかし、私は信じていますこれはファイル全体をダウンロードするので、多くのリソース/時間を節約しません。

私がしたいのは、ファイル全体をダウンロードするのではなく、もっと速くすべきであると思うファイルを見直すことです。

私の現在のコードは非常に乱雑でnoobishです(文字列の置換などを参照してください)私は確かに、より良い/より速い方法です - あなたは何を提案できますか?

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)) 

答えて

1

ケースの誰もがこれを読み込むだけで、ここで私がなってしまったものです:

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)) 
関連する問題