Skip to content

Commit

Permalink
Merge commit '033a49c82a2618c184ea47b6a4b7108c6bbda7fb'
Browse files Browse the repository at this point in the history
  • Loading branch information
Shadow403 committed Jan 5, 2025
2 parents 37f6da6 + 033a49c commit 3c5beab
Show file tree
Hide file tree
Showing 59 changed files with 789 additions and 82 deletions.
4 changes: 4 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
git clone https://github.com/Shadow403/BiliListener.git
cd BiliListener
pip install poetry
poetry config virtualenvs.in-project true
poetry install
```

Expand Down Expand Up @@ -78,3 +79,6 @@ BiliListener
- `v0.1.6` 📦 打包该项目 🐍 修复数据库 `Bugs`
- `v0.1.7` 🛠️ 支持配置文件
- `v0.1.8` 🛠️ 支持更多配置项 🐍 修复数据库 `commit.handle` `Bugs`
- `v0.1.9-b1` 🌐 添加前端
- `v0.1.9-b2` 📦 添加打包图标 `(ico)`
- `v0.1.9` 🛠️ 支持更多配置项 ✨ 添加重置直播状态 `24h` ✨ 添加 `api` 版本区分
2 changes: 1 addition & 1 deletion app/config/__init__.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
__version__ = "0.1.8"
__version__ = "0.1.9"

from .logo import *
from .utils import *
Expand Down
12 changes: 6 additions & 6 deletions app/config/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,19 +2,18 @@
from fastapi.openapi.models import Contact

from . import __version__
from .utils import read_config, init_config
from .utils import read_config


class BaseConfig:
init_config()

def __init__(self, config_data):
__PATH__: str = os.getcwd()

self.host = config_data["api"]["host"]
self.port = config_data["api"]["port"]
self.appver: str = __version__
self.perfix: str = "/api"
self.tags: list = ["API 💾"]
self.appname: str = "BiliListener"
self._c_404: dict = {"code": 404, "message": "notfound", "data": {}}
self._c_422: dict = {"code": 422, "message": "parm error | method not allowed", "data": {}}
Expand Down Expand Up @@ -49,18 +48,19 @@ def __init__(self, config_data):
}
self.auth: str = config_data["auth"]
self.live_push_url: str = "https://api.live.bilibili.com/room/v1/Room/get_status_info_by_uids"
self.push_query_delay: int = config_data["query_delay"]
self.push_query_delay: int = config_data["live_query_delay"]
self.live_clear_delay: int = config_data["live_clear_delay"]

self.data_path: str = f"{__PATH__}/{config_data['data']['path']}"
self.hide_console: bool = config_data["hide_console"]

self.debug: bool = config_data["debug"]

config: BaseConfig = BaseConfig(read_config())


class Router:
api_tags: list = ["API 💾"]
api_perfix: list = "/api"
v1_root: str = f"{config.perfix}/v1"

stats_tags: list = ["STATS 📈"]
stats_perfix: str = "/stats"
Expand Down
17 changes: 9 additions & 8 deletions app/config/utils/_func_init_config.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,27 +6,28 @@


def init_config():
path = 'config.yml'
data = {
"api": {
"host": "127.0.0.1",
"port": 5000
"port": 5700
},
"query_delay": 30,
"live_query_delay": 30,
"live_clear_delay": 86400,
"data": {
"path": "data"
},
"auth": {
"sessdata": ""
},
"debug": False
"debug": False,
"hide_console": True
}

config_path = "config.yml"

if not os.path.exists(config_path):
with open(config_path, "w") as f:
if not os.path.exists(path):
with open(path, "w") as f:
yaml.dump(data, f, default_flow_style=False)
print(ASCII_LOGO.pusher)
print("config.yml 文件已生成, 请填写SESSDATA\n")
print(f"{path} 文件已生成, 请填写SESSDATA\n")
os.system("pause")
sys.exit(0)
4 changes: 3 additions & 1 deletion app/config/utils/_func_read_config.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
import yaml

from ._func_init_config import init_config

def read_config():
init_config()

with open("config.yml", "r") as f:
config = yaml.safe_load(f)

Expand Down
6 changes: 4 additions & 2 deletions app/pusher.py
Original file line number Diff line number Diff line change
@@ -1,18 +1,20 @@
import ctypes
import uvicorn
from colorama import init

from pusher.app import app
from config import config, ASCII_LOGO
from config.utils import init_config
from database.connector import config_database_init


init(autoreset=True)

kernel32 = ctypes.windll.kernel32
kernel32.SetConsoleMode(kernel32.GetStdHandle(-10), (0x4|0x80|0x20|0x2|0x10|0x1|0x00|0x100))

def main():
print(ASCII_LOGO.pusher)
config_database_init()
init_config()

uvicorn.run(app, host=config.host, port=config.port)

Expand Down
9 changes: 5 additions & 4 deletions app/pusher/app.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,14 +10,15 @@

from .utils import *
from .base_return import *
from config import config
from .router import MRouter
from config import config, Router
from .router.model._model_api import get_api
from .utils.tasks import live_status_inspectors
from .router.model.v1._model_api import get_api
from .utils.tasks import live_status_inspectors, live_clear_inspectors


scheduler = BackgroundScheduler()
scheduler.add_job(live_status_inspectors, "interval", seconds=config.push_query_delay)
scheduler.add_job(live_clear_inspectors, "interval", seconds=config.live_clear_delay)
scheduler.start()

_openapi = FastAPI.openapi
Expand Down Expand Up @@ -60,7 +61,7 @@ async def custom_swagger_ui_html():
swagger_ui_parameters={"defaultModelsExpandDepth": -1}
)

@app.get(Router.api_perfix, tags=Router.api_tags, response_model=get_api)
@app.get(config.perfix, tags=config.tags, response_model=get_api)
async def read_root():
return ret_200(data={
"server_time": func_time(info=True),
Expand Down
2 changes: 1 addition & 1 deletion app/pusher/router/__init__.py
Original file line number Diff line number Diff line change
@@ -1 +1 @@
from .main_router import MRouter
from .v1.main_router import MRouter
Empty file.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
18 changes: 18 additions & 0 deletions app/pusher/router/model/v1/_model_total.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
from pydantic import BaseModel, Field

class Total(BaseModel):
gift: int = Field(example=999, description="礼物数量")
enter: int = Field(example=999, description="进入房间次数")
guard: int = Field(example=999, description="舰队数量")
danmaku: int = Field(example=999, description="弹幕数量")
superchat: int = Field(example=999, description="超级聊天数量")

class Data(BaseModel):
count: int = Field(example=999, description="直播记录总数")
total: Total

class get_total(BaseModel):
code: int = Field(example=0, description="状态码")
message: str = Field(example="success", description="状态信息")
data: Data

File renamed without changes.
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,14 @@
from fastapi import APIRouter

from config import Router
from ..base_return import *
from ...base_return import *

from database import *
from database.model import *
from database.connector import get_db_config_session, get_db_worker_session

from .model._model_db2json_data import get_data_db2json
from .model._model_db2json_config import get_config_db2json
from ..model.v1._model_db2json_data import get_data_db2json
from ..model.v1._model_db2json_config import get_config_db2json


router = APIRouter(prefix=Router.livelogs_perfix, tags=Router.livelogs_tags)
Expand Down
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
from fastapi import APIRouter

from config import Router
from ..base_return import *
from ...base_return import *

from database import *
from database.model import UIDS
from database.connector import get_db_config_session

from .model._model_listening import get_listening
from .model._model_uids import get_uids
from ..model.v1._model_listening import get_listening
from ..model.v1._model_uids import get_uids


router = APIRouter(prefix=Router.info_perfix, tags=Router.info_tags)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,16 +2,17 @@
from fastapi import APIRouter

from config import Router
from ..base_return import *
from ...base_return import *

from database import *
from database.model import *
from database.connector import get_db_config_session

from ..model.v1._model_total import get_total

router = APIRouter(prefix=Router.stats_perfix, tags=Router.stats_tags)

@router.get("/total")
@router.get("/total", response_model=get_total)
async def get_total_():
with get_db_config_session() as config_db_session:
count = len(config_db_session.query(LIVE_DATA).filter(LIVE_DATA.end_timestamp != 0).all())
Expand All @@ -36,4 +37,3 @@ async def get_total_():
}

return ret_200(full_data)

Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
from fastapi import APIRouter

from config import Router
from ..base_return import *
from ...base_return import *

from database import *
from database.model import UIDS
from database.connector import get_db_config_session

from .model._model_edit_add import put_add_uid
from .model._model_del_uid import put_delete_uid
from ..model.v1._model_edit_add import put_add_uid
from ..model.v1._model_del_uid import put_delete_uid


router = APIRouter(prefix=Router.edit_perfix, tags=Router.edit_tags)
Expand All @@ -25,8 +25,8 @@ async def put_add_uid_(
else:
new_uid = UIDS(
uid=uid,
is_live = 0,
is_ban = 0
is_live=False,
is_ban=False
)
session.add(new_uid)
session.commit()
Expand Down
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
from config import config
from config import Router
from fastapi import APIRouter

from ._get_info import router as get_info_router
from ._put_edit import router as put_edit_router
from ._get_stats import router as get_stats_router
from ._get_db2json import router as get_db2json_router

MRouter = APIRouter(prefix=config.perfix)
MRouter = APIRouter(prefix=Router.v1_root)

MRouter.include_router(get_info_router)
MRouter.include_router(put_edit_router)
Expand Down
1 change: 1 addition & 0 deletions app/pusher/utils/tasks/__init__.py
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
from ._task_livestatus import live_status_inspectors
from ._task_liveclear import live_clear_inspectors
22 changes: 22 additions & 0 deletions app/pusher/utils/tasks/_task_liveclear.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
from blivedm import logger

from .utils import func_get_live_status

from database.model import UIDS
from database.connector import get_db_config_session


def live_clear_inspectors():
with get_db_config_session() as config_db_session:
uid_query_list = config_db_session.query(UIDS.uid).filter(UIDS.is_live == True).all()
uid_list = [uid[0] for uid in uid_query_list]
uid_dict = {"uids": uid_list}
live_config = func_get_live_status(uid_dict, False)

for x in live_config:
uid = x["uid"]
live_status_update = config_db_session.query(UIDS).filter(UIDS.uid == uid).first()
live_status_update.is_live = False

config_db_session.commit()
logger.info(f"PREPARING LISTENING {uid}")
10 changes: 7 additions & 3 deletions app/pusher/utils/tasks/_task_livestatus.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import subprocess
from fastapi.logger import logger
from blivedm import logger

from config import config
from .utils import func_get_live_status

from database.model import UIDS, LIVE_DATA
from database.utils import func_generate_uuid
from database.connector import get_db_config_session
Expand Down Expand Up @@ -44,6 +44,10 @@ def live_status_inspectors():
config_db_session.commit()

command = f"worker.exe {uid} {rid} {live_time}"
subprocess.Popen(["cmd", "/c", command], creationflags=subprocess.CREATE_NEW_CONSOLE)

if config.console:
subprocess.Popen(["cmd", "/c", command], creationflags=subprocess.CREATE_NO_WINDOW)
else:
subprocess.Popen(["cmd", "/c", command], creationflags=subprocess.CREATE_NEW_CONSOLE)

logger.warning(f"STARTING LISTENING {uid}")
22 changes: 18 additions & 4 deletions app/pusher/utils/tasks/utils/_func_livestatus.py
Original file line number Diff line number Diff line change
@@ -1,13 +1,27 @@
from blivedm import logger

from .api import api_query_live_status

def func_get_live_status(uid_list):
def func_get_live_status(
uid_list: list = [],
check_live_code: bool = True,
):
live_list = []
if check_live_code:
live_status = 1
else:
live_status = 0

ret_json = api_query_live_status(uid_list)
if ret_json["code"] != 0:
return live_list

for x, c in ret_json["data"].items():
if c["live_status"] == 1:
live_list.append(c)
try:
for x, c in ret_json["data"].items():
if c["live_status"] == live_status:
live_list.append(c)
except:
# logger.error("UID错误 {}")
return []

return live_list
4 changes: 4 additions & 0 deletions app/worker.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,13 @@
import sys
import ctypes

from config import ASCII_LOGO
from worker.app import worker_initializer


kernel32 = ctypes.windll.kernel32
kernel32.SetConsoleMode(kernel32.GetStdHandle(-10), (0x4|0x80|0x20|0x2|0x10|0x1|0x00|0x100))

def main():
print(ASCII_LOGO.worker)

Expand Down
Loading

0 comments on commit 3c5beab

Please sign in to comment.