Пример #1
0
def getWeibo():
    weibo = Weibo()
    try:
        INFO('check weibo')
        global weibo_id_array
        global firstcheck_weibo
        # 初次启动记录前十条微博id
        if firstcheck_weibo is True:
            INFO('first check weibo')
            weibo_id_array = weibo.IdArray
            firstcheck_weibo = False
        if firstcheck_weibo is False:
            # 取最新的前三条微博
            for idcount in range(0, 3):
                # 广告位微博id为0,忽略
                if int(weibo.IdArray[idcount]) == 0:
                    continue
                # 微博id不在记录的id列表里,判断为新微博
                if weibo.IdArray[idcount] not in weibo_id_array:
                    msg = []
                    # 将id计入id列表
                    weibo_id_array.append(weibo.IdArray[idcount])
                    # 检查新微博是否是转发
                    if weibo.checkRetweet(idcount):
                        msg.append(
                            {
                                'type': 'text',
                                'data': {'text': '小偶像刚刚转发了一条微博:\n'}})
                        msg.append(
                            {
                                'type': 'text',
                                'data': {'text': '%s\n' % weibo.getRetweetWeibo(idcount)}})
                    # 原创微博
                    else:
                        msg.append(
                            {
                                'type': 'text',
                                'data': {'text': '小偶像刚刚发了一条新微博:\n'}})
                        msg.append(
                            {
                                'type': 'text',
                                'data': {'text': '%s\n' % weibo.getWeibo(idcount)}})
                        # 检查原创微博是否带图
                        if weibo.checkPic(idcount):
                            # 只取第一张图,pro可以直接发图,air则无
                            msg.append(
                                {
                                    'type': 'image',
                                    'data': {'file': '%s' % weibo.getPic(idcount)[0]}})
                            # 播报图的总数
                            if len(weibo.getPic(idcount)) > 1:
                                msg.append(
                                    {
                                        'type': 'text',
                                        'data': {'text': '\n(一共有%d张图喔)\n' % len(weibo.getPic(idcount))}})
                    msg.append(
                        {
                            'type': 'text',
                            'data': {'text': '传送门:%s' % weibo.getScheme(idcount)}})
                    for grpid in groupid():
                        bot.send_group_msg_async(
                            group_id=grpid, message=msg, auto_escape=False)
                        time.sleep(0.5)
                    # print(msg)
    except Exception as e:
        WARN('error when getWeibo', e)
    finally:
        INFO('weibo check completed')