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

main.py 8.8 KiB

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