示例#1
0
    def _get_album_comment_data(self, album_info):
        '''获取指定相册的评论数据
        '''

        if not self.can_access:
            return

        comment_url = "https://h5.qzone.qq.com/proxy/domain/app.photo.qzone.qq.com/cgi-bin/app/cgi_pcomment_xml_v2"
        payload = {
            "inCharset": "utf-8",  # inCharset 必选
            "outCharset": "utf-8",  # outCharset 必选
            "g_tk": self._account_info.g_tk,
            "hostUin": self._account_info.target_uin,
            "uin": self._account_info.self_uin,
            "topicId": album_info.id,  # topicId 必选,相册id
            "order": "1",
            "start": "0",
            "num": "0"  # num最大可设定为100
        }
        num = 100
        start = 0
        while True:
            payload["start"] = "%s" % start
            payload["num"] = "%s" % num

            r = self._account_info.get_url(comment_url, params=payload)
            json_data = get_json_data_from_response(r.text)

            if "comments" not in json_data["data"] or len(
                    json_data["data"]["comments"]) == 0:
                break

            current_num = len(json_data["data"]["comments"])

            photo_comment = PhotoComment(json_data, start, start + current_num,
                                         self._directory, album_info.directory,
                                         self._account_info)

            photo_comment.export()

            start += current_num
        if start == 0:
            print("get 0 comment, try to find comments by traversing album")

        print(str(album_info), "comment[%d] done" % start)

        return start
示例#2
0
    def _get_album_photo_data(self, album_info, need_get_comment=False):
        '''获取相册中照片数据
        '''

        if not self.can_access:
            return

        print("process album, name: %s\tid: %s" %
              (album_info.name, album_info.id))

        list_photo_url = "https://h5.qzone.qq.com/proxy/domain/photo.qzone.qq.com/fcgi-bin/cgi_list_photo"
        floatview_photo_list = "https://h5.qzone.qq.com/proxy/domain/photo.qzone.qq.com/fcgi-bin/cgi_floatview_photo_list_v2"
        start = 0
        num = 500
        current_num = num

        list_photo_payload = {
            "inCharset": "utf-8",
            "outCharset": "utf-8",
            "g_tk": self._account_info.g_tk,
            "hostUin": self._account_info.target_uin,
            "uin": self._account_info.self_uin,
            "topicId": album_info.id,
            "pageStart": "%d" % start,
            "pageNum": "%d" % current_num,
        }
        floatview_photo_payload = {
            "g_tk": self._account_info.g_tk,
            "topicId": album_info.id,
            "hostUin": self._account_info.target_uin,
            "uin": self._account_info.self_uin,
            "fupdate": "1",
            "plat": "qzone",
            "source": "qzone",
            "cmtNum": "99",  # 必选
            "sortOrder": "1",
            "need_private_comment": "1",
            "inCharset": "utf-8",
            "outCharset": "utf-8",
            "appid": "4",
            "isFirst": "1",
            "picKey": "unknown",
            "postNum": "0"  # 获取后续照片数量
        }

        ttt = '''{"data": {"comments":[]}}'''
        single_comment_data = json.loads(ttt)
        comment_exported_num = 0
        total_comment_num = 0

        loop_num = math.ceil(album_info.photo_num / num)
        for i in range(loop_num):
            start = i * num
            current_num = num if i < loop_num - 1 else album_info.photo_num - i * num
            list_photo_payload["pageStart"] = "%d" % start
            list_photo_payload["pageNum"] = "%d" % current_num
            r = self._account_info.get_url(list_photo_url,
                                           params=list_photo_payload)
            json_data = get_json_data_from_response(r.text)
            photo_parser = PhotoParser(json_data, start, start + current_num,
                                       self._directory, album_info.directory)
            photo_parser.export()

            # 获取原图及视频url
            if "photoList" in json_data["data"] and json_data["data"]["photoList"] \
                    and len(json_data["data"]["photoList"]) > 0:
                floatview_photo_payload["picKey"] = json_data["data"][
                    "photoList"][0]["lloc"]
                floatview_photo_payload["postNum"] = "%d" % (current_num - 1)
                r = self._account_info.get_url(floatview_photo_list,
                                               params=floatview_photo_payload)
                floatview_json_data = get_json_data_from_response(r.text)
                photo_parser = PhotoParser(floatview_json_data, start,
                                           start + current_num,
                                           self._directory,
                                           album_info.directory, True)
                photo_parser.export()

                # 获取评论数据
                if need_get_comment:
                    for photo in json_data["data"]["photoList"]:
                        pic_comment_num = photo["forum"] or 0
                        if pic_comment_num == 0:
                            continue
                        print("find %d comment(s) in %s" %
                              (pic_comment_num, photo["lloc"]))
                        # 评论数可能显示错误
                        floatview_photo_payload["cmtNum"] = "%d" % (
                            pic_comment_num if pic_comment_num > 99 else 99)
                        floatview_photo_payload["picKey"] = photo["lloc"]
                        floatview_photo_payload["postNum"] = "0"

                        r = self._account_info.get_url(
                            floatview_photo_list,
                            params=floatview_photo_payload)
                        floatview_json_data = get_json_data_from_response(
                            r.text)
                        if not ("single" in floatview_json_data["data"]
                                and floatview_json_data["data"]["single"]):
                            continue
                        comment_data = floatview_json_data["data"]["single"][
                            "comments"]
                        single_comment_data["data"]["comments"] += comment_data
                        pic_comment_num = len(comment_data)
                        total_comment_num += pic_comment_num
                        if total_comment_num > 100 + comment_exported_num:
                            photo_comment = PhotoComment(
                                single_comment_data, comment_exported_num,
                                total_comment_num, self._directory,
                                album_info.directory, self._account_info)
                            photo_comment.export()
                            single_comment_data["data"]["comments"] = []
                            comment_exported_num = total_comment_num
                        random_sleep(0, 1)

            random_sleep(1, 2)

        # 导出剩余评论数据
        if need_get_comment:
            if comment_exported_num < total_comment_num:
                photo_comment = PhotoComment(single_comment_data,
                                             comment_exported_num,
                                             total_comment_num,
                                             self._directory,
                                             album_info.directory,
                                             self._account_info)
                photo_comment.export()
            print("get %d comment(s) in %s" %
                  (total_comment_num, album_info.name))

        print(str(album_info), "photo data done")