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

110 lines
3.7 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. messages: list[str] = []
  30. while (True):
  31. screen.fill ((0, 255, 0))
  32. for i in range (4):
  33. screen.blit (
  34. system_font.render (
  35. f'live_chat.is_alive () == {live_chat.is_alive ()}',
  36. True, (0, 0, 0)),
  37. (i % 2, i // 2 * 2))
  38. screen.blit (
  39. system_font.render (str (datetime.now ()), True, (0, 0, 0)),
  40. (i % 2, 32 + i // 2 * 2))
  41. if live_chat.is_alive ():
  42. screen.blit (
  43. system_font.render (
  44. f'messages == {messages}',
  45. True, (0, 0, 0)),
  46. (i % 2, 64 + i // 2 * 2))
  47. if live_chat.is_alive ():
  48. chat_items: list = live_chat.get ().items
  49. if chat_items:
  50. chat_item = random.choice (chat_items)
  51. chat_item.author = chat_item.author.__dict__
  52. message: str = chat_item.message
  53. answer: str = Talk.main (message)
  54. with open ('log.txt', 'a') as f:
  55. f.write (f'{datetime.now ()}\t{json.dumps (chat_item.__dict__)}\t{answer}\n')
  56. screen.blit (balloon, (0, 0))
  57. screen.blit (
  58. user_font.render (
  59. '> ' + (message
  60. if (CommonModule.len_by_full (message)
  61. <= 21)
  62. else (CommonModule.mid_by_full (
  63. message, 0, 19.5)
  64. + '...')), True, (0, 0, 0)),
  65. (120, 70))
  66. screen.blit (
  67. nizika_font.render (answer, True, (192, 0, 0)),
  68. (100, 150))
  69. pygame.display.update ()
  70. noon.play ()
  71. time.sleep (2)
  72. wav: bytearray | None = Aques.main (answer)
  73. if wav is not None:
  74. with open ('./nizika_talking.wav', 'wb') as f:
  75. f.write (wav)
  76. playsound ('./nizika_talking.wav')
  77. time.sleep (10)
  78. else:
  79. live_chat = pytchat.create (video_id = YOUTUBE_ID)
  80. pygame.display.update ()
  81. for event in pygame.event.get ():
  82. if event.type == QUIT:
  83. pygame.quit ()
  84. sys.exit ()
  85. if __name__ == '__main__':
  86. Main.main ()