From 63b72d9c5bf9968a3a164b4c3a12d2ff5ff0246f Mon Sep 17 00:00:00 2001 From: Azide Date: Mon, 23 May 2022 14:38:35 +0800 Subject: [PATCH] =?UTF-8?q?=E6=B7=BB=E5=8A=A0=E4=BA=86bilibili=E7=9B=B4?= =?UTF-8?q?=E6=92=AD=E7=9A=84=E5=8D=95=E5=85=83=E6=B5=8B=E8=AF=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../nonebot_bison/platform/bilibili.py | 16 +-- .../platforms/static/bili_live_status-0.json | 114 ++++++++++++++++++ .../platforms/static/bili_live_status-1.json | 114 ++++++++++++++++++ tests/platforms/test_bilibili_live.py | 41 +++++++ 4 files changed, 277 insertions(+), 8 deletions(-) create mode 100644 tests/platforms/static/bili_live_status-0.json create mode 100644 tests/platforms/static/bili_live_status-1.json create mode 100644 tests/platforms/test_bilibili_live.py diff --git a/src/plugins/nonebot_bison/platform/bilibili.py b/src/plugins/nonebot_bison/platform/bilibili.py index 0c543ff..ae196e4 100644 --- a/src/plugins/nonebot_bison/platform/bilibili.py +++ b/src/plugins/nonebot_bison/platform/bilibili.py @@ -1,4 +1,5 @@ import json +from turtle import title from typing import Any, Optional import httpx @@ -152,13 +153,13 @@ class Bilibililive(StatusChange): # Description : bilibili开播提醒 # E-mail : 1557157806@qq.com categories = {} - platform_name = "bilibililive" + platform_name = "bilibili-live" enable_tag = True enabled = True is_common = True schedule_type = "interval" schedule_kw = {"seconds": 10} - name = "B站直播" + name = "Bilibili直播" has_target = True async def get_target_name(self, target: Target) -> Optional[str]: @@ -185,7 +186,7 @@ class Bilibililive(StatusChange): info["uid"] = res_dict["data"]["mid"] info["uname"] = res_dict["data"]["name"] info["live_state"] = res_dict["data"]["live_room"]["liveStatus"] - info["url"] = res_dict["data"]["live_room"]["url"] + info["room_id"] = res_dict["data"]["live_room"]["roomid"] info["title"] = res_dict["data"]["live_room"]["title"] info["cover"] = res_dict["data"]["live_room"]["cover"] return info @@ -202,10 +203,9 @@ class Bilibililive(StatusChange): return [] async def parse(self, raw_post: RawPost) -> Post: - url = raw_post["url"] + url = "https://live.bilibili.com/{}".format(raw_post["room_id"]) pic = [raw_post["cover"]] target_name = raw_post["uname"] - text = target_name + "老师的直播 开播啦!小伙伴们请务必速速来围观!" - return Post( - "bilibililive", text=text, url=url, pics=pic, target_name=target_name - ) + title = raw_post["title"] + text = "{} 直播中:\n{}\n\n小伙伴们速速前来围观!".format(target_name, title) + return Post(self.name, text=text, url=url, pics=pic, target_name=target_name) diff --git a/tests/platforms/static/bili_live_status-0.json b/tests/platforms/static/bili_live_status-0.json new file mode 100644 index 0000000..e907dc9 --- /dev/null +++ b/tests/platforms/static/bili_live_status-0.json @@ -0,0 +1,114 @@ +{ + "code": 0, + "message": "0", + "ttl": 1, + "data": { + "mid": 13164144, + "name": "魔法Zc目录", + "sex": "男", + "face": "http://i0.hdslb.com/bfs/face/a84fa10f90f7060d0336384954ee1cde7a8e9bc6.jpg", + "face_nft": 0, + "sign": "每日18:00~22:00欢乐直播!请勿在任何乌有相关内容中刷Zc,尊重角色;商务合作qq271374252", + "rank": 10000, + "level": 6, + "jointime": 0, + "moral": 0, + "silence": 0, + "coins": 0, + "fans_badge": true, + "fans_medal": { + "show": false, + "wear": false, + "medal": null + }, + "official": { + "role": 1, + "title": "bilibili 2021百大UP主、知名游戏UP主、直播高能主播", + "desc": "", + "type": 0 + }, + "vip": { + "type": 2, + "status": 1, + "due_date": 1702051200000, + "vip_pay_type": 0, + "theme_type": 0, + "label": { + "path": "", + "text": "年度大会员", + "label_theme": "annual_vip", + "text_color": "#FFFFFF", + "bg_style": 1, + "bg_color": "#FB7299", + "border_color": "" + }, + "avatar_subscript": 1, + "nickname_color": "#FB7299", + "role": 3, + "avatar_subscript_url": "http://i0.hdslb.com/bfs/vip/icon_Certification_big_member_22_3x.png" + }, + "pendant": { + "pid": 3399, + "name": "2233幻星集", + "image": "http://i0.hdslb.com/bfs/garb/item/20c07ded13498a5b12db99660c766ddd92ecfe31.png", + "expire": 0, + "image_enhance": "http://i0.hdslb.com/bfs/garb/item/20c07ded13498a5b12db99660c766ddd92ecfe31.png", + "image_enhance_frame": "" + }, + "nameplate": { + "nid": 1, + "name": "黄金殿堂", + "image": "http://i2.hdslb.com/bfs/face/82896ff40fcb4e7c7259cb98056975830cb55695.png", + "image_small": "http://i0.hdslb.com/bfs/face/627e342851dfda6fe7380c2fa0cbd7fae2e61533.png", + "level": "稀有勋章", + "condition": "单个自制视频总播放数\u003e=100万" + }, + "user_honour_info": { + "mid": 0, + "colour": null, + "tags": [] + }, + "is_followed": true, + "top_photo": "http://i2.hdslb.com/bfs/space/853fea2728651588a2cdef0a1e586bcefff8e3d8.png", + "theme": {}, + "sys_notice": {}, + "live_room": { + "roomStatus": 1, + "liveStatus": 0, + "url": "https://live.bilibili.com/3044248?broadcast_type=0\u0026is_room_feed=1", + "title": "【Zc】早朝危机合约!", + "cover": "http://i0.hdslb.com/bfs/live/new_room_cover/cf7d4d3b2f336c6dba299644c3af952c5db82612.jpg", + "roomid": 3044248, + "roundStatus": 1, + "broadcast_type": 0, + "watched_show": { + "switch": true, + "num": 13753, + "text_small": "1.3万", + "text_large": "1.3万人看过", + "icon": "https://i0.hdslb.com/bfs/live/a725a9e61242ef44d764ac911691a7ce07f36c1d.png", + "icon_location": "", + "icon_web": "https://i0.hdslb.com/bfs/live/8d9d0f33ef8bf6f308742752d13dd0df731df19c.png" + } + }, + "birthday": "07-21", + "school": { + "name": "" + }, + "profession": { + "name": "", + "department": "", + "title": "", + "is_show": 0 + }, + "tags": [ + "评论区UP主", + "目标是星辰大海" + ], + "series": { + "user_upgrade_status": 3, + "show_upgrade_window": false + }, + "is_senior_member": 1 + } +} \ No newline at end of file diff --git a/tests/platforms/static/bili_live_status-1.json b/tests/platforms/static/bili_live_status-1.json new file mode 100644 index 0000000..9d0d02f --- /dev/null +++ b/tests/platforms/static/bili_live_status-1.json @@ -0,0 +1,114 @@ +{ + "code": 0, + "message": "0", + "ttl": 1, + "data": { + "mid": 13164144, + "name": "魔法Zc目录", + "sex": "男", + "face": "http://i0.hdslb.com/bfs/face/a84fa10f90f7060d0336384954ee1cde7a8e9bc6.jpg", + "face_nft": 0, + "sign": "每日18:00~22:00欢乐直播!请勿在任何乌有相关内容中刷Zc,尊重角色;商务合作qq271374252", + "rank": 10000, + "level": 6, + "jointime": 0, + "moral": 0, + "silence": 0, + "coins": 0, + "fans_badge": true, + "fans_medal": { + "show": false, + "wear": false, + "medal": null + }, + "official": { + "role": 1, + "title": "bilibili 2021百大UP主、知名游戏UP主、直播高能主播", + "desc": "", + "type": 0 + }, + "vip": { + "type": 2, + "status": 1, + "due_date": 1702051200000, + "vip_pay_type": 0, + "theme_type": 0, + "label": { + "path": "", + "text": "年度大会员", + "label_theme": "annual_vip", + "text_color": "#FFFFFF", + "bg_style": 1, + "bg_color": "#FB7299", + "border_color": "" + }, + "avatar_subscript": 1, + "nickname_color": "#FB7299", + "role": 3, + "avatar_subscript_url": "http://i0.hdslb.com/bfs/vip/icon_Certification_big_member_22_3x.png" + }, + "pendant": { + "pid": 3399, + "name": "2233幻星集", + "image": "http://i0.hdslb.com/bfs/garb/item/20c07ded13498a5b12db99660c766ddd92ecfe31.png", + "expire": 0, + "image_enhance": "http://i0.hdslb.com/bfs/garb/item/20c07ded13498a5b12db99660c766ddd92ecfe31.png", + "image_enhance_frame": "" + }, + "nameplate": { + "nid": 1, + "name": "黄金殿堂", + "image": "http://i2.hdslb.com/bfs/face/82896ff40fcb4e7c7259cb98056975830cb55695.png", + "image_small": "http://i0.hdslb.com/bfs/face/627e342851dfda6fe7380c2fa0cbd7fae2e61533.png", + "level": "稀有勋章", + "condition": "单个自制视频总播放数\u003e=100万" + }, + "user_honour_info": { + "mid": 0, + "colour": null, + "tags": [] + }, + "is_followed": true, + "top_photo": "http://i2.hdslb.com/bfs/space/853fea2728651588a2cdef0a1e586bcefff8e3d8.png", + "theme": {}, + "sys_notice": {}, + "live_room": { + "roomStatus": 1, + "liveStatus": 1, + "url": "https://live.bilibili.com/3044248?broadcast_type=0\u0026is_room_feed=1", + "title": "【Zc】早朝危机合约!", + "cover": "http://i0.hdslb.com/bfs/live/new_room_cover/cf7d4d3b2f336c6dba299644c3af952c5db82612.jpg", + "roomid": 3044248, + "roundStatus": 1, + "broadcast_type": 0, + "watched_show": { + "switch": true, + "num": 13753, + "text_small": "1.3万", + "text_large": "1.3万人看过", + "icon": "https://i0.hdslb.com/bfs/live/a725a9e61242ef44d764ac911691a7ce07f36c1d.png", + "icon_location": "", + "icon_web": "https://i0.hdslb.com/bfs/live/8d9d0f33ef8bf6f308742752d13dd0df731df19c.png" + } + }, + "birthday": "07-21", + "school": { + "name": "" + }, + "profession": { + "name": "", + "department": "", + "title": "", + "is_show": 0 + }, + "tags": [ + "评论区UP主", + "目标是星辰大海" + ], + "series": { + "user_upgrade_status": 3, + "show_upgrade_window": false + }, + "is_senior_member": 1 + } +} \ No newline at end of file diff --git a/tests/platforms/test_bilibili_live.py b/tests/platforms/test_bilibili_live.py new file mode 100644 index 0000000..ccdb78f --- /dev/null +++ b/tests/platforms/test_bilibili_live.py @@ -0,0 +1,41 @@ +from datetime import datetime + +import feedparser +import pytest +import respx +from httpx import Response +from nonebug.app import App +from pytz import timezone + +from .utils import get_file, get_json + + +@pytest.fixture +def bili_live(app: App): + from nonebot_bison.platform import platform_manager + + return platform_manager["bilibili-live"] + + +@pytest.mark.asyncio +@respx.mock +async def test_fetch_bilibili_live_status(bili_live, dummy_user_subinfo): + bili_live_router = respx.get( + "https://api.bilibili.com/x/space/acc/info?mid=13164144" + ) + bili_live_router.mock( + return_value=Response(200, json=get_json("bili_live_status-0.json")) + ) + target = "13164144" + res = await bili_live.fetch_new_post(target, [dummy_user_subinfo]) + assert bili_live_router.called + assert len(res) == 0 + bili_live_router.mock( + return_value=Response(200, json=get_json("bili_live_status-1.json")) + ) + res2 = await bili_live.fetch_new_post(target, [dummy_user_subinfo]) + post = res2[0][1][0] + assert post.target_type == "Bilibili直播" + assert post.text == "魔法Zc目录 直播中:\n【Zc】早朝危机合约!\n\n小伙伴们速速前来围观!" + assert post.url == "https://live.bilibili.com/3044248" + assert post.target_name == "魔法Zc目录"