def _downloader(self, item):
lockdir = None
try:
url, target = item
if os.path.exists(target):
return
base, joiner, name = target.rpartition("/")
lockdir = self.lockdir / name
try:
lockdir.mkdir()
except OSError:
lockdir = None
return
meta_file = base + joiner + "meta-" + name + ".txt"
try:
res = requests.get(
url,
headers={"Authorization": "Bearer %s" %(self.token, )},
stream=True,
timeout=60,
)
except Exception as e:
print "Error:", e
with open_atomic(meta_file) as meta:
meta.write("999\nException: %r" %(e, ))
return
with open_atomic(meta_file) as meta, open_atomic(target) as f:
meta.write("%s\n%s" %(
res.status_code,
"\n".join(
"%s: %s" %(key, res.headers[key])
for key
in res.headers
),
))
for chunk in res.iter_content(4096):
f.write(chunk)
self.counter += 1
print "Downloaded %s (%s left): %s" %(
self.counter,
self.pool.qsize(),
url,
)
except:
if item is not None:
self.pool.put(item)
raise
finally:
if lockdir is not None:
lockdir.rmdir()
评论列表
文章目录