伊地知ニジカ放送局だぬ゛ん゛. 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.

186 lines
6.6 KiB

  1. import json
  2. import random
  3. import subprocess
  4. import sys
  5. import time
  6. from datetime import datetime
  7. import emoji
  8. import pygame
  9. import pytchat
  10. from playsound import playsound
  11. from pygame.locals import *
  12. from aques import Aques
  13. from common_module import CommonModule
  14. from talk import Talk
  15. from youtube import *
  16. class Main:
  17. @classmethod
  18. def main (cls, goatoh_mode: bool = False) -> None:
  19. print (goatoh_mode)
  20. # ウィンドゥの初期化
  21. pygame.init ()
  22. screen: pygame.Surface = pygame.display.set_mode ((1024, 768))
  23. # 吹き出し
  24. balloon = pygame.transform.scale (pygame.image.load ('talking.png'),
  25. (1024, 384))
  26. if goatoh_mode:
  27. balloon = pygame.transform.flip (balloon, False, True)
  28. # 音声再生器の初期化
  29. pygame.mixer.init (frequency = 44100)
  30. # ニジカの “ぬ゛ぅ゛ぅ゛ぅ゛ん゛”
  31. noon = pygame.mixer.Sound ('noon.wav')
  32. # ゴートうの “ムムムム”
  33. mumumumu = pygame.mixer.Sound ('mumumumu.wav')
  34. # ゴートうの “クサタベテル!!”
  35. kusa = pygame.mixer.Sound ('kusa.wav')
  36. # YouTube Chat オブジェクト
  37. live_chat = pytchat.create (video_id = YOUTUBE_ID)
  38. # デバッグ・メシジのフォント
  39. system_font = pygame.font.SysFont ('notosanscjkjp', 24, bold = True)
  40. # 視聴者コメントのフォント
  41. user_font = pygame.font.SysFont ('notosanscjkjp', 32,
  42. italic = True)
  43. # ニジカのフォント
  44. nizika_font = pygame.font.SysFont ('07nikumarufont', 50)
  45. # Youtube Chat から取得したコメントたち
  46. chat_items: list = []
  47. # 会話の履歴(3 件分保持)
  48. histories: list = []
  49. while (True):
  50. screen.fill ((0, 255, 0))
  51. # 左上に時刻表示
  52. for i in range (4):
  53. screen.blit (
  54. system_font.render (str (datetime.now ()), True, (0, 0, 0)),
  55. (i % 2, i // 2 * 2))
  56. if live_chat.is_alive ():
  57. # Chat オブジェクトが有効
  58. # Chat 取得
  59. chat_items: list = live_chat.get ().items
  60. if chat_items:
  61. # 溜まってゐる Chat からランダムに 1 つ抽出
  62. chat_item = random.choice (chat_items)
  63. # 投稿者情報を辞書化
  64. chat_item.author = chat_item.author.__dict__
  65. # 絵文字を復元
  66. chat_item.message = emoji.emojize (chat_item.message)
  67. message: str = chat_item.message
  68. # ChatGPT API を呼出し,返答を取得
  69. answer: str = Talk.main (message, chat_item.author['name'], histories, goatoh_mode).replace ('\n', ' ')
  70. # 履歴に追加
  71. histories = (histories
  72. + [{'role': 'user', 'content': message},
  73. {'role': 'assistant', 'content': answer}])[(-6):]
  74. # ログ書込み
  75. with open ('log.txt', 'a') as f:
  76. f.write (f'{datetime.now ()}\t{json.dumps (chat_item.__dict__)}\t{answer}\n')
  77. # 吹出し描画(ニジカは上,ゴートうは下)
  78. screen.blit (balloon, (0, 384) if goatoh_mode else (0, 0))
  79. # 視聴者コメント描画
  80. screen.blit (
  81. user_font.render (
  82. '> ' + (message
  83. if (CommonModule.len_by_full (message)
  84. <= 21)
  85. else (CommonModule.mid_by_full (
  86. message, 0, 19.5)
  87. + '...')),
  88. True,
  89. (0, 0, 0)),
  90. (120, 70 + 384) if goatoh_mode else (120, 70))
  91. # ニジカの返答描画
  92. screen.blit (
  93. nizika_font.render (
  94. (answer
  95. if CommonModule.len_by_full (answer) <= 16
  96. else CommonModule.mid_by_full (answer, 0, 16)),
  97. True,
  98. (192, 0, 0)),
  99. (100, 150 + 384) if goatoh_mode else (100, 150))
  100. if CommonModule.len_by_full (answer) > 16:
  101. screen.blit (
  102. nizika_font.render (
  103. (CommonModule.mid_by_full (answer, 16, 16)
  104. if CommonModule.len_by_full (answer) <= 32
  105. else (CommonModule.mid_by_full (
  106. answer, 16, 14.5)
  107. + '...')),
  108. True,
  109. (192, 0, 0)),
  110. (100, 200 + 384) if goatoh_mode else (100, 200))
  111. pygame.display.update ()
  112. # 鳴く.
  113. if goatoh_mode:
  114. if random.random () < 0.1:
  115. kusa.play ()
  116. else:
  117. mumumumu.play ()
  118. else:
  119. noon.play ()
  120. time.sleep (1.5)
  121. # 返答の読上げを WAV ディタとして生成,取得
  122. try:
  123. wav: bytearray | None = Aques.main (answer, goatoh_mode)
  124. except:
  125. wav: None = None
  126. # 再生
  127. if wav is not None:
  128. with open ('./nizika_talking.wav', 'wb') as f:
  129. f.write (wav)
  130. playsound ('./nizika_talking.wav')
  131. time.sleep (10)
  132. else:
  133. # Chat オブジェクトが無効
  134. # 再生成
  135. live_chat = pytchat.create (video_id = YOUTUBE_ID)
  136. pygame.display.update ()
  137. for event in pygame.event.get ():
  138. if event.type == QUIT:
  139. pygame.quit ()
  140. sys.exit ()
  141. if __name__ == '__main__':
  142. Main.main ((len (sys.argv) > 1) and (sys.argv[1] == '-g'))