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

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