64 lines
2.1 KiB
Python
64 lines
2.1 KiB
Python
import requests
|
|
import time
|
|
|
|
# 配置信息
|
|
ROOM_ID = "7718843"
|
|
CHECK_INTERVAL = 300 # 5分钟 = 300秒
|
|
HEADERS = {
|
|
"User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/120.0.0.0 Safari/537.36",
|
|
"Referer": f"https://www.douyu.com/{ROOM_ID}"
|
|
}
|
|
|
|
|
|
class DouyuMonitor:
|
|
def __init__(self, room_id):
|
|
self.room_id = room_id
|
|
self.api_url = f"https://www.douyu.com/betard/{room_id}"
|
|
self.is_live = False # 记录当前直播状态
|
|
|
|
def check_status(self):
|
|
try:
|
|
# 发送请求
|
|
response = requests.get(self.api_url, headers=HEADERS, timeout=10)
|
|
data = response.json()
|
|
print(data)
|
|
# 提取房间信息和状态
|
|
room_info = data.get("room", {})
|
|
show_status = room_info.get("show_status") # 1 为开播
|
|
room_name = room_info.get("room_name", "无标题")
|
|
nickname = room_info.get("nickname", "未知主播")
|
|
|
|
# 逻辑判定
|
|
if show_status == 1:
|
|
if not self.is_live:
|
|
print(f"【通知】主播 {nickname} 开播了!标题:{room_name}")
|
|
self.send_desktop_notification(nickname, room_name)
|
|
self.is_live = True
|
|
else:
|
|
print(f"[{time.strftime('%H:%M:%S')}] 主播正在直播中...")
|
|
else:
|
|
if self.is_live:
|
|
print(f"[{time.strftime('%H:%M:%S')}] 主播下播了。")
|
|
self.is_live = False
|
|
print(f"[{time.strftime('%H:%M:%S')}] 主播尚未开播。")
|
|
|
|
except Exception as e:
|
|
print(f"查询出错: {e}")
|
|
|
|
def send_desktop_notification(self, streamer, title):
|
|
"""发送系统桌面通知"""
|
|
print(f"斗鱼直播提醒: {streamer}" + f"你关注的主播开播啦!\n标题:{title}"
|
|
)
|
|
|
|
|
|
if __name__ == "__main__":
|
|
monitor = DouyuMonitor(ROOM_ID)
|
|
print(f"开始监控直播间: {ROOM_ID},每 5 分钟检查一次...")
|
|
|
|
while True:
|
|
monitor.check_status()
|
|
time.sleep(CHECK_INTERVAL)
|
|
|
|
|
|
|