async def dump_state(): channel_state = { channel: [video.dump() for video in videos] for channel, videos in channel_list.items() } read_state = {"videos": [video.dump() for video in read_list]} await app.plugin_state.put(KVPair("youtube_live_state", channel_state)) await app.plugin_state.put(KVPair("youtube_video_state", read_state))
async def load_state(): global channel_list global read_list try: channel_state = await app.plugin_state.get("youtube_live_state") except KeyError: logging.warning("Missing live state dict. Ignoring.") channel_state = KVPair("youtube_live_state", {}) try: read_state = await app.plugin_state.get("youtube_video_state") except KeyError: logging.warning("Missing video state dict. Ignoring.") read_state = KVPair("youtube_video_state", {"videos": []}) for channel, videos in channel_state.value.items(): for _video in videos: video = Video.load(_video) if await video.fetch() and not video.actual_start_time: logging.debug(f"Load saved broadcast: {video}") event_reminder = YoutubeEvent(type=video.type, event=YoutubeEventType.REMINDER, channel=channel, video=video) # set a reminder job_id = f"reminder_{channel}_{video.video_id}" reminder_time = video.scheduled_start_time - datetime.timedelta( minutes=30) if reminder_time > datetime.datetime.now().replace( tzinfo=tz.tzlocal()): scheduler.add_job(partial(send_youtube_event, event_reminder), trigger="cron", id=job_id, year=reminder_time.year, month=reminder_time.month, day=reminder_time.day, hour=reminder_time.hour, minute=reminder_time.minute, second=reminder_time.second) channel_list[channel].append(video) read_list = [Video.load(video) for video in read_state.value["videos"]]
async def delete(self, request: Request): table = get_table(request.path_params["table"]) key = request.path_params["prime_key"] try: await table.get(key) except KeyError: return PlainTextResponse("Not Found", status_code=HTTP_404_NOT_FOUND) await table.delete(KVPair(key, {})) return Response()
async def post(self, request: Request): try: table = get_table(request.path_params["table"]) except KeyError: return PlainTextResponse("Not Found", status_code=HTTP_404_NOT_FOUND) key = (await request.body()).decode("utf-8") try: await table.get(key) except KeyError: await table.put(KVPair(key, {})) return RedirectResponse(url=urljoin( app.credentials.get("base_url"), key), status_code=HTTP_201_CREATED) return PlainTextResponse("Conflict", status_code=HTTP_409_CONFLICT)
async def bilibili_setup(): try: await app.plugin_state.get("bilibili_since") except KeyError: await app.plugin_state.put(KVPair("bilibili_since", {}))
async def put(self, obj: KVPair) -> Tuple[Optional[KVPair], KVPair]: if old_doc := (await self.collections.find_one({"key": obj.key})): await self.collections.replace_one({"_id": old_doc["_id"]}, obj.dump()) return KVPair.load(old_doc), obj
async def has_field(self, field: str) -> AsyncGenerator[KVPair, None]: async for doc in self.collections.find({field: {"$exists": True}}): yield KVPair.load(doc)
async def get(self, key: str) -> KVPair: doc: dict = await self.collections.find_one({"key": key}) return KVPair.load(doc) if doc else None
async def iter(self) -> AsyncGenerator[KVPair, None]: async for doc in self.collections.find(): yield KVPair.load(doc)
async def twitter_startup(): try: await app.plugin_state.get("twitter_since") except KeyError: await app.plugin_state.put(KVPair("twitter_since", {}))