#!/usr/bin/env python # -*- coding: utf-8 -*- # # fetchvc.py fetch resources from verycd # # author: observer # email: jingchaohu@gmail.com # blog: http://obmem.com # last edit @ 2009.12.23 import os def ensure_dir(f): d = os.path.dirname(f) if not os.path.exists(d): os.makedirs(d) def clear_idcache(id): #clear idcache: try: l1 = str(long(id)%10) l2 = str(long(id)/10%100) l3 = str(long(id)/1000) cachefile = path + '/idcache/%s/%s/%s.html'%(l1,l2,l3) if os.path.exists(cachefile): os.remove(cachefile) except: pass def cache_image(links): if not links: return for l in links: #http://image-7.verycd.com/asdlkfjasdlfjsadkf()/thumb.jpg try: print '___caching',l f = re.compile(r'http://[^/]*').sub(r'',l) f = f.replace('/','') ensure_dir(path+'/imgcache/1') ensure_dir(path+'/imgcache/%s/1'%f[0]) ensure_dir(path+'/imgcache/%s/%s/1'%(f[0],f[1:3])) f = path + '/imgcache/%s/%s/'%(f[0],f[1:3])+f if (not os.path.exists(f)) or (os.path.getsize(f) == 0): open(f,'w').write(urllib.urlopen(l).read()) except: pass