Skip to content

Commit

Permalink
Reformatted Code and Optimized Imports; Added documentation for refre…
Browse files Browse the repository at this point in the history
…shCredentials
  • Loading branch information
TZFC committed Nov 15, 2023
1 parent af3ef44 commit 4f2baa0
Show file tree
Hide file tree
Showing 9 changed files with 101 additions and 49 deletions.
26 changes: 18 additions & 8 deletions Configs/config{room_id}.json
Original file line number Diff line number Diff line change
@@ -1,10 +1,20 @@
{
"room_id":"{room_id}",
"nickname":"昵称",
"listener_email":["[email protected]", "[email protected]"],
"keyword":["--","=="],
"required_level": 10,
"repo":[],
"master":"tzfc",
"feature_flags": {"replay_comment": 0, "renqi_remind": 0, "ban": 0}
"room_id": "{room_id}",
"nickname": "昵称",
"listener_email": [
"[email protected]",
"[email protected]"
],
"keyword": [
"--",
"=="
],
"required_level": 10,
"repo": [],
"master": "tzfc",
"feature_flags": {
"replay_comment": 0,
"renqi_remind": 0,
"ban": 0
}
}
11 changes: 8 additions & 3 deletions Configs/masterConfig.json
Original file line number Diff line number Diff line change
@@ -1,5 +1,10 @@
{
"username":"[email protected]",
"password":"PASSWORD",
"room_ids":["23596840", "870004", "23808442", "5561470"]
"username": "[email protected]",
"password": "PASSWORD",
"room_ids": [
"23596840",
"870004",
"23808442",
"5561470"
]
}
10 changes: 5 additions & 5 deletions Configs/mysql.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"host":"{host_url}",
"port":"{port}",
"user":"tzfc",
"password":"{password}",
"database":"{database_name}"
"host": "{host_url}",
"port": "{port}",
"user": "tzfc",
"password": "{password}",
"database": "{database_name}"
}
11 changes: 7 additions & 4 deletions EmailSender.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,12 @@
import aiosmtplib

from email.mime.multipart import MIMEMultipart
from email.mime.text import MIMEText
from json import load
async def send_mail_async(sender:str, to:list[str], subject:str, text:str, mimeText: str, image = None, textType='plain'):

import aiosmtplib


