From 101a8ed41629106e9448249b017dc1ac5dcefa00 Mon Sep 17 00:00:00 2001 From: Cam Date: Sun, 9 Jan 2022 11:19:51 +0800 Subject: [PATCH] publish package --- .gitignore | 3 +++ LICENSE | 21 +++++++++++++++++++++ README.md | 10 ++++++---- app.py | 17 ++++++++++++++--- blive/framework.py | 6 +++++- blive/msg.py | 32 ++++++++++++++++++++++++++++++++ setup.py | 19 +++++++++++++++++++ 7 files changed, 100 insertions(+), 8 deletions(-) create mode 100644 LICENSE create mode 100644 setup.py diff --git a/.gitignore b/.gitignore index d832596..63385e2 100644 --- a/.gitignore +++ b/.gitignore @@ -1,3 +1,6 @@ .venv .vscode **/__pycache__ +dist +build +blive.egg-info \ No newline at end of file diff --git a/LICENSE b/LICENSE new file mode 100644 index 0000000..4379f7a --- /dev/null +++ b/LICENSE @@ -0,0 +1,21 @@ +MIT License + +Copyright (c) [2020] [yulinfeng] + +Permission is hereby granted, free of charge, to any person obtaining a copy +of this software and associated documentation files (the "Software"), to deal +in the Software without restriction, including without limitation the rights +to use, copy, modify, merge, publish, distribute, sublicense, and/or sell +copies of the Software, and to permit persons to whom the Software is +furnished to do so, subject to the following conditions: + +The above copyright notice and this permission notice shall be included in all +copies or substantial portions of the Software. + +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE +SOFTWARE. diff --git a/README.md b/README.md index 58c3dcd..b2086ac 100644 --- a/README.md +++ b/README.md @@ -11,9 +11,11 @@ ## 快速开始 -1. 创建 app +1. 安装 - > 目前请克隆该代码仓库,并执行 pip install -r rquirements.txt + `pip install blive` + +2. 创建 app ```python from blive import BLiver @@ -21,7 +23,7 @@ app = BLiver(123) #123为房间号 ``` -2. 创建处理器 +3. 创建处理器 ```python from blive import BLiver, Events, BLiverCtx @@ -37,7 +39,7 @@ print(danmu.timestamp) ``` -3. 运行 +4. 运行 ```python diff --git a/app.py b/app.py index dba172c..e55b0c9 100644 --- a/app.py +++ b/app.py @@ -1,7 +1,13 @@ from blive import BLiver, Events, BLiverCtx -from blive.msg import DanMuMsg, HotRankChangeV2Msg, InteractWordMsg, SendGiftMsg +from blive.msg import ( + DanMuMsg, + HotRankChangeV2Msg, + InteractWordMsg, + SendGiftMsg, + SuperChatMsg, +) -app = BLiver(510) +app = BLiver(605) @app.on(Events.DANMU_MSG) @@ -24,7 +30,12 @@ async def listen_join(ctx: BLiverCtx): @app.on(Events.SUPER_CHAT_MESSAGE) async def listen_sc(ctx: BLiverCtx): - print(ctx.body) + msg = SuperChatMsg(ctx.body) + print(msg.sender) + print(msg.content) + print(msg.start_time) + print(msg.time) + print(msg.price) @app.on(Events.SEND_GIFT) diff --git a/blive/framework.py b/blive/framework.py index da9ca6c..09f7b3d 100644 --- a/blive/framework.py +++ b/blive/framework.py @@ -2,12 +2,13 @@ import sys import json import asyncio from typing import Awaitable, Dict, List, Tuple, Union +import loguru import aiohttp from aiohttp.client_ws import ClientWebSocketResponse from aiohttp.http_websocket import WSMessage from apscheduler.schedulers.asyncio import AsyncIOScheduler -import loguru from apscheduler.util import _Undefined + from .core import ( packman, Events, @@ -43,6 +44,9 @@ class Channel: def register_handler(self, handler): self.listeners.append(handler) + def __getitem__(self, idx): + return self.listeners.__getitem__(idx) + def __iter__(self): return iter(self.listeners) diff --git a/blive/msg.py b/blive/msg.py index 76393b2..3d3e6ec 100644 --- a/blive/msg.py +++ b/blive/msg.py @@ -136,3 +136,35 @@ class SendGiftMsg(BaseMsg): "combo_stay_time": self.body["data"]["combo_stay_time"], "combo_total_coin": self.body["data"]["combo_total_coin"], } + + +class SuperChatMsg(BaseMsg): + def __init__(self, body) -> None: + super().__init__(body) + + @property + def content(self): + return self.body["data"]["message"] + + @property + def sender(self): + return { + "id": self.body["data"]["user_info"]["uname"], + "name": self.body["data"]["uid"], + "medal": { + "medal_name": self.body["data"]["medal_info"]["medal_name"], + "medal_level": self.body["data"]["medal_info"]["medal_level"], + }, + } + + @property + def price(self): + return self.body["data"]["price"] + + @property + def start_time(self): + return self.body["data"]["start_time"] + + @property + def time(self): + return self.body["data"]["time"] diff --git a/setup.py b/setup.py new file mode 100644 index 0000000..57fe639 --- /dev/null +++ b/setup.py @@ -0,0 +1,19 @@ +import setuptools + +with open("./README.md", "r") as f: + description = f.read() + +setuptools.setup( + name="blive", + version="0.0.1", + author="cam", + author_email="yulinfeng000@gmail.com", + long_description=description, + long_description_content_type="text/markdown", + url="https://github.com/yulinfeng000/blive", + packages=setuptools.find_packages(), + classifiers=[ + "Programming Language :: Python :: 3", + "License :: OSI Approved :: MIT License", + ], +)