2011-09-12 31 views
7

我試圖看看HTML5應用程序是如何工作的,任何試圖在webkit瀏覽器(chrome,Safari)中保存頁面的嘗試都包含一些但不是全部的cache.manifest資源。是否有一個庫或一組代碼將解析cache.manifest文件,並下載所有資源(圖像,腳本,CSS)?下載HTML5 cache.manifest文件中列出的所有工件的最佳方法?

(原代碼移動來回答......小白錯誤> <。)

+1

在你的問題中缺少「questioness」,但代碼看起來可以運行Python,儘管一些部分可以簡化。還有一些名爲urlgrabber的庫和可以使文件保存過程更容易的請求。 –

+0

感謝您的反饋Mikko我會檢查你提到的圖書館進一步發展。所以基本上,你不知道一個庫下載cache.manifest文件內的資源列表。不幸的是,這篇文章中最重要的關鍵字是「cache.manifest」,它顯然不會被添加爲關鍵字。沒有1500分我不能添加它。 >。不幸的是,這個問題將會被正在看「python」標籤的人看到,而不是那些對HTML5「cache.manifest」感興趣的人。 – rockhowse

+0

由於解析和下載緩存清單隻有50行Python代碼,我不明白爲什麼有人應該爲此目的創建一個利基庫:) –

回答

0

我最初發布這是問題的一部分......(沒有新手計算器海報EVER做到這一點;)

自有一個響亮的答案缺乏。在這裏你去:

我能想出下面的Python腳本來做到這一點,但任何輸入,將不勝感激=)(這是我在Python代碼首次嘗試這樣有可能是一個更好的辦法)

import os 
import urllib2 
import urllib 

cmServerURL = 'http://<serverURL>:<port>/<path-to-cache.manifest>' 

# download file code taken from stackoverflow 
# http://stackoverflow.com/questions/22676/how-do-i-download-a-file-over-http-using-python 
def loadURL(url, dirToSave): 
     file_name = url.split('/')[-1] 
     u = urllib2.urlopen(url) 
     f = open(dirToSave, 'wb') 
     meta = u.info() 
     file_size = int(meta.getheaders("Content-Length")[0]) 
     print "Downloading: %s Bytes: %s" % (file_name, file_size) 

     file_size_dl = 0 
     block_sz = 8192 
     while True: 
       buffer = u.read(block_sz) 
       if not buffer: 
         break 

       file_size_dl += len(buffer) 
       f.write(buffer) 
       status = r"%10d [%3.2f%%]" % (file_size_dl, file_size_dl * 100./file_size) 
       status = status + chr(8)*(len(status)+1) 
       print status, 

     f.close() 

# download the cache.manifest file 
# since this request doesn't include the Conent-Length header we will use a different api =P 
urllib.urlretrieve (cmServerURL+ 'cache.manifest', './cache.manifest') 

# open the cache.manifest and go through line-by-line checking for the existance of files 
f = open('cache.manifest', 'r') 
for line in f: 
     filepath = line.split('/') 
     if len(filepath) > 1: 
       fileName = line.strip() 
       # if the file doesn't exist, lets download it 
       if not os.path.exists(fileName): 
           print 'NOT FOUND: ' + line 
           dirName = os.path.dirname(fileName) 
           print 'checking dirctory: ' + dirName 
           if not os.path.exists(dirName): 
             os.makedirs(dirName) 
           else: 
             print 'directory exists' 
           print 'downloading file: ' + cmServerURL + line, 
           loadURL (cmServerURL+fileName, fileName) 
相關問題