async def send_mail_async(sender: str, to: list[str], subject: str, text: str, mimeText: str, image=None,
textType='plain'):
msg = MIMEMultipart()
msg['Subject'] = subject
msg['From'] = sender
Expand All @@ -18,4 +21,4 @@ async def send_mail_async(sender:str, to:list[str], subject:str, text:str, mimeT
login_info = load(open("Configs/masterConfig.json"))
await smtp.login(login_info["username"], login_info["password"])
await smtp.send_message(msg)
await smtp.quit()
await smtp.quit()
54 changes: 41 additions & 13 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,64 +1,78 @@
# BiliLiveManager

Basic features:
Send email on Live start
Send email with danmaku preceded by keyword on Live end
Send email on Live start
Send email with danmaku preceded by keyword on Live end

Optional features:
Send comment to jump to marked timestamp under livestream replay
Send reminder on the hour
Ban/Unban users on keywords
Record and report active user ranking
Send comment to jump to marked timestamp under livestream replay
Send reminder on the hour
Ban/Unban users on keywords
Record and report active user ranking
Welcome to the BiliLiveManager wiki!

# How to deploy?

## 1. Install python **3.11** and dependencies

Note: python3.10 and python3.12 WILL NOT WORK

Create virtual environment

```
python3.11 -m venv py311
```

Activate virtual environment

```
source py311/bin/activate
```

Install dependencies

```
pip install -r requirements.txt
```

## 2. Clone this repo

```
git clone https://github.com/TZFC/BiliLiveManager
```

Note: Use the production branch if you already have ./Configs

## 3. Create mysql database

Fill information into [Configs/mysql.json](https://github.com/TZFC/BiliLiveManager/blob/main/Configs/mysql.json)

Create table credentials

```
CREATE TABLE credentials (user VARCHAR, sessdata VARCHAR, bili_jct VARCHAR, buvid3 VARCHAR, ac_time_value VARCHAR)
```

Create table liveTime

```
CREATE TABLE credentials (room_id INT, start TIMESTAMP, end TIMESTAMP, summary VARCHAR)
```

Create table danmu

```
CREATE TABLE danmu (name VARCHAR, uid INT, text VARCHAR, medal_id INT, medal_level INT, time TIMESTAMP, room_id INT)
```

## 4. Log into bilibili.com in **PRIVATE** browser window

Get credentials following [this guide](https://nemo2011.github.io/bilibili-api/#/get-credential)

Store your credentials in database

User is your custom nickname

```
from json import load
from mysql.connector import connect
Expand All @@ -71,30 +85,44 @@ mydb.commit()
```

## 5. Get gmail credentials

Get gmail app password following [this guide](https://support.google.com/mail/answer/185833?hl=en)

Fill your email address and app password into username and password fields of [Configs/masterCongid.json](https://github.com/TZFC/BiliLiveManager/blob/main/Configs/masterConfig.json)
Fill your email address and app password into username and password fields
of [Configs/masterCongid.json](https://github.com/TZFC/BiliLiveManager/blob/main/Configs/masterConfig.json)

## 6. Fill in which Stream to monitor
Fill the room id into room_ids of [Configs/masterConfig.json](https://github.com/TZFC/BiliLiveManager/blob/main/Configs/masterConfig.json)

Fill the room id into room_ids
of [Configs/masterConfig.json](https://github.com/TZFC/BiliLiveManager/blob/main/Configs/masterConfig.json)

Note: Put room id in quote!

## 7. Create a config{room_id}.json per streamer
See template [Configs/config{room_id}.json](https://github.com/TZFC/BiliLiveManager/blob/main/Configs/config%7Broom_id%7D.json)

See
template [Configs/config{room_id}.json](https://github.com/TZFC/BiliLiveManager/blob/main/Configs/config%7Broom_id%7D.json)

Note: Feature flag 0 is OFF ; 1 is ON

## 8. Run master.py

```
python master.py &
```

## 9. (optional) Run refreshCredentials.py
You may want to set a cron job to run it daily at noon
## 10. Run refreshCredentials.py

set a cron job to run it hourly at 55th minute

```
crontab -e
55 * * * * python poppularTicketRemind.py
```

## 10. (optional) Run refreshCredentials.py

You may want to set a cron job to run it daily at noon

```
0 12 * * * python refreshCredentials.py
```
```
15 changes: 9 additions & 6 deletions Summarizer.py
Original file line number Diff line number Diff line change
@@ -1,25 +1,28 @@
from datetime import datetime
from json import load

from mysql.connector import connect
import datetime


def isDeng(row: dict, field_index: dict, room_id: int, roomConfig) -> bool:
text = row[field_index["text"]]
keywords = roomConfig["keyword"]
required_level = roomConfig["required_level"]
if (text[:2] in keywords
and str(row[field_index["medal_id"]]) == str(room_id)
and row[field_index["medal_level"]] >= required_level):
and str(row[field_index["medal_id"]]) == str(room_id)
and row[field_index["medal_level"]] >= required_level):
return True
else:
return False

def summarize(room_id: int) -> (str, str, datetime.datetime, datetime.datetime):

def summarize(room_id: int) -> (str, str, datetime, datetime):
with connect(**load(open("Configs/mysql.json"))) as mydb:
with mydb.cursor() as cursor:
cursor.execute("SELECT start, end FROM liveTime WHERE room_id = %s AND end IS NOT NULL AND summary IS NULL", (room_id,))
start_time, end_time = cursor.fetchall()[0] # TODO: change back to fetchone and throw exception when more than one
cursor.execute("SELECT start, end FROM liveTime WHERE room_id = %s AND end IS NOT NULL AND summary IS NULL",
(room_id,))
start_time, end_time = cursor.fetchall()[
0] # TODO: change back to fetchone and throw exception when more than one
cursor.execute("SELECT * FROM danmu WHERE room_id = %s AND time BETWEEN %s AND %s",
(room_id, start_time, end_time))
raw_danmu = cursor.fetchall()
Expand Down
12 changes: 7 additions & 5 deletions master.py
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
import asyncio
from datetime import datetime
from json import load

from bilibili_api import Danmaku, sync
from bilibili_api.live import LiveDanmaku, LiveRoom
from json import load
from mysql.connector import connect
from datetime import datetime

from CredentialGetter import getCredential
from EmailSender import send_mail_async
from Summarizer import summarize
Expand All @@ -29,8 +30,10 @@
liveRooms = {room: LiveRoom(room, credential=masterCredentials[room]) for room in ROOM_IDS}
mydb = connect(**load(open("Configs/mysql.json")))


def bind(room: LiveDanmaku):
__room = room

@__room.on("DANMU_MSG")
async def recv(event):
room_id = event['room_display_id']
Expand Down Expand Up @@ -60,7 +63,6 @@ async def recv(event):
cursor.execute(sql, val)
mydb.commit()


@__room.on("LIVE")
async def liveStart(event):
room_id = event['room_display_id']
Expand All @@ -80,7 +82,7 @@ async def liveStart(event):
area = info['room_info']['area_name']
tg.create_task(send_mail_async(sender=masterConfig["username"], to=roomConfigs[room_id]["listener_email"],
subject=f"{roomConfigs[room_id]['nickname']}开始直播{title}",
text=f"{event}", mimeText = area, image = image))
text=f"{event}", mimeText=area, image=image))

# 发送打招呼弹幕
tg.create_task(liveRooms[room_id].send_danmaku(Danmaku("来啦!")))
Expand All @@ -92,7 +94,6 @@ async def liveStart(event):
cursor.execute(sql, val)
mydb.commit()


@__room.on("PREPARING")
async def liveEnd(event):
room_id = event['room_display_id']
Expand Down Expand Up @@ -142,6 +143,7 @@ async def liveEnd(event):
cursor.execute(sql, val)
mydb.commit()


for room in liveDanmakus.values():
bind(room)

Expand Down
5 changes: 2 additions & 3 deletions popularTicketRemind.py
Original file line number Diff line number Diff line change
@@ -1,12 +1,11 @@
from json import load
import time
from json import load

from bilibili_api import sync, Danmaku
from bilibili_api.live import LiveDanmaku, LiveRoom

from CredentialGetter import getCredential


masterConfig = load(open(f"Configs/masterConfig.json"))
ROOM_IDS = masterConfig["room_ids"]
roomConfigs = {room: load(open(f"Configs/config{room}.json")) for room in ROOM_IDS}
Expand All @@ -27,4 +26,4 @@
if live_start_time + 3600 > time.time():
continue
liveRooms[room_id].send_danmaku(Danmaku("点点上方免费人气票!"))
time.sleep(5)
time.sleep(5)
6 changes: 4 additions & 2 deletions refreshCredentials.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,10 @@

from bilibili_api import sync
from mysql.connector import connect

from CredentialGetter import getCredential
#from Utils.EmailSender import sendEmail

# from Utils.EmailSender import sendEmail

# Hard coding 'tzfc' for now. Will iterate through user list when needed
credential = getCredential("tzfc")
Expand All @@ -14,4 +16,4 @@
with connect(**json.load(open("./mysql.json"))) as mydb:
with mydb.cursor() as cursor:
cursor.execute(sql, val)
mydb.commit()
mydb.commit()

0 comments on commit 4f2baa0

Please sign in to comment.