From 47e8538164436a19a578db8f50c7a29457d99e11 Mon Sep 17 00:00:00 2001 From: wbt5 Date: Tue, 18 Aug 2020 21:52:56 +0800 Subject: [PATCH] =?UTF-8?q?=E6=96=B0=E5=A2=9E2=E4=B8=AA=E5=B9=B3=E5=8F=B0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- danmu/danmaku/__init__.py | 102 ++++++++++++++++++++++++++++++++++---- 1 file changed, 91 insertions(+), 11 deletions(-) diff --git a/danmu/danmaku/__init__.py b/danmu/danmaku/__init__.py index 7a0b5ab..5f8cd1d 100644 --- a/danmu/danmaku/__init__.py +++ b/danmu/danmaku/__init__.py @@ -1,23 +1,26 @@ -import aiohttp import asyncio import re +import aiohttp + +from ._173 import YiQiShan +from .acfun import AcFun from .bilibili import Bilibili +from .cc import CC from .douyu import Douyu -from .huya import Huya -from .kuaishou import KuaiShou -from .huomao import HuoMao from .egame import eGame from .huajiao import HuaJiao +from .huomao import HuoMao +from .huya import Huya from .inke import Inke -from .cc import CC +from .kuaishou import KuaiShou from .kugou import KuGou -from .zhanqi import ZhanQi +from .laifeng import LaiFeng from .longzhu import LongZhu +from .look import Look from .pps import QiXiu from .qf import QF -from .laifeng import LaiFeng -from .look import Look +from .zhanqi import ZhanQi __all__ = ['DanmakuClient'] @@ -50,14 +53,16 @@ class DanmakuClient: 'pps.tv': QiXiu, 'qf.56.com': QF, 'laifeng.com': LaiFeng, - 'look.163.com': Look}.items(): + 'look.163.com': Look, + 'acfun.cn': AcFun, + '173.com': YiQiShan}.items(): if re.match(r'^(?:http[s]?://)?.*?%s/(.+?)$' % u, url): self.__site = s self.__u = u break if self.__site is None: print('Invalid link!') - exit + exit() self.__hs = aiohttp.ClientSession() async def init_ws(self): @@ -108,14 +113,89 @@ class DanmakuClient: for m in ms: await self.__dm_queue.put(m) count += 1 - await self.heartbeats() + + async def init_ws_acfun(self, s): + self.__ws = await self.__hs.ws_connect(self.__site.ws_url) + await self.__ws.send_bytes(s.encode_packet('register')) + + async def ping_acfun(self, s): + while True: + await asyncio.sleep(1) + await self.__ws.send_bytes(s.encode_packet('ping')) + + async def keepalive_acfun(self, s): + while True: + await asyncio.sleep(50) + await self.__ws.send_bytes(s.encode_packet('keepalive')) + + async def heartbeat_acfun(self, s): + while True: + await asyncio.sleep(10) + await self.__ws.send_bytes(s.encode_packet('ztlivecsheartbeat')) + + async def fetch_danmaku_acfun(self, s): + count = 0 + async for msg in self.__ws: + self.__link_status = True + ms = s.decode_packet(msg.data) + if count == 0: + await self.__ws.send_bytes(s.encode_packet('ztlivecsenterroom')) + count += 1 + for m in ms: + await self.__dm_queue.put(m) + + async def init_ws_173(self, s): + self.__ws = await self.__hs.ws_connect(self.__site.ws_url) + await self.__ws.send_bytes(s.pack('startup')) + await asyncio.sleep(1) + await self.__ws.send_bytes(s.pack('enterroomreq')) + + async def tcphelloreq_173(self, s): + while True: + await asyncio.sleep(10) + await self.__ws.send_bytes(s.pack('tcphelloreq')) + + async def roomhelloreq_173(self, s): + while True: + await asyncio.sleep(5) + await self.__ws.send_bytes(s.pack('roomhelloreq')) + + async def fetch_danmaku_173(self, s): + async for msg in self.__ws: + self.__link_status = True + ms = s.unpack(msg.data) + for m in ms: + await self.__dm_queue.put(m) async def start(self): if self.__u == 'huajiao.com': await self.init_ws_huajiao() + elif self.__u == 'acfun.cn': + rid = re.search(r'\d+', self.__url).group(0) + s = self.__site(rid) + await self.init_ws_acfun(s) + await asyncio.gather( + self.ping_acfun(s), + self.fetch_danmaku_acfun(s), + self.keepalive_acfun(s), + self.heartbeat_acfun(s), + ) + elif self.__u == '173.com': + rid = self.__url.split('/')[-1] + s = self.__site(rid) + await self.init_ws_173(s) + await asyncio.gather( + self.fetch_danmaku_173(s), + self.tcphelloreq_173(s), + self.roomhelloreq_173(s), + ) else: await self.init_ws() await asyncio.gather( self.heartbeats(), self.fetch_danmaku(), ) + + async def stop(self): + self.__stop = True + await self.__hs.close()