You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

81 lines
2.8 KiB

from nonebot import require, get_bots
import xml.etree.ElementTree as ET
import requests
import json
import os
streamer_list = {"Unreal__Dreamer":[198425889, 906923500], "iMaximizing":[198425889]}
#group_id = 372637992
cache_file = "./twitch_config.json"
url = 'http://172.16.1.11:1200/twitch/live/'
scheduler = require("nonebot_plugin_apscheduler").scheduler
@scheduler.scheduled_job("interval", seconds=60*2)
async def twitch_sub_run():
# create the api instance and get user auth either from storage or website
if not os.path.exists(cache_file) or os.path.getsize(cache_file) == 0:
f = open(cache_file, "w")
d = {}
json.dump(d, f)
f.close()
f = open(cache_file, "r")
data = json.load(f)
f.close()
# get the currently logged in user
for streamer in streamer_list:
message = ""
file_changed = False
response = requests.get(url+streamer)
rss_data = response.content
# 解析XML数据
root = ET.fromstring(rss_data)
# 提取并打印新闻标题和链接
l = root.findall('.//item')
if len(l) > 0:
title = l[0].find('title').text
if not streamer in data:
data[streamer]={}
data[streamer]["title"] = title
data[streamer]["status"] = 1
file_changed = True
else:
if data[streamer]["title"] != title:
data[streamer]["title"] = title
message = streamer + " 直播间更新:\n" + data[streamer]["title"]
file_changed = True
if data[streamer]["status"] == 0:
data[streamer]["title"] = title
data[streamer]["status"] = 1
message = streamer + " 开播了!\n" + data[streamer]["title"]
file_changed = True
else:
if not streamer in data:
data[streamer]={}
data[streamer]["title"] = "title"
data[streamer]["status"] = 0
file_changed = True
else:
if data[streamer]["status"] == 1:
data[streamer]["status"] = 0
message = streamer + " 下播了"
file_changed = True
if file_changed and message != "":
# 获取 self_id 为 12345 的 bot 实例
bots = get_bots()
# 所有机器人都向群 ID 为 10000 的群发送消息
for bot in bots.values():
for gid in streamer_list[streamer]:
await bot.send_group_msg(group_id=gid, message=message)
print("changed")
if file_changed:
f = open(cache_file, "w")
json.dump(data, f)
f.close()