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