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

135 lines
5.0 KiB

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