Skip to content

Commit

Permalink
Merge pull request #22 from TZFC/dev
Browse files Browse the repository at this point in the history
sync from dev
  • Loading branch information
TZFC authored Dec 8, 2023
2 parents ac170af + 8b169a3 commit c688ba3
Show file tree
Hide file tree
Showing 7 changed files with 73 additions and 27 deletions.
5 changes: 4 additions & 1 deletion Configs/config{room_id}.json
Original file line number Diff line number Diff line change
Expand Up @@ -21,11 +21,14 @@
"辣条",
"粉丝团灯牌"
],
"blacklist": [
],
"repo": "https://space.bilibili.com/654321/channel/seriesdetail?sid=123456&ctype=0",
"master": "tzfc",
"feature_flags": {
"replay_comment": 0,
"renqi_remind": 0,
"unban": 1
"unban": 0,
"checkin": 0
}
}
7 changes: 2 additions & 5 deletions Utils/BanWithTimeout.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,15 +5,12 @@


async def ban_with_timeout(live_room: LiveRoom, uid: int, timeout: int, database):
try:
await live_room.ban_user(uid)
except ResponseCodeException:
return
await live_room.ban_user(uid)
await asyncio.sleep(timeout)
try:
await live_room.unban_user(uid)
except ResponseCodeException:
return
pass
sql = "DELETE FROM banned WHERE uid=%s AND room_id=%s"
val = (uid, live_room.room_display_id)
with database.cursor() as cursor:
Expand Down
36 changes: 36 additions & 0 deletions Utils/Checkin.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
from datetime import datetime


async def record_checkin(start_time: datetime, end_time: datetime, master: str, blacklist: list, room_id: int,
database):
with database.cursor() as cursor:
sql = "SELECT dedeuserid FROM credentials WHERE master = %s"
val = (master,)
cursor.execute(sql, val)
dedeuserid = int(cursor.fetchall()[0][0])

sql = "SELECT slot_0, slot_1, slot_2, slot_3, slot_4, slot_5, slot_6, slot_7, slot_8, slot_9 FROM checkin WHERE uid = %s AND room_id = %s"
val = (dedeuserid, room_id)
cursor.execute(sql, val)
slots: tuple = cursor.fetchall()[0]
head = slots.index(1)
next_head = head + 1 if head != 9 else 0

sql = "SELECT DISTINCT uid FROM danmu WHERE room_id = %s AND time BETWEEN %s AND %s"
val = (room_id, start_time, end_time)
cursor.execute(sql, val)
result = cursor.fetchall()
unique_uid = [item[0] for item in result]

sql = f"UPDATE checkin SET slot_{head} = 0 WHERE room_id = %s"
val = (room_id,)
cursor.execute(sql, val)

sql = f"INSERT INTO checkin (room_id, uid, slot_{head}) VALUES(%s, %s, 1) ON DUPLICATE KEY UPDATE slot_{head} = 1"
val = [(room_id, uid) for uid in unique_uid if uid not in blacklist and uid != dedeuserid]
cursor.executemany(sql, val)

