From 0e1e87ec05972d19511ad06c74e82cec32812db3 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E3=81=BF=E3=81=A6=E3=82=8B=E3=81=9E?= Date: Sat, 3 Jan 2026 13:23:32 +0900 Subject: [PATCH] =?UTF-8?q?feat:=20=E5=9B=9E=E7=AD=94=E9=81=85=E5=BB=B6?= =?UTF-8?q?=E5=95=8F=E9=A1=8C=E5=AF=BE=E5=BF=9C=EF=BC=88#40=EF=BC=89=20(#4?= =?UTF-8?q?1)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit #40 #40 #40 Co-authored-by: miteruzo Reviewed-on: https://git.miteruzo.com/miteruzo/nizika_broadcast/pulls/41 --- main.py | 35 +++++++++++++++++++++++++++++------ 1 file changed, 29 insertions(+), 6 deletions(-) diff --git a/main.py b/main.py index 6d2b1f5..65dc95f 100644 --- a/main.py +++ b/main.py @@ -55,13 +55,15 @@ def main ( snack_time = SnackTime (game) CurrentTime (game, DEERJIKA_FONT) + broadcast: Broadcast | None = None try: broadcast = Broadcast (os.environ['BROADCAST_CODE']) - except Exception: - pass + except KeyError: + broadcast = None waiting_balloon = (False, '', '') last_flags_poll: float = 0 + traced_af_ids: list[int] = [] while True: now_m = time.monotonic () @@ -76,15 +78,19 @@ def main ( deerjika.talk (waiting_balloon[1], waiting_balloon[2]) waiting_balloon = (False, '', '') - if now_m - last_flags_poll >= 10: + if now_m - last_flags_poll >= 1: last_flags_poll = now_m + log (f"balloon: { balloon.enabled }, snack: { snack_time.enabled }") try: DB.begin_transaction () answer_flags = (AnsweredFlag.where ('platform', Platform.YOUTUBE.value) .where ('answered', False) + .where_not_in ('id', traced_af_ids) .get ()) + log (f"pending: { len (answer_flags) }") if answer_flags: answer_flag = random.choice (answer_flags) + log (f"flag_id: { answer_flag.id }, answer_id: { answer_flag.answer_id }") answer = Answer.find (answer_flag.answer_id) match QueryType (answer.query_rel.query_type): case QueryType.YOUTUBE_COMMENT: @@ -98,10 +104,19 @@ def main ( waiting_balloon = (True, query.content, answer.content) answer_flag.answered = True answer_flag.save () + case _: + traced_af_ids.append (answer_flag.id) DB.commit () - add_query (broadcast) except Exception as ex: DB.rollback () + log ('EXCEPTION in poll loop') + print (ex) + + try: + if broadcast is not None: + add_query (broadcast) + except Exception as ex: + log ('EXCEPTION in adding a query') print (ex) game.redraw () @@ -582,8 +597,9 @@ class Balloon (GameObject): query = self.query if CommonModule.len_by_full (query) > 21: query = CommonModule.mid_by_full (query, 0, 19.5) + '...' - answer = Surface ((375, ((CommonModule.len_by_full (self.answer) - 1) // 16 + 1) * 23.4375), - pygame.SRCALPHA) + answer = Surface ( + (375, int (((CommonModule.len_by_full (self.answer) - 1) // 16 + 1) * 23.4375)), + pygame.SRCALPHA) for i in range (int (CommonModule.len_by_full (self.answer) - 1) // 16 + 1): answer.blit (DEERJIKA_FONT.render ( CommonModule.mid_by_full (self.answer, 16 * i, 16), True, (192, 0, 0)), @@ -991,5 +1007,12 @@ def add_query ( DB.commit () +def log ( + msg: str, +) -> None: + print (f"[{ datetime.now ().isoformat (sep = ' ', timespec = 'seconds') }] { msg }", + flush = True) + + if __name__ == '__main__': main ()