コード例 #1
0
ファイル: super_dxj.py プロジェクト: pjy612/stormgift
    async def run(self):
        expected = await SuperDxjUserAccounts.get_all_live_rooms()
        expected = [room_id for room_id in expected if room_id != 123]
        if not expected:
            logging.error(f"Cannot load monitor live rooms from redis!")
            return

        self.monitor_live_rooms = expected
        dps = []

        for room_id in expected:
            flag, data = await BiliApi.get_live_room_info_by_room_id(room_id=room_id)
            if flag:
                uid = data["uid"]
                user_name = await BiliApi.get_user_name(uid=uid)
                short_room_id = data["short_id"]
                room_id = data["room_id"]
            else:
                user_name = "??"
                short_room_id = room_id

            q = asyncio.Queue()
            await self.new_room(room_id, q)

            dp = DanmakuProcessor(q=q, room_id=room_id, short_room_id=short_room_id, name=user_name)
            dps.append(asyncio.create_task(dp.run()))

        logging.info(f"Ws monitor settings read finished, Need add: {expected}.")
        for dp_task in dps:
            await dp_task
コード例 #2
0
ファイル: super_dxj.py プロジェクト: pjy612/stormgift
    async def parse_danmaku(self):
        while True:
            dmk = await self.q.get()
            try:
                await self.proc_one_danmaku(dmk)
            except Exception as e:
                if dmk == {"code": 0}:
                    continue

                logging.error(f"Error happened in processing one dmk: {dmk}, e: {e}")
コード例 #3
0
ファイル: super_dxj.py プロジェクト: pjy612/stormgift
    async def run(self):
        flag, live_status = await BiliApi.get_live_status(room_id=self.room_id)
        if not flag:
            logging.error(f"Cannot get live status when init... e: {live_status}")
        else:
            logging.info(f"Live room status: {self.room_id} -> {live_status}")

            self._is_live = bool(live_status)
            self._last_live_time = time.time()

        await asyncio.gather(*[
            self.thank_follower(),
            self.parse_danmaku(),
            self.send_danmaku(),
            self.send_carousel_msg(),
            self.thank_gift(),
        ])
コード例 #4
0
ファイル: super_dxj.py プロジェクト: pjy612/stormgift
    async def get_live_status(self):
        if self._is_live is False:
            return False

        now = time.time()
        if now - self._last_live_time < 1800:
            return True

        flag, live_status = await BiliApi.get_live_status(room_id=self.room_id)
        if not flag:
            logging.error(f"Cannot get live room status, room_id: {self.room_id}, {live_status}")
            live_status = False

        if live_status:
            self._is_live = True
            self._last_live_time = now
            return True
        else:
            self._is_live = False
            return False
コード例 #5
0
ファイル: super_dxj.py プロジェクト: pjy612/stormgift
    async def load_cookie(self):
        if self.cookie and self.cookie_expire_time > time.time():
            return True, self.cookie

        config = await self.load_config()
        account = config["account"]
        password = config["password"]
        cookie = await SuperDxjCookieMgr.load_cookie(account=account)
        if cookie:
            self.cookie = cookie
            self.cookie_expire_time = time.time() + 60
            return True, cookie

        flag, cookie = await CookieFetcher.get_cookie(account=account, password=password)
        if not flag:
            logging.error(f"Super dxj CookieFetcher.get_cookie Error: {cookie}")
            return False, f"登录失败:{cookie}"

        await SuperDxjCookieMgr.save_cookie(account=account, cookie=cookie)
        self.cookie = cookie
        self.cookie_expire_time = time.time() + 60
        logging.info(f"Super dxj CookieFetcher.get_cookie 登录成功!{self.room_id}.")
        return True, cookie
コード例 #6
0
ファイル: super_dxj.py プロジェクト: pjy612/stormgift
    async def send_danmaku(self):
        while True:
            dmk = await self.dmk_q.get()

            now = time.time()
            if now < self.msg_block_until:
                logging.warning(f"DMK BLOCK: {self.short_room_id}-{self.name} -> {dmk}")
                continue

            # 计数
            if now - self.msg_speed_counter_start_time > 60:
                self.msg_speed_counter_start_time = now
                self.msg_speed_counter = 0
            self.msg_speed_counter += 1

            # 检查计数
            if self.msg_speed_counter > 60:
                self.msg_block_until = now + 30

            flag, cookie = await self.load_cookie()
            if not flag:
                # 登录失败,冷却1分钟
                self.msg_block_until = now + 30
                logging.warning(f"DMK BLOCK(Login failed): {self.short_room_id}-{self.name} -> {dmk}")
                continue

            flag, msg = await BiliApi.send_danmaku(message=dmk, room_id=self.room_id, cookie=cookie)
            if flag:
                await asyncio.sleep(1)
                continue

            logging.error(f"DMK send failed. {self.short_room_id}-{self.name} -> {dmk}\n\t{msg}")
            if "412" in msg:
                self.msg_block_until = now + 60 * 5

            elif "账号未登录" in msg:
                await self.set_cookie_invalid()
コード例 #7
0
ファイル: super_dxj.py プロジェクト: pjy612/stormgift
 async def on_error(e, msg):
     self._broken_live_rooms.append(room_id)
     logging.error(f"WS ERROR! room_id: [{room_id}], msg: {msg}, e: {e}")