async def send_youtube_event(ytb_event: YoutubeEvent): # noinspection PyTypeChecker vtuber = await get_vtuber(ytb_event.channel) video = ytb_event.video event: Optional[Event] = None if ytb_event.type == ResourceType.VIDEO and not await get_option( "video_disabled"): event = Event( "youtube_video", vtuber.key, { "title": video.title, "description": video.description, "images": [video.thumbnail], "link": video.link }) elif ytb_event.type == ResourceType.BROADCAST: scheduled_start_time_print = video.scheduled_start_time.strftime( "%Y-%m-%d %I:%M%p (CST)") if ytb_event.event == YoutubeEventType.LIVE and not await get_option( "live_disabled"): actual_start_time_print = video.actual_start_time.strftime( "%Y-%m-%d %I:%M%p (CST)") event = Event( "youtube_broadcast_live", vtuber.key, { "title": video.title, "description": video.description, "link": video.link, "images": [video.thumbnail], "scheduled_start_time": scheduled_start_time_print, "actual_start_time": actual_start_time_print }) elif ytb_event.event == YoutubeEventType.REMINDER and not await get_option( "reminder_disabled"): event = Event( "youtube_broadcast_reminder", vtuber.key, { "title": video.title, "description": video.description, "link": video.link, "images": [video.thumbnail], "scheduled_start_time": scheduled_start_time_print, }) elif ytb_event.event == YoutubeEventType.SCHEDULE and not await get_option( "schedule_disabled"): event = Event( "youtube_broadcast_schedule", vtuber.key, { "title": video.title, "description": video.description, "link": video.link, "images": [video.thumbnail], "scheduled_start_time": scheduled_start_time_print, }) if event: await app.send_event(event)
async def bilibili_task(): if await get_option("disabled"): return b_since: KVPair = await app.plugin_state.get("bilibili_since") b_valid_ids = [] b_names = [] # noinspection PyTypeChecker async for vtuber in app.vtubers.has_field("bilibili"): b_names.append(vtuber.key) b_valid_ids.append(vtuber.value["bilibili"]) dyns = await asyncio.gather( *(bilibili.fetch(b_id, b_since.value.get(b_name, 1)) for b_name, b_id in zip(b_names, b_valid_ids))) valid_dyns = {name: dyn for name, dyn in zip(b_names, dyns) if dyn[1]} since = {name: dyn[0] for name, dyn in valid_dyns.items()} b_since.value.update(since) await app.plugin_state.put(b_since) dyn: Dynamic events = (Event(dyn.type.to_event(), name, { "text": dyn.text, "images": dyn.photos, "link": dyn.link }) for name, dyn_set in valid_dyns.items() for dyn in dyn_set[1]) await asyncio.gather(*(app.send_event(event) for event in events))
async def send_youtube_event(ytb_event: YoutubeEvent): # noinspection PyTypeChecker vtuber = await get_vtuber(ytb_event.channel) video = ytb_event.video scheduled_start_time_print = video.scheduled_start_time.strftime("%Y-%m-%d %I:%M%p (CST)") \ if video.scheduled_start_time else None actual_start_time_print = video.actual_start_time.strftime("%Y-%m-%d %I:%M%p (CST)") \ if video.actual_start_time else None body = { "title": video.title, "description": video.description, "link": video.link, "images": [video.thumbnail] if ytb_event.event != YoutubeEventType.SCHEDULE and video.thumbnail else [] } if scheduled_start_time_print: body["scheduled_start_time"] = scheduled_start_time_print if actual_start_time_print: body["actual_start_time"] = actual_start_time_print event = Event(ytb_event.event.value, vtuber.key, body) await app.send_event(event)
async def twitter_task(): t_since: KVPair = await app.plugin_state.get("twitter_since") t_valid_ids = [] t_names = [] # noinspection PyTypeChecker async for vtuber in app.vtubers.has_field("twitter"): t_names.append(vtuber.key) t_valid_ids.append(vtuber.value["twitter"]) tweets = await asyncio.gather( *(twitter.fetch(t_id, t_since.value.get(t_name, 1)) for t_name, t_id in zip(t_names, t_valid_ids))) valid_tweets = { name: tweet for name, tweet in zip(t_names, tweets) if tweet[1] } since = {name: tweet[0] for name, tweet in valid_tweets.items()} t_since.value.update(since) await app.plugin_state.put(t_since) tweet: Tweet events = (Event("t_rt" if tweet.is_rt else "t_tweet", name, { "text": tweet.text, "images": tweet.photos, "link": tweet.link }) for name, tweet_set in valid_tweets.items() for tweet in tweet_set[1]) await asyncio.gather(*(app.send_event(event) for event in events))
async def on_live(client: LiveClient, command: dict): vtuber = await get_vtuber_by_uid(client.room_owner_uid) live_room = await LiveRoom.from_room_id(client.room_id) body = { "title": live_room.title, "link": f"https://live.bilibili.com/{client.room_id}", "images": [cover] if (cover := live_room.cover) else [] } event = Event("bili_live", vtuber.key, body) await app.send_event(event)
async def eeehihihi(): if await get_option("disabled"): return await app.send_event(Event("dummy_suisei", "suisei", {}))
async def ws_send(event: Event): for client in ws_clients: await client.send_json(event.to_json())