sql = f"UPDATE checkin SET slot_{next_head} = 1 where room_id = %s AND uid = %s"
val = (room_id, dedeuserid)
cursor.execute(sql, val)
database.commit()
6 changes: 3 additions & 3 deletions Utils/CredentialGetter.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ def get_credential(master: str) -> Credential:
with connect(**load(open("Configs/mysql.json"))) as mydb:
with mydb.cursor() as cursor:
cursor.execute(
f"SELECT sessdata, bili_jct, buvid3, ac_time_value FROM credentials WHERE master = '{master}'")
sess, jct, bd3, act = cursor.fetchone()
credential = Credential(sessdata=sess, bili_jct=jct, buvid3=bd3, ac_time_value=act)
f"SELECT sessdata, bili_jct, buvid3, ac_time_value, dedeuserid FROM credentials WHERE master = '{master}'")
sessdata, bili_jct, buvid3, ac_time_value, dedeuserid = cursor.fetchone()
credential = Credential(sessdata=sessdata, bili_jct=bili_jct, buvid3=buvid3, ac_time_value=ac_time_value, dedeuserid=dedeuserid)
return credential
26 changes: 13 additions & 13 deletions Utils/Summarizer.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,19 +15,19 @@ def isDeng(row: dict, field_index: dict, room_id: int, room_config) -> bool:
return False


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,))
result = cursor.fetchall()
if not result:
return None, None, None, None
start_time, end_time = result[0]
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()
field_index = {field_name: index for index, field_name in enumerate(cursor.column_names)}
def summarize(room_id: int, database) -> (str, str, datetime, datetime):
with database.cursor() as cursor:
sql = "SELECT start, end FROM liveTime WHERE room_id = %s AND end IS NOT NULL AND summary IS NULL"
val = (room_id,)
cursor.execute(sql, val)
result = cursor.fetchall()
if not result:
return None, None, None, None
start_time, end_time = result[0]
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()
field_index = {field_name: index for index, field_name in enumerate(cursor.column_names)}

# 找出路灯关键词
room_config = load(open(f"Configs/config{room_id}.json"))
Expand Down
12 changes: 11 additions & 1 deletion master.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
from mysql.connector import connect

from Utils.BanOnKeyword import ban_on_keyword
from Utils.Checkin import record_checkin
from Utils.CredentialGetter import get_credential
from Utils.EVENT_IDX import *
from Utils.EmailSender import send_mail_async
Expand Down Expand Up @@ -114,7 +115,7 @@ async def live_end(event):
mydb.commit()

# 提炼路灯邮件文 及 跳转文
email_text, jump_text, start_time, end_time = summarize(room_id)
email_text, jump_text, start_time, end_time = summarize(room_id, database=mydb)
if not any([email_text, jump_text, start_time, end_time]):
return

Expand All @@ -130,6 +131,15 @@ async def live_end(event):
subject=f"{roomConfigs[room_id]['nickname']}{start_time}路灯",
text="本期无路灯", mime_text=f"{event}"))

if roomConfigs[room_id]["feature_flags"]["checkin"]:
# 统计直播间发言人
await record_checkin(start_time=start_time,
end_time=end_time,
master=roomConfigs[room_id]['master'],
blacklist=roomConfigs[room_id]['blacklist'],
room_id=room_id,
database=mydb)

if roomConfigs[room_id]["feature_flags"]["replay_comment"]:
# 记录路灯跳转
sql = "UPDATE liveTime SET summary = %s WHERE room_id = %s AND end IS NOT NULL AND summary IS NULL"
Expand Down
8 changes: 4 additions & 4 deletions postReplayComment.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,14 +24,14 @@
if not roomConfigs[room_id]["feature_flags"]["replay_comment"]:
continue
# get available summaries
sql = "SELECT start, summary FROM liveTime WHERE room_id=%s AND end IS NOT NULL AND summary IS NOT NULL ORDER BY start ASC"
sql = "SELECT start, summary FROM liveTime WHERE room_id=%s AND end IS NOT NULL AND summary IS NOT NULL ORDER BY start"
val = (room_id,)
cursor.execute(sql, val)
summaries = cursor.fetchall()
if not summaries:
continue

# get all videos in repo
# get all videos details in repo
repo = roomConfigs[room_id]["repo"]
split_repo = repo.split("/")
if split_repo[-1] == "video":
Expand Down Expand Up @@ -60,13 +60,13 @@
sql = "SELECT aid FROM postProgress WHERE room_id=%s"
val = (room_id,)
cursor.execute(sql, val)
prev_aid = cursor.fetchall()[0]
prev_aid = cursor.fetchall()[0][0]
if prev_aid == details[0]['aid']:
continue

# match each available summary with videos
success = []
for i in range(len(summaries)):
for i in reversed(range(len(summaries))):
title = details[i]["title"]
four_part_date = re.search(r"(\d+)年(\d+)月(\d+)日(\d+)点", title)
if four_part_date:
Expand Down

0 comments on commit c688ba3

Please sign in to comment.