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

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