伊地知ニジカ放送局だぬ゛ん゛. https://www.youtube.com/@deerjika
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.

922 lines
25 KiB

  1. from __future__ import annotations
  2. import math
  3. import os
  4. import random
  5. import sys
  6. import wave
  7. from datetime import datetime, timedelta
  8. from enum import Enum, auto
  9. from io import BytesIO
  10. from typing import Callable, TypedDict
  11. import cv2
  12. import emoji
  13. import ephem
  14. import numpy as np
  15. import pygame
  16. import pygame.gfxdraw
  17. import pytchat
  18. import requests
  19. from cv2 import VideoCapture
  20. from ephem import Moon, Observer, Sun
  21. from pygame import Rect, Surface
  22. from pygame.font import Font
  23. from pygame.mixer import Sound
  24. from pygame.time import Clock
  25. from pytchat.core.pytchat import PytchatCore
  26. from pytchat.processors.default.processor import Chat
  27. from aques import Aques
  28. from common_module import CommonModule
  29. from nizika_ai.config import DB
  30. from nizika_ai.consts import (AnswerType, Character, GPTModel, Platform,
  31. QueryType)
  32. from nizika_ai.models import Answer, AnsweredFlag, Query, User
  33. pygame.init ()
  34. FPS = 30
  35. SYSTEM_FONT = pygame.font.SysFont ('notosanscjkjp', 24, bold = True)
  36. USER_FONT = pygame.font.SysFont ('notosanscjkjp', 32, italic = True)
  37. DEERJIKA_FONT = pygame.font.SysFont ('07nikumarufont', 50)
  38. def main (
  39. ) -> None:
  40. game = Game ()
  41. Bg (game)
  42. balloon = Balloon (game)
  43. deerjika = Deerjika (game, DeerjikaPattern.RELAXED,
  44. x = CWindow.WIDTH * 3 / 4,
  45. y = CWindow.HEIGHT - 120,
  46. balloon = balloon)
  47. CurrentTime (game, SYSTEM_FONT)
  48. try:
  49. broadcast = Broadcast (os.environ['BROADCAST_CODE'])
  50. except Exception:
  51. pass
  52. while True:
  53. for event in pygame.event.get ():
  54. if event.type == pygame.QUIT:
  55. pygame.quit ()
  56. sys.exit ()
  57. if not balloon.enabled:
  58. try:
  59. DB.begin_transaction ()
  60. answer_flags = (AnsweredFlag.where ('platform', Platform.YOUTUBE.value)
  61. .where ('answered', False)
  62. .get ())
  63. if answer_flags:
  64. answer_flag = random.choice (answer_flags)
  65. answer = Answer.find (answer_flag.answer_id)
  66. if answer.answer_type == AnswerType.YOUTUBE_REPLY.value:
  67. query = Query.find (answer.query_id)
  68. deerjika.talk (query.content, answer.content)
  69. answer_flag.answered = True
  70. answer_flag.save ()
  71. DB.commit ()
  72. add_query (broadcast)
  73. except Exception:
  74. pass
  75. game.redraw ()
  76. class Bg:
  77. """
  78. 背景オブゼクト管理用クラス
  79. Attributes:
  80. base (BgBase): 最背面
  81. grass (BgGrass): 草原部分
  82. jojoko (Jojoko): 大月ヨヨコ
  83. kita (KitaSun): き太く陽
  84. """
  85. base: BgBase
  86. grass: BgGrass
  87. jojoko: Jojoko
  88. kita: KitaSun
  89. def __init__ (
  90. self,
  91. game: Game,
  92. ):
  93. self.base = BgBase (game)
  94. self.jojoko = Jojoko (game)
  95. self.kita = KitaSun (game)
  96. self.grass = BgGrass (game)
  97. class DeerjikaPattern (Enum):
  98. """
  99. ニジカの状態
  100. Members:
  101. NORMAL: 通常
  102. RELAXED: 足パタパタ
  103. SLEEPING: 寝ニジカ
  104. DANCING: ダンシング・ニジカ
  105. """
  106. NORMAL = auto ()
  107. RELAXED = auto ()
  108. SLEEPING = auto ()
  109. DANCING = auto ()
  110. class Direction (Enum):
  111. """
  112. クリーチャの向き
  113. Members:
  114. LEFT: 左向き
  115. RIGHT: 右向き
  116. """
  117. LEFT = auto ()
  118. RIGHT = auto ()
  119. class Game:
  120. """
  121. ゲーム・クラス
  122. Attributes:
  123. clock (Clock): Clock オブゼクト
  124. frame (int): フレーム・カウンタ
  125. last_answered_at (datetime): 最後に回答した時刻
  126. now (datetime): 基準日時
  127. objects (list[GameObject]): 再描画するクラスのリスト
  128. screen (Surface): 基底スクリーン
  129. sky (Sky): 天体情報
  130. """
  131. bgm: Sound
  132. clock: Clock
  133. fps: float
  134. frame: int
  135. last_answered_at: datetime
  136. now: datetime
  137. objects: list[GameObject]
  138. screen: Surface
  139. sky: Sky
  140. def __init__ (
  141. self,
  142. ):
  143. self.now = datetime.now ()
  144. self.screen = pygame.display.set_mode ((CWindow.WIDTH, CWindow.HEIGHT))
  145. self.clock = Clock ()
  146. self.fps = FPS
  147. self.frame = 0
  148. self.objects = []
  149. self.bgm = Sound ('assets/bgm.mp3')
  150. self.bgm.set_volume (.15)
  151. self.bgm.play (loops = -1)
  152. self._create_sky ()
  153. def redraw (
  154. self,
  155. ) -> None:
  156. self.now = datetime.now ()
  157. self.sky.observer.date = self.now - timedelta (hours = 9)
  158. for obj in sorted (self.objects, key = lambda obj: obj.layer):
  159. if obj.enabled:
  160. obj.redraw ()
  161. pygame.display.update ()
  162. delta_time = self.clock.tick (FPS) / 1000
  163. self.fps = 1 / delta_time
  164. if delta_time > 1 / FPS:
  165. for _ in range (int (FPS * delta_time) - 1):
  166. for obj in self.objects:
  167. if obj.enabled:
  168. obj.update ()
  169. def _create_sky (
  170. self,
  171. ) -> None:
  172. self.sky = Sky ()
  173. self.sky.observer = Observer ()
  174. self.sky.observer.lat = '35'
  175. self.sky.observer.lon = '139'
  176. class GameObject:
  177. """
  178. 各ゲーム・オブゼクトの基底クラス
  179. Attributes:
  180. arg (float): 回転角度 (rad)
  181. ax (float): X 軸に対する加速度 (px/frame^2)
  182. ay (float): y 軸に対する加速度 (px/frame^2)
  183. enabled (bool): オブゼクトの表示可否
  184. frame (int): フレーム・カウンタ
  185. game (Game): ゲーム基盤
  186. height (int): 高さ (px)
  187. vx (float): x 軸に対する速度 (px/frame)
  188. vy (float): y 軸に対する速度 (px/frame)
  189. width (int): 幅 (px)
  190. x (float): X 座標 (px)
  191. y (float): Y 座標 (px)
  192. """
  193. arg: float = 0
  194. ax: float = 0
  195. ay: float = 0
  196. enabled: bool = True
  197. frame: int
  198. game: Game
  199. height: int
  200. layer: float
  201. vx: float = 0
  202. vy: float = 0
  203. width: int
  204. x: float
  205. y: float
  206. def __init__ (
  207. self,
  208. game: Game,
  209. layer: float | None = None,
  210. enabled: bool = True,
  211. x: float = 0,
  212. y: float = 0,
  213. ):
  214. self.game = game
  215. self.enabled = enabled
  216. self.frame = 0
  217. if layer is None:
  218. if self.game.objects:
  219. layer = max (obj.layer for obj in self.game.objects) + 10
  220. else:
  221. layer = 0
  222. self.layer = layer
  223. self.x = x
  224. self.y = y
  225. self.game.objects.append (self)
  226. def redraw (
  227. self,
  228. ) -> None:
  229. self.update ()
  230. def update (
  231. self,
  232. ) -> None:
  233. self.x += self.vx
  234. self.y += self.vy
  235. self.vx += self.ax
  236. self.vy += self.ay
  237. self.frame += 1
  238. class BgBase (GameObject):
  239. """
  240. 背景
  241. Attributes:
  242. surface (Surface): 背景 Surface
  243. """
  244. bg: Surface
  245. bg_evening: Surface
  246. bg_grass: Surface
  247. bg_night: Surface
  248. surface: Surface
  249. def __init__ (
  250. self,
  251. game: Game,
  252. ):
  253. super ().__init__ (game)
  254. self.bg = pygame.image.load ('assets/bg.jpg')
  255. self.bg_evening = pygame.image.load ('assets/bg-evening.jpg')
  256. self.bg_grass = pygame.image.load ('assets/bg-grass.png')
  257. self.bg_night = pygame.image.load ('assets/bg-night.jpg')
  258. self.surface = pygame.transform.scale (self.bg, (CWindow.WIDTH, CWindow.HEIGHT))
  259. def redraw (
  260. self,
  261. ) -> None:
  262. self.game.screen.blit (self.surface, (self.x, self.y))
  263. super ().redraw ()
  264. class BgGrass (GameObject):
  265. """
  266. 背景の草原部分
  267. Attributes:
  268. surface (Surface): 草原 Surface
  269. """
  270. surface: Surface
  271. def __init__ (
  272. self,
  273. game: Game,
  274. ):
  275. super ().__init__ (game)
  276. self.game = game
  277. self.surface = pygame.image.load ('assets/bg-grass.png')
  278. self.surface = pygame.transform.scale (self.surface, (CWindow.WIDTH, CWindow.HEIGHT))
  279. def redraw (
  280. self,
  281. ) -> None:
  282. self.game.screen.blit (self.surface, (self.x, self.y))
  283. super ().redraw ()
  284. class Creature (GameObject):
  285. sound: Sound
  286. def bell (
  287. self,
  288. ) -> None:
  289. self.sound.play ()
  290. class Deerjika (Creature):
  291. """
  292. 伊地知ニジカ
  293. Attributes:
  294. height (int): 高さ (px)
  295. scale (float): 拡大率
  296. surfaces (list[Surface]): ニジカの各フレームを Surface にしたリスト
  297. width (int): 幅 (px)
  298. """
  299. FPS = 30
  300. height: int
  301. scale: float = .8
  302. surfaces: list[Surface]
  303. width: int
  304. talking: bool = False
  305. wav: bytearray | None = None
  306. balloon: Balloon
  307. def __init__ (
  308. self,
  309. game: Game,
  310. pattern: DeerjikaPattern = DeerjikaPattern.NORMAL,
  311. direction: Direction = Direction.LEFT,
  312. layer: float | None = None,
  313. x: float = 0,
  314. y: float = 0,
  315. balloon: Balloon | None = None,
  316. ):
  317. if balloon is None:
  318. raise Exception
  319. super ().__init__ (game, layer, x = x, y = y)
  320. self.pattern = pattern
  321. self.direction = direction
  322. self.balloon = balloon
  323. match pattern:
  324. case DeerjikaPattern.NORMAL:
  325. ...
  326. case DeerjikaPattern.RELAXED:
  327. match direction:
  328. case Direction.LEFT:
  329. self.width = 1280
  330. self.height = 720
  331. surface = pygame.image.load ('assets/deerjika_relax_left.png')
  332. self.surfaces = []
  333. for x in range (0, surface.get_width (), self.width):
  334. self.surfaces.append (
  335. surface.subsurface (x, 0, self.width, self.height))
  336. case Direction.RIGHT:
  337. ...
  338. self.sound = Sound ('assets/noon.wav')
  339. def redraw (
  340. self,
  341. ) -> None:
  342. surface = pygame.transform.scale (self.surfaces[self.frame * self.FPS // FPS
  343. % len (self.surfaces)],
  344. (self.width * self.scale, self.height * self.scale))
  345. self.game.screen.blit (surface, surface.get_rect (center = (self.x, self.y)))
  346. super ().redraw ()
  347. def update (
  348. self,
  349. ) -> None:
  350. if (not self.balloon.enabled) and self.talking:
  351. self.talking = False
  352. if (self.balloon.enabled and self.balloon.frame >= FPS * 1.5
  353. and not self.talking):
  354. self.read_out ()
  355. super ().update ()
  356. def talk (
  357. self,
  358. query: str,
  359. answer: str,
  360. ) -> None:
  361. self.bell ()
  362. self._create_wav (answer)
  363. length = 300
  364. if self.wav is not None:
  365. with wave.open ('./nizika_talking.wav', 'rb') as f:
  366. length = int (FPS * (f.getnframes () / f.getframerate () + 4))
  367. self.balloon.talk (query, answer, length = length)
  368. def read_out (
  369. self,
  370. ) -> None:
  371. Sound ('./nizika_talking.wav').play ()
  372. self.talking = True
  373. def _create_wav (
  374. self,
  375. message: str,
  376. ) -> None:
  377. try:
  378. self.wav = Aques.main (message, False)
  379. except:
  380. self.wav = None
  381. if self.wav is None:
  382. return
  383. with open ('./nizika_talking.wav', 'wb') as f:
  384. f.write (self.wav)
  385. class CurrentTime (GameObject):
  386. """
  387. 現在日時表示
  388. Attributes:
  389. font (Font): フォント
  390. """
  391. font: Font
  392. def __init__ (
  393. self,
  394. game: Game,
  395. font: Font,
  396. ):
  397. super ().__init__ (game)
  398. self.font = font
  399. def redraw (
  400. self,
  401. ) -> None:
  402. for i in range (4):
  403. self.game.screen.blit (
  404. self.font.render (f"{ self.game.now } { self.game.fps } fps", True, (0, 0, 0)),
  405. (i % 2, i // 2 * 2))
  406. super ().redraw ()
  407. class Balloon (GameObject):
  408. """
  409. 吹出し
  410. Attributes:
  411. answer (str): 回答テキスト
  412. image_url (str, None): 画像 URL
  413. length (int): 表示する時間 (frame)
  414. query (str): 質問テキスト
  415. surface (Surface): 吹出し Surface
  416. x_flip (bool): 左右反転フラグ
  417. y_flip (bool): 上下反転フラグ
  418. """
  419. answer: str = ''
  420. image_url: str | None = None
  421. length: int = 300
  422. query: str = ''
  423. surface: Surface
  424. x_flip: bool = False
  425. y_flip: bool = False
  426. def __init__ (
  427. self,
  428. game: Game,
  429. x_flip: bool = False,
  430. y_flip: bool = False,
  431. ):
  432. super ().__init__ (game, enabled = False)
  433. self.x_flip = x_flip
  434. self.y_flip = y_flip
  435. self.surface = pygame.transform.scale (pygame.image.load ('assets/balloon.png'),
  436. (CWindow.WIDTH, CWindow.HEIGHT / 2))
  437. self.surface = pygame.transform.flip (self.surface, self.x_flip, self.y_flip)
  438. def redraw (
  439. self,
  440. ) -> None:
  441. if self.frame >= self.length:
  442. self.enabled = False
  443. self.game.last_answered_at = self.game.now
  444. return
  445. query = self.query
  446. if CommonModule.len_by_full (query) > 21:
  447. query = CommonModule.mid_by_full (query, 0, 19.5) + '...'
  448. answer = Surface ((800, ((CommonModule.len_by_full (self.answer) - 1) // 16 + 1) * 50),
  449. pygame.SRCALPHA)
  450. for i in range (int (CommonModule.len_by_full (self.answer) - 1) // 16 + 1):
  451. answer.blit (DEERJIKA_FONT.render (
  452. CommonModule.mid_by_full (self.answer, 16 * i, 16), True, (192, 0, 0)),
  453. (0, 50 * i))
  454. surface = self.surface.copy ()
  455. surface.blit (USER_FONT.render ('>' + query, True, (0, 0, 0)), (120, 70))
  456. y: int
  457. if self.frame < 30:
  458. y = 0
  459. elif self.frame >= self.length - 90:
  460. y = answer.get_height () - 100
  461. else:
  462. y = int ((answer.get_height () - 100) * (self.frame - 30) / (self.length - 120))
  463. surface.blit (answer, (100, 150), Rect (0, y, 800, 100))
  464. self.game.screen.blit (surface, (0, 0))
  465. super ().redraw ()
  466. def talk (
  467. self,
  468. query: str,
  469. answer: str,
  470. image_url: str | None = None,
  471. length: int = 300,
  472. ) -> None:
  473. self.query = query
  474. self.answer = answer
  475. self.image_url = image_url
  476. self.length = length
  477. self.frame = 0
  478. self.enabled = True
  479. class KitaSun (GameObject):
  480. """
  481. き太く陽
  482. Attributes:
  483. sun (Sun): ephem の太陽オブゼクト
  484. surface (Surface): き太く陽 Surface
  485. """
  486. alt: float
  487. az: float
  488. sun: Sun
  489. surface: Surface
  490. def __init__ (
  491. self,
  492. game: Game,
  493. ):
  494. super ().__init__ (game)
  495. self.surface = pygame.transform.scale (pygame.image.load ('assets/sun.png'), (200, 200))
  496. self.sun = Sun ()
  497. def redraw (
  498. self,
  499. ) -> None:
  500. surface = pygame.transform.rotate (self.surface, -(90 + math.degrees (self.arg)))
  501. self.game.screen.blit (surface, surface.get_rect (center = (self.x, self.y)))
  502. super ().redraw ()
  503. def update (
  504. self,
  505. ) -> None:
  506. self.sun.compute (self.game.sky.observer)
  507. self.alt = self.sun.alt
  508. self.az = self.sun.az
  509. if abs (self.new_arg - self.arg) > math.radians (15):
  510. self.arg = self.new_arg
  511. self.x = self.new_x
  512. self.y = self.new_y
  513. super ().update ()
  514. @property
  515. def new_x (
  516. self,
  517. ) -> float:
  518. return CWindow.WIDTH * (math.degrees (self.az) - 80) / 120
  519. @property
  520. def new_y (
  521. self,
  522. ) -> float:
  523. return ((CWindow.HEIGHT / 2)
  524. - ((CWindow.HEIGHT / 2 + 100) * math.sin (self.alt)
  525. / math.sin (math.radians (60))))
  526. @property
  527. def new_arg (
  528. self,
  529. ) -> float:
  530. return math.atan2 (self.new_y - self.y, self.new_x - self.x)
  531. class Jojoko (GameObject):
  532. """
  533. 大月ヨヨコ
  534. Attributes:
  535. base (Surface): 満月ヨヨコ Surface
  536. moon (Moon): ephem の月オブゼクト
  537. surface (Surface): 缺けたヨヨコ
  538. """
  539. alt: float
  540. az: float
  541. base: Surface
  542. moon: Moon
  543. surface: Surface
  544. def __init__ (
  545. self,
  546. game: Game,
  547. ):
  548. super ().__init__ (game)
  549. self.base = pygame.transform.scale (pygame.image.load ('assets/moon.png'), (200, 200))
  550. self.moon = Moon ()
  551. self.surface = self._get_surface ()
  552. def redraw (
  553. self,
  554. ) -> None:
  555. self.moon.compute (self.game.sky.observer)
  556. self.alt = self.moon.alt
  557. self.az = self.moon.az
  558. if abs (self.new_arg - self.arg) > math.radians (15):
  559. self.arg = self.new_arg
  560. self.x = self.new_x
  561. self.y = self.new_y
  562. if self.frame % (FPS * 3600) == 0:
  563. self.surface = self._get_surface ()
  564. surface = pygame.transform.rotate (self.surface, -(90 + math.degrees (self.arg)))
  565. surface.set_colorkey ((0, 255, 0))
  566. self.game.screen.blit (surface, surface.get_rect (center = (self.x, self.y)))
  567. super ().redraw ()
  568. @property
  569. def phase (
  570. self,
  571. ) -> float:
  572. dt: datetime = ephem.localtime (ephem.previous_new_moon (self.game.sky.observer.date))
  573. return (self.game.now - dt).total_seconds () / 60 / 60 / 24
  574. def _get_surface (
  575. self,
  576. ) -> Surface:
  577. """
  578. ヨヨコを月齢に応じて缺かす.
  579. Returns:
  580. Surface: 缺けたヨヨコ
  581. """
  582. jojoko = self.base.copy ()
  583. for i in range (200):
  584. if 1 <= self.phase < 15:
  585. pygame.gfxdraw.bezier (jojoko, ((0, 100 + i), (100, 180 * self.phase / 7 - 80 + i), (200, 100 + i)), 3, (0, 255, 0))
  586. elif self.phase < 16:
  587. pass
  588. elif self.phase < 30:
  589. pygame.gfxdraw.bezier (jojoko, ((0, 100 - i), (100, 180 * (self.phase - 15) / 7 - 80 - i), (200, 100 - i)), 3, (0, 255, 0))
  590. else:
  591. jojoko.fill ((0, 255, 0))
  592. return jojoko
  593. @property
  594. def new_x (
  595. self,
  596. ) -> float:
  597. return CWindow.WIDTH * (math.degrees (self.az) - 80) / 120
  598. @property
  599. def new_y (
  600. self,
  601. ) -> float:
  602. return ((CWindow.HEIGHT / 2)
  603. - ((CWindow.HEIGHT / 2 + 100) * math.sin (self.alt)
  604. / math.sin (math.radians (60))))
  605. @property
  606. def new_arg (
  607. self,
  608. ) -> float:
  609. return math.atan2 (self.new_y - self.y, self.new_x - self.x)
  610. class Sky:
  611. """
  612. 天体に関する情報を保持するクラス
  613. Attributes:
  614. observer (Observer): 観測値
  615. """
  616. observer: Observer
  617. class CWindow:
  618. """
  619. ウィンドゥに関する定数クラス
  620. Attributes:
  621. WIDTH (int): ウィンドゥ幅
  622. HEIGHT (int): ウィンドゥ高さ
  623. """
  624. WIDTH = 1024
  625. HEIGHT = 768
  626. class Broadcast:
  627. chat: PytchatCore
  628. code: str
  629. def __init__ (
  630. self,
  631. broadcast_code,
  632. ):
  633. self.code = broadcast_code
  634. self.chat = pytchat.create (self.code)
  635. def fetch_chat (
  636. self,
  637. ) -> Chat | None:
  638. if not self.chat.is_alive ():
  639. self.chat = pytchat.create (self.code)
  640. return None
  641. chats = self.chat.get ().items
  642. if not chats:
  643. return None
  644. return random.choice (chats)
  645. class Video (GameObject):
  646. fps: int
  647. pausing: bool = False
  648. sound: Sound | None
  649. surfaces: list[Surface]
  650. def __init__ (
  651. self,
  652. game: Game,
  653. path: str,
  654. ):
  655. super ().__init__ (game)
  656. self.pausing = False
  657. (self.surfaces, self.fps) = self._create_surfaces (path)
  658. self.sound = self._create_sound (path)
  659. self.stop ()
  660. def _create_sound (
  661. self,
  662. path: str,
  663. ) -> Sound | None:
  664. bytes_io = BytesIO ()
  665. try:
  666. from pydub import AudioSegment
  667. audio = AudioSegment.from_file (path, format = path.split ('.')[-1])
  668. except ModuleNotFoundError:
  669. return None
  670. audio.export (bytes_io, format = 'wav')
  671. bytes_io.seek (0)
  672. return pygame.mixer.Sound (bytes_io)
  673. def _create_surfaces (
  674. self,
  675. path: str,
  676. ) -> tuple[list[Surface], int]:
  677. cap = self._load (path)
  678. surfaces: list[Surface] = []
  679. if cap is None:
  680. return ([], FPS)
  681. fps = int (cap.get (cv2.CAP_PROP_FPS))
  682. while cap.isOpened ():
  683. frame = self._read_frame (cap)
  684. if frame is None:
  685. break
  686. surfaces.append (self._convert_to_surface (frame))
  687. new_surfaces: list[Surface] = []
  688. for i in range (len (surfaces) * FPS // fps):
  689. new_surfaces.append (surfaces[i * fps // FPS])
  690. return (new_surfaces, fps)
  691. def _load (
  692. self,
  693. path: str,
  694. ) -> VideoCapture | None:
  695. """
  696. OpenCV で動画を読込む.
  697. """
  698. cap = VideoCapture (path)
  699. if cap.isOpened ():
  700. return cap
  701. return None
  702. def _read_frame (
  703. self,
  704. cap: VideoCapture,
  705. ) -> np.ndarray | None:
  706. """
  707. 動画のフレームを読込む.
  708. """
  709. ret: bool
  710. frame: np.ndarray
  711. (ret, frame) = cap.read ()
  712. if ret:
  713. return frame
  714. return None
  715. def _convert_to_surface (
  716. self,
  717. frame: np.ndarray,
  718. ) -> Surface:
  719. frame = cv2.cvtColor (frame, cv2.COLOR_BGR2RGB)
  720. frame_surface = pygame.surfarray.make_surface (frame)
  721. frame_surface = pygame.transform.rotate (frame_surface, -90)
  722. frame_surface = pygame.transform.flip (frame_surface, True, False)
  723. return frame_surface
  724. def play (
  725. self,
  726. ) -> None:
  727. self.enabled = True
  728. self.pausing = False
  729. if self.sound is not None:
  730. self.sound.play ()
  731. def stop (
  732. self,
  733. ) -> None:
  734. self.enabled = False
  735. self.frame = 0
  736. def pause (
  737. self,
  738. ) -> None:
  739. self.pausing = True
  740. def redraw (
  741. self,
  742. ) -> None:
  743. self.game.screen.blit (self.surfaces[self.frame], (self.x, self.y))
  744. super ().redraw ()
  745. def update (
  746. self,
  747. ) -> None:
  748. if self.frame >= len (self.surfaces) - 1:
  749. self.pause ()
  750. if self.pausing:
  751. self.frame -= 1
  752. super ().update ()
  753. class NicoVideo (Video):
  754. ...
  755. def fetch_bytes_from_url (
  756. url: str,
  757. ) -> bytes | None:
  758. res = requests.get (url, timeout = 60)
  759. if res.status_code != 200:
  760. return None
  761. return res.content
  762. def add_query (
  763. broadcast: Broadcast,
  764. ) -> None:
  765. chat = broadcast.fetch_chat ()
  766. if chat is None:
  767. return
  768. DB.begin_transaction ()
  769. chat.message = emoji.emojize (chat.message)
  770. message: str = chat.message
  771. user = (User.where ('platform', Platform.YOUTUBE.value)
  772. .where ('code', chat.author.channelId)
  773. .first ())
  774. if user is None:
  775. user = User ()
  776. user.platform = Platform.YOUTUBE.value
  777. user.code = chat.author.channelId
  778. user.name = chat.author.name
  779. user.icon = fetch_bytes_from_url (chat.author.imageUrl)
  780. user.save ()
  781. query = Query ()
  782. query.user_id = user.id
  783. query.target_character = Character.DEERJIKA.value
  784. query.content = chat.message
  785. query.query_type = QueryType.YOUTUBE_COMMENT.value
  786. query.model = GPTModel.GPT3_TURBO.value
  787. query.sent_at = datetime.now ()
  788. query.answered = False
  789. query.save ()
  790. DB.commit ()
  791. if __name__ == '__main__':
  792. main ()