| 23 | |
| 24 | # Main procedure |
| 25 | def grab(wurl, outdir, wfile, wgif, lgif, cachedir = 'cache', |
| 26 | tmpfile = 'tmp.htm'): |
| 27 | global imre, gifre |
| 28 | imre = re.compile(wgif) |
| 29 | gifre = re.compile(lgif) |
| 30 | # path to temporary file |
| 31 | tmpf = path.join(cachedir,tmpfile) |
| 32 | print "Retrieving page..." |
| 33 | download(wurl, tmpf) |
| 34 | f = file(tmpf,'r') |
| 35 | s = f.read() |
| 36 | f.close() |
| 37 | all = imre.findall(s) |
| 38 | res = [] |
| 39 | res2 = [] |
| 40 | # Fill up result list |
| 41 | for i in all: |
| 42 | if i not in res: |
| 43 | res.append(i) |
| 44 | res2.append(gifre.findall(i)[0]) |
| 45 | result = zip(res, res2) |
| 46 | |
| 47 | # Replace web links with local links |
| 48 | ns = re.sub(wgif, |
| 49 | gifsub, s) |
| 50 | f = file(path.join(outdir,wfile),'wb') |
| 51 | f.write(ns) |
| 52 | f.close() |
| 53 | |
| 54 | # Download images |
| 55 | for i in result: |
| 56 | if not path.exists(path.join(outdir,i[1])): |
| 57 | download(i[0], path.join(outdir,i[1])) |
| 58 | |
| 59 | print "Done." |
| 60 | |
| 61 | if __name__ == '__main__': |
| 62 | # Document URL |