示例#1
0
# 实例化 BlockingScheduler
sched = BlockingScheduler()

global weibo_id_array
global firstcheck_weibo

weibo_id_array = []
firstcheck_weibo = True

# 查询时间间隔初始化
interval_md = md_interval()
interval_wb = wb_interval()
interval_kd = kd_interval()

# 获取酷q版本
version_dict = bot.get_version_info()
version = version_dict['coolq_edition']


def getModian():
    # bot = CQHttp(api_root='http://127.0.0.1:5700/')
    try:
        # INFO(printStrTime() + 'check modian')
        INFO('check modian')
        stampTime = int(time.time())
        msgDict_array = newOrder(stampTime, int(interval_md))
        for msgDict in msgDict_array[0:-1]:
            if msgDict:
                for msg in msgDict['msg']:
                    msg += msgDict['end']
                    # print(printStrTime() + msg)
示例#2
0
class ListenKdMdWb(QThread):
    def __init__(self, kd, md, wb):
        super(QThread, self).__init__()
        self.switch_kd = kd
        self.switch_md = md
        self.switch_wb = wb
        self.sched = BlockingScheduler()
        self.weibo_id_array = []
        self.firstcheck_weibo = True
        self.pref3 = Preferences()
        self.api_root_url = 'http://127.0.0.1:%s/' % self.pref3.getapi_root_port(
        )
        self.bot = CQHttp(api_root=self.api_root_url)
        self.version_dict = self.bot.get_version_info()
        self.version = self.version_dict['coolq_edition']

    def run(self):
        if self.switch_md:
            self.interval_md = int(self.pref3.getmdinterval())
            self.sched.add_job(self.getmodian,
                               'interval',
                               seconds=self.interval_md,
                               misfire_grace_time=10,
                               coalesce=True,
                               max_instances=2)
        if self.switch_wb:
            self.interval_wb = int(self.pref3.getwbinterval())
            self.sched.add_job(self.getweibo,
                               'interval',
                               seconds=self.interval_wb,
                               misfire_grace_time=10,
                               coalesce=True,
                               max_instances=2)
        if self.switch_kd:
            self.interval_kd = int(self.pref3.getkdinterval())
            self.sched.add_job(self.getkoudai,
                               'interval',
                               seconds=self.interval_kd,
                               misfire_grace_time=10,
                               coalesce=True,
                               max_instances=2)
        self.sched.start()

    def getmodian(self):
        try:
            INFO('check modian')
            stampTime = int(time.time())
            msgDict_array = newOrder(stampTime, int(self.interval_md))
            for msgDict in msgDict_array[0:-1]:
                if msgDict:
                    for msg in msgDict['msg']:
                        msg += msgDict['end']
                        print(msg)
                        for grpid in self.pref3.getqqidarray():
                            self.bot.send_group_msg_async(group_id=grpid,
                                                          message=msg,
                                                          auto_escape=False)
                            time.sleep(0.1)
        except Exception as e:
            WARN('error when getModian', e, "modian dict:", msgDict_array[-1])
        finally:
            INFO('modian check completed')

    def getweibo(self):
        try:
            weibo = Weibo()
            INFO('check weibo')
            # 初次启动记录前十条微博id
            if self.firstcheck_weibo is True:
                INFO('first check weibo')
                self.weibo_id_array = weibo.IdArray
                self.firstcheck_weibo = False
            if self.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 self.weibo_id_array:
                        msg = []
                        # 将id计入id列表
                        self.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 self.pref3.getqqidarray():
                            self.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')

    def getkoudai(self):
        try:
            INFO('check koudai room')
            koudai = Koudai()
            # 检查是否有新消息
            if koudai.checkNew():
                INFO('have new room msg')
                # 判断酷Q版本
                if self.version == 'air':
                    msgArray = koudai.msgAir()
                elif self.version == 'pro':
                    msgArray = koudai.msgPro()
                # 消息序列反向排序
                msgArray.reverse()
                for msg in msgArray:
                    print(msg)
                    for grpid in self.pref3.getqqidarray():
                        self.bot.send_group_msg_async(group_id=grpid,
                                                      message=msg,
                                                      auto_escape=False)
                        time.sleep(0.5)
        except Exception as e:
            WARN('error when getRoomMsg', e)
            raise e
        finally:
            INFO('koudai check completed')