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

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