From 527d296c9dbdbaef9cc39bd9aefab685c6ed168b Mon Sep 17 00:00:00 2001 From: wbt5 Date: Sun, 5 Jul 2020 09:58:03 +0800 Subject: [PATCH] =?UTF-8?q?=E6=96=B0=E5=A2=9E=E6=98=A0=E5=AE=A2=E7=9B=B4?= =?UTF-8?q?=E6=92=AD=E5=BC=B9=E5=B9=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- danmu/danmaku/inke.py | 41 +++++++++++++++++++++++++++++++++++++++++ 1 file changed, 41 insertions(+) create mode 100644 danmu/danmaku/inke.py diff --git a/danmu/danmaku/inke.py b/danmu/danmaku/inke.py new file mode 100644 index 0000000..12ac7bf --- /dev/null +++ b/danmu/danmaku/inke.py @@ -0,0 +1,41 @@ +import aiohttp +import re +import time +import json + + +class Inke: + + @staticmethod + async def get_ws_info(url): + uid = re.search(r'uid=(\d+)', url).group(1) + roomid = id = re.search(r'&id=(\d+)', url).group(1) + t = int(time.time() * 1e3) + cr = 'https://chatroom.inke.cn/url?roomid={}&uid={}&id={}&access_from=pc_web&_t={}'.format(roomid, uid, id, t) + async with aiohttp.ClientSession() as session: + async with session.get(cr) as resp: + res = await resp.text() + wss_url = json.loads(res).get('url') + return wss_url + + @staticmethod + def decode_msg(data): + msgs = [] + name = content = '' + msg_type = 'other' + message = json.loads(data) + ms = message['ms'] + c = ms[-1].get('c', 0) + if c: + tp = ms[-1].get('tp', 0) + if tp == 'pub' or tp == 'color': + name = ms[0].get('from').get('nic', '') + elif tp == 'user_join_tip': + name = ms[0].get('u').get('nic', '') + else: + name = 'sys' + content = c + msg_type = 'danmaku' + msg = {'name': name, 'content': content, 'msg_type': msg_type} + msgs.append(msg) + return msgs