Skip to content

Commit

Permalink
fix
Browse files Browse the repository at this point in the history
  • Loading branch information
lc4t committed Apr 10, 2020
1 parent 505f3be commit 30f1496
Show file tree
Hide file tree
Showing 3 changed files with 37 additions and 33 deletions.
2 changes: 1 addition & 1 deletion danmu/bili_abc/bili_danmu.py
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ def parse_body(self, body: bytes, opt: int) -> bool:
pass
# cmd
elif opt == Opt.SEND_MSG_REPLY:
if not self.handle_danmu(json.loads(body.decode('utf-8'))):
if not await self.handle_danmu(json.loads(body.decode('utf-8'))):
return False
# 握手确认
elif opt == Opt.AUTH_REPLY:
Expand Down
5 changes: 3 additions & 2 deletions danmu/bili_danmu_giftthx.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
from .bili_danmu import WsDanmuClient

import asyncio
import traceback
import json
Expand All @@ -9,11 +9,12 @@
import re
import queue
from printer import info as print
from danmu.bili_abc import bili_danmu
from reqs.utils import UtilsReq
from reqs.custom import BanUserReq


class DanmuGiftThx(WsDanmuClient):
class DanmuGiftThx(bili_danmu.WsDanmuClient):

# GIFT_MSG = '谢谢可爱的{username}投喂{giftname}x{num} (╭ ̄3 ̄)╭♡'
# DELAY_SECOND = 3
Expand Down
63 changes: 33 additions & 30 deletions danmu/bili_danmu_monitor.py
Original file line number Diff line number Diff line change
@@ -1,33 +1,39 @@
import asyncio
from typing import Optional
import re

from aiohttp import ClientSession

from printer import print_danmu
from printer import info as print
import notifier
import bili_statistics
from .bili_danmu import WsDanmuClient
from danmu.bili_abc import bili_danmu
from tasks.tv_raffle_handler import TvRaffleJoinTask
from tasks.guard_raffle_handler import GuardRafflJoinTask
from tasks.storm_raffle_handler import StormRaffleJoinTask
from tasks.utils import UtilsTask
from . import raffle_handler
from danmu import raffle_handler
from utils import clear_whitespace
import printer


class DanmuPrinter(WsDanmuClient):
def handle_danmu(self, data: dict):
cmd = data['cmd']
print(cmd)
# if cmd == 'DANMU_MSG':
# print_danmu(data)
class DanmuPrinter(bili_danmu.WsDanmuClient):
__slots__ = ()

def handle_danmu(self, data: dict) -> bool:
if 'cmd' in data:
cmd = data['cmd']
elif 'msg' in data:
data = data['msg']
cmd = data['cmd']
else:
return True # 预防未来sbb站

if cmd == 'DANMU_MSG':
print_danmu(data)
return True


class DanmuRaffleMonitor(WsDanmuClient):
class DanmuRaffleMonitor(bili_danmu.WsDanmuClient):
__slots__ = ()

# clear_whitespace之后
# 全区广播:<%生而为人要温柔%>送给<%岛岛子%>1个应援喵,点击前往TA的房间去抽奖吧
# 全区广播:主播<%咩咩想喝甜奶盖%>的直播间得到了偶像恋人的回应,快去前往抽奖吧!
Expand All @@ -45,15 +51,8 @@ class DanmuRaffleMonitor(WsDanmuClient):
NOTICE_MSG_GUARD_PATTERN = re.compile(r'.+%>(?!.*%>)' # 匹配最后一个 %>
r'[^,,了]+了(.{2})', re.DOTALL)

def __init__(
self, room_id: int, area_id: int,
session: Optional[ClientSession] = None, loop=None):
super().__init__(
room_id=room_id,
area_id=area_id,
session=session,
loop=loop
)
def __init__(self, *args, **kwargs):
super().__init__(*args, **kwargs)
self._funcs_task.append(self._check_area) # 比正常的监控多了一个分区定时查看

async def _check_area(self):
Expand All @@ -62,17 +61,21 @@ async def _check_area(self):
while await asyncio.shield(
notifier.exec_func(UtilsTask.is_ok_as_monitor, self._room_id, self._area_id)):
await asyncio.sleep(300)
print(f'{self._room_id}不再适合作为监控房间,即将切换')
print(f'{self._room_id} 不再适合作为监控房间,即将切换')
except asyncio.CancelledError:
pass
return

async def _prepare_client(self) -> bool:
# 1566786363: 把room_id删了,否则导致下播后又选择几率过高(b站api有延迟)
self._room_id = await notifier.exec_func(
UtilsTask.get_room_by_area,
self._area_id)
print(f'{self._area_id}号数据连接选择房间({self._room_id})')
return self._room_id is not None
print(f'{self._area_id} 号数据连接选择房间({self._room_id})')
if self._room_id is None:
print(f'{self._area_id} 号数据连接准备部分发生致命错误')
self._closed = True # 内部关闭,不再重连
return False
return True

def handle_danmu(self, data: dict) -> bool:
if 'cmd' in data:
Expand All @@ -84,7 +87,7 @@ def handle_danmu(self, data: dict) -> bool:
return True # 预防未来sbb站

if cmd == 'PREPARING':
print(f'{self._area_id}号数据连接房间下播({self._room_id})')
print(f'{self._area_id} 号数据连接房间下播({self._room_id})')
return False

elif cmd == 'NOTICE_MSG':
Expand All @@ -103,19 +106,19 @@ def handle_danmu(self, data: dict) -> bool:
broadcast = 'nmb'
raffle_num = 1
raffle_name = '小电视'
print(f'{self._area_id}号数据连接检测到{real_roomid:^9}{raffle_name}')
print(f'{self._area_id} 号数据连接检测到{real_roomid:^9}{raffle_name}')
raffle_handler.push2queue(TvRaffleJoinTask, real_roomid)
broadcast_type = 0 if broadcast == '全区' else 1
bili_statistics.add2pushed_raffles(raffle_name, broadcast_type, raffle_num)
elif msg_type == 3:
raffle_name = self.NOTICE_MSG_GUARD_PATTERN.match(msg_common).group(1)
print(f'{self._area_id}号数据连接检测到{real_roomid:^9}{raffle_name}')
print(f'{self._area_id} 号数据连接检测到{real_roomid:^9}{raffle_name}')
raffle_handler.push2queue(GuardRafflJoinTask, real_roomid)
broadcast_type = 0 if raffle_name == '总督' else 2
bili_statistics.add2pushed_raffles(raffle_name, broadcast_type)
elif msg_type == 6:
raffle_name = '二十倍节奏风暴'
print(f'{self._area_id}号数据连接检测到{real_roomid:^9}{raffle_name}')
print(f'{self._area_id} 号数据连接检测到{real_roomid:^9}{raffle_name}')
raffle_handler.push2queue(StormRaffleJoinTask, real_roomid)
bili_statistics.add2pushed_raffles(raffle_name)
return True

0 comments on commit 30f1496

Please sign in to comment.