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

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