wayslack.py 文件源码

python
阅读 24 收藏 0 点赞 0 评论 0

项目:wayslack 作者: wolever 项目源码 文件源码
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()
评论列表
文章目录


问题


面经


文章

微信
公众号

扫码关注公众号