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

152 lines
5.5 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 mode import Mode
  15. from talk import Talk
  16. from youtube import *
  17. class Main:
  18. @classmethod
  19. def main (cls, argv: list, argc: int) -> None:
  20. mode = Mode.NIZIKA
  21. match (argc > 0) and argv[0]:
  22. case '-g':
  23. mode = Mode.GOATOH
  24. case '-w':
  25. mode = Mode.DOUBLE
  26. goatoh_mode = mode == Mode.GOATOH
  27. double_mode = mode == Mode.DOUBLE
  28. print (mode)
  29. pygame.init ()
  30. screen: pygame.Surface = pygame.display.set_mode ((1024, 768))
  31. balloon = pygame.transform.scale (pygame.image.load ('talking.png'),
  32. (1024, 384))
  33. if goatoh_mode:
  34. balloon = pygame.transform.flip (balloon, False, True)
  35. pygame.mixer.init (frequency = 44100)
  36. noon = pygame.mixer.Sound ('noon.wav')
  37. mumumumu = pygame.mixer.Sound ('mumumumu.wav')
  38. kusa = pygame.mixer.Sound ('kusa.wav')
  39. live_chat = pytchat.create (video_id = YOUTUBE_ID)
  40. system_font = pygame.font.SysFont ('notosanscjkjp', 24, bold = True)
  41. user_font = pygame.font.SysFont ('notosanscjkjp', 32, italic = True)
  42. nizika_font = pygame.font.SysFont ('07nikumarufont', 50)
  43. chat_items: list = []
  44. histories: list = []
  45. while (True):
  46. screen.fill ((0, 255, 0))
  47. for i in range (4):
  48. screen.blit (
  49. system_font.render (str (datetime.now ()), True, (0, 0, 0)),
  50. (i % 2, i // 2 * 2))
  51. if live_chat.is_alive ():
  52. chat_items: list = live_chat.get ().items
  53. if chat_items:
  54. chat_item = random.choice (chat_items)
  55. chat_item.author = chat_item.author.__dict__
  56. chat_item.message = emoji.emojize (chat_item.message)
  57. message: str = chat_item.message
  58. answer: str = Talk.main (message, chat_item.author['name'], histories, goatoh_mode).replace ('\n', ' ')
  59. histories = (histories
  60. + [{'role': 'user', 'content': message},
  61. {'role': 'assistant', 'content': answer}])[(-6):]
  62. with open ('log.txt', 'a') as f:
  63. f.write (f'{datetime.now ()}\t{json.dumps (chat_item.__dict__)}\t{answer}\n')
  64. screen.blit (balloon, (0, 384) if goatoh_mode else (0, 0))
  65. screen.blit (
  66. user_font.render (
  67. '> ' + (message
  68. if (CommonModule.len_by_full (message)
  69. <= 21)
  70. else (CommonModule.mid_by_full (
  71. message, 0, 19.5)
  72. + '...')),
  73. True,
  74. (0, 0, 0)),
  75. (120, 70 + 384) if goatoh_mode else (120, 70))
  76. screen.blit (
  77. nizika_font.render (
  78. (answer
  79. if CommonModule.len_by_full (answer) <= 16
  80. else CommonModule.mid_by_full (answer, 0, 16)),
  81. True,
  82. (192, 0, 0)),
  83. (100, 150 + 384) if goatoh_mode else (100, 150))
  84. if CommonModule.len_by_full (answer) > 16:
  85. screen.blit (
  86. nizika_font.render (
  87. (CommonModule.mid_by_full (answer, 16, 16)
  88. if CommonModule.len_by_full (answer) <= 32
  89. else (CommonModule.mid_by_full (
  90. answer, 16, 14.5)
  91. + '...')),
  92. True,
  93. (192, 0, 0)),
  94. (100, 200 + 384) if goatoh_mode else (100, 200))
  95. pygame.display.update ()
  96. if goatoh_mode:
  97. if random.random () < 0.1:
  98. kusa.play ()
  99. else:
  100. mumumumu.play ()
  101. else:
  102. noon.play ()
  103. time.sleep (1.5)
  104. try:
  105. wav: bytearray | None = Aques.main (answer, goatoh_mode)
  106. except:
  107. wav: None = None
  108. if wav is not None:
  109. with open ('./nizika_talking.wav', 'wb') as f:
  110. f.write (wav)
  111. playsound ('./nizika_talking.wav')
  112. time.sleep (10)
  113. else:
  114. live_chat = pytchat.create (video_id = YOUTUBE_ID)
  115. pygame.display.update ()
  116. for event in pygame.event.get ():
  117. if event.type == QUIT:
  118. pygame.quit ()
  119. sys.exit ()
  120. if __name__ == '__main__':
  121. # Main.main ((len (sys.argv) > 1) and (sys.argv[1] == '-g'))
  122. Main.main (sys.argv, len (sys.argv))