This repository has been archived by the owner on Apr 10, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathlive_danmaku.py
71 lines (65 loc) · 2.51 KB
/
live_danmaku.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
import bilib
import time
import sys
from colorama import init,Fore,Back,Style
init(autoreset=True)
def color_print(string,color):
output = ""
color == str(color)
if color == str("BLACK"):
output = str(Fore.BLACK + str(string) + Style.RESET_ALL)
elif color == str("RED"):
output = str(Fore.RED + str(string) + Style.RESET_ALL)
elif color == str("GREEN"):
output = str(Fore.GREEN + str(string) + Style.RESET_ALL)
elif color == str("YELLOW"):
output = str(Fore.YELLOW + str(string) + Style.RESET_ALL)
elif color == str("BLUE"):
output = str(Fore.BLUE + str(string) + Style.RESET_ALL)
elif color == str("MAGENTA"):
output = str(Fore.MAGENTA + str(string) + Style.RESET_ALL)
elif color == str("CYAN"):
output = str(Fore.CYAN + str(string) + Style.RESET_ALL)
elif color == str("WHITE"):
output = str(Fore.WHITE + str(string) + Style.RESET_ALL)
else:
output = str(string)
return output
# 模式0:直接输入直播间ID
# 模式1:根据UID查直播间ID
# 模式2:根据UID查直播间ID(即使未开播也尝试显示弹幕)
mode = 1
now_timestamp = 0
timesleep = 0.1
roomid = 88621
uid = 5970160
if mode == 0:
pass
else:
userinfo = bilib.user_info(uid)
status = int(userinfo["liveStatus"])
roomid = userinfo["stream_room_id"]
name = userinfo["name"]
print(("%s(UID:%s)的直播间号为%s")%(color_print(str(name),"CYAN"),color_print(str(uid),"MAGENTA"),color_print(str(roomid),"GREEN")))
if mode == 1 and status == 0:
print("[" + color_print("警告","RED") + "]该直播间没有开播!")
print("使用" + color_print("模式2","YELLOW") + "以尝试强制输出弹幕")
sys.exit(1)
else:
pass
print(("房间号:%s")%(str(roomid)))
print(("加载弹幕冷却时间:%s秒")%(str(timesleep)))
time.sleep(2)
while True:
danmaku_list = bilib.listall_danmaku_live(roomid,type="room")
for index,include in danmaku_list.items():
if now_timestamp < int(include['timestamp']):
now_timestamp = int(include['timestamp'])
now_time = color_print(str(time.strftime("%Y-%m-%d %H:%M:%S",time.localtime(now_timestamp))),"GREEN")
text = color_print(include['text'],"CYAN")
name = color_print(include['name'],"YELLOW")
uid = color_print(include['uid'],"YELLOW")
print(("[%s](%s,%s)%s")%(str(now_time),str(name),str(uid),str(text)))
else:
continue
time.sleep(timesleep)