Ejemplo n.º 1
0
 def __download_pic(self, pic, path):
     """
     下载单个图片
     :param pic: 图片数据
     :param path: 存储路径目录
     :return: bool 下载结果, str 下载路径
     """
     path = os.path.join(path, self.__make_photo_name(pic))
     if not os.path.exists(path):
         url = WeiboApi.make_large_url(pic)
         response = WeiboApi.get(url, timeout=60)
         with open(path, 'wb') as fp:
             fp.write(response.content)
         return True, path
     return False, path
Ejemplo n.º 2
0
    def __download_album(self, album):
        """
        下载单个相册
        :param album: 相册数据
        :return: None
        """
        # 相册所有图片的id
        all_photo_ids = WeiboApi.fetch_photo_ids(self.uid, album['album_id'],
                                                 album['type'])
        self.logger.info(Fore.BLUE + '检测到 %d 张图片' % len(all_photo_ids))

        # 相册所有大图的数据
        all_large_pics = self.__fetch_large_pics(album, all_photo_ids)
        total = len(all_large_pics)

        # 下载所有大图
        with concurrent.futures.ThreadPoolExecutor() as executor:
            album_path = self.__make_album_path(album)

            future_to_large = {
                executor.submit(self.__download_pic, large, album_path): large
                for large in all_large_pics
            }

            for i, future in enumerate(
                    concurrent.futures.as_completed(future_to_large)):
                large = future_to_large[future]
                count_msg = '%d/%d ' % (i + 1, total)
                try:
                    result, path = future.result()
                except Exception as exc:
                    err = '%s 抛出了异常: %s' % (WeiboApi.make_large_url(large),
                                            exc)
                    self.logger.error(''.join([Fore.RED, count_msg, err]))
                else:
                    style = result and Style.NORMAL or Style.DIM
                    self.logger.info(''.join(
                        [Fore.GREEN, style, count_msg, path]))
            else:
                self.logger.info(Fore.BLUE + '《%s》 已完成' % album['caption'])