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

main.py 5.6 KiB

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