Exemple #1
0
def insert_jd_comments(comment_list: list, jd_ss: Union[Shop, JDSku]):
    for comment in comment_list:
        color, rom = parse_iPhone11_product_info(comment['productColor'], comment['productSize'])
        new_comment, created = Comment.get_or_create(
            source='京东',
            is_self=jd_ss.is_self,
            comment_id='JD' + str(comment['id']),
            create_time=comment['creationTime'],
            content=comment['content'],
            star=comment['score'],
            order_time=comment['referenceTime'],
            order_days=comment['days'],
            color=color,
            rom=rom
        )
        if created is True:
            if 'afterUserComment' in comment:
                after_comment = comment['afterUserComment']
                new_comment.after_time = after_comment['created']
                new_comment.after_content = after_comment['content']
                new_comment.after_days = comment['afterDays']
            if comment['userClient'] == 4:
                new_comment.user_device = 'Android'
            elif comment['userClient'] == 2:
                new_comment.user_device = 'iOS'
            else:
                new_comment.user_device = 'other'
            new_comment.save()
def insert_sn_comments(comment_list: list, sn_ss: Union[Shop, SNSku]):
    for comment in comment_list:
        try:
            commodity_info = comment['commodityInfo']
            if commodity_info['charaterId1'] == '颜色':
                color, rom = parse_iPhone11_product_info(commodity_info['charaterDesc1'],
                                                         commodity_info['charaterDesc2'])
            elif commodity_info['charaterId2'] == '颜色':
                color, rom = parse_iPhone11_product_info(commodity_info['charaterDesc2'],
                                                         commodity_info['charaterDesc1'])
            else:
                color, rom = parse_iPhone11_product_info(commodity_info['charaterDesc1'],
                                                         commodity_info['charaterDesc1'])
        except (AttributeError, KeyError):
            print('---有一条评论对应的产品信息不规范, 跳过此条评论---')
            continue
        new_comment, created = Comment.get_or_create(
            source='苏宁',
            is_self=sn_ss.is_self,
            comment_id='SN' + str(comment['commodityReviewId']),
            create_time=comment['publishTime'],
            content=comment['content'],
            star=comment['qualityStar'],
            color=color,
            rom=rom
        )
        if created is True:
            if comment['againFlag'] is True:
                after_comment = comment['againReview']
                new_comment.after_time = after_comment['publishTime']
                new_comment.after_content = after_comment['againContent']
                after_days_str = after_comment['publishTimeStr']
                if after_days_str == '当天追加':
                    after_days_num = 0
                else:
                    after_days_num = int(re.match(r'^\d+', after_days_str).group())
                new_comment.after_days = after_days_num
            if comment['sourceSystem'] == 'android':
                new_comment.user_device = 'Android'
            elif comment['sourceSystem'] == 'ios':
                new_comment.user_device = 'iOS'
            else:
                new_comment.user_device = 'other'
            new_comment.save()