140 行
5.3 KiB
Python
140 行
5.3 KiB
Python
import json
|
|
import random
|
|
import subprocess
|
|
import sys
|
|
import time
|
|
from datetime import datetime
|
|
|
|
import emoji
|
|
import pygame
|
|
import pytchat
|
|
from playsound import playsound
|
|
from pygame.locals import *
|
|
|
|
from aques import Aques
|
|
from common_module import CommonModule
|
|
from talk import Talk
|
|
from youtube import *
|
|
|
|
|
|
class Main:
|
|
@classmethod
|
|
def main (cls, goatoh_mode: bool = False) -> None:
|
|
print (goatoh_mode)
|
|
|
|
pygame.init ()
|
|
screen: pygame.Surface = pygame.display.set_mode ((1024, 768))
|
|
balloon = pygame.transform.scale (pygame.image.load ('talking.png'),
|
|
(1024, 384))
|
|
if goatoh_mode:
|
|
balloon = pygame.transform.flip (balloon, False, True)
|
|
pygame.mixer.init (frequency = 44100)
|
|
noon = pygame.mixer.Sound ('noon.wav')
|
|
mumumumu = pygame.mixer.Sound ('mumumumu.wav')
|
|
kusa = pygame.mixer.Sound ('kusa.wav')
|
|
|
|
live_chat = pytchat.create (video_id = YOUTUBE_ID)
|
|
|
|
system_font = pygame.font.SysFont ('notosanscjkjp', 24, bold = True)
|
|
user_font = pygame.font.SysFont ('notosanscjkjp', 32,
|
|
italic = True)
|
|
nizika_font = pygame.font.SysFont ('07nikumarufont', 50)
|
|
|
|
chat_items: list = []
|
|
histories: list = []
|
|
|
|
while (True):
|
|
screen.fill ((0, 255, 0))
|
|
|
|
for i in range (4):
|
|
screen.blit (
|
|
system_font.render (str (datetime.now ()), True, (0, 0, 0)),
|
|
(i % 2, i // 2 * 2))
|
|
|
|
if live_chat.is_alive ():
|
|
chat_items: list = live_chat.get ().items
|
|
|
|
if chat_items:
|
|
chat_item = random.choice (chat_items)
|
|
chat_item.author = chat_item.author.__dict__
|
|
chat_item.message = emoji.emojize (chat_item.message)
|
|
message: str = chat_item.message
|
|
answer: str = Talk.main (message, chat_item.author['name'], histories, goatoh_mode).replace ('\n', ' ')
|
|
histories = (histories
|
|
+ [{'role': 'user', 'content': message},
|
|
{'role': 'assistant', 'content': answer}])[(-6):]
|
|
|
|
with open ('log.txt', 'a') as f:
|
|
f.write (f'{datetime.now ()}\t{json.dumps (chat_item.__dict__)}\t{answer}\n')
|
|
|
|
screen.blit (balloon, (0, 384) if goatoh_mode else (0, 0))
|
|
|
|
screen.blit (
|
|
user_font.render (
|
|
'> ' + (message
|
|
if (CommonModule.len_by_full (message)
|
|
<= 21)
|
|
else (CommonModule.mid_by_full (
|
|
message, 0, 19.5)
|
|
+ '...')),
|
|
True,
|
|
(0, 0, 0)),
|
|
(120, 70 + 384) if goatoh_mode else (120, 70))
|
|
screen.blit (
|
|
nizika_font.render (
|
|
(answer
|
|
if CommonModule.len_by_full (answer) <= 16
|
|
else CommonModule.mid_by_full (answer, 0, 16)),
|
|
True,
|
|
(192, 0, 0)),
|
|
(100, 150 + 384) if goatoh_mode else (100, 150))
|
|
if CommonModule.len_by_full (answer) > 16:
|
|
screen.blit (
|
|
nizika_font.render (
|
|
(CommonModule.mid_by_full (answer, 16, 16)
|
|
if CommonModule.len_by_full (answer) <= 32
|
|
else (CommonModule.mid_by_full (
|
|
answer, 16, 14.5)
|
|
+ '...')),
|
|
True,
|
|
(192, 0, 0)),
|
|
(100, 200 + 384) if goatoh_mode else (100, 200))
|
|
|
|
pygame.display.update ()
|
|
|
|
if goatoh_mode:
|
|
if random.random () < 0.1:
|
|
kusa.play ()
|
|
else:
|
|
mumumumu.play ()
|
|
else:
|
|
noon.play ()
|
|
|
|
time.sleep (1.5)
|
|
|
|
try:
|
|
wav: bytearray | None = Aques.main (answer, goatoh_mode)
|
|
except:
|
|
wav: None = None
|
|
if wav is not None:
|
|
with open ('./nizika_talking.wav', 'wb') as f:
|
|
f.write (wav)
|
|
|
|
playsound ('./nizika_talking.wav')
|
|
|
|
time.sleep (10)
|
|
else:
|
|
live_chat = pytchat.create (video_id = YOUTUBE_ID)
|
|
|
|
pygame.display.update ()
|
|
|
|
for event in pygame.event.get ():
|
|
if event.type == QUIT:
|
|
pygame.quit ()
|
|
sys.exit ()
|
|
|
|
|
|
if __name__ == '__main__':
|
|
Main.main ((len (sys.argv) > 1) and (sys.argv[1] == '-g'))
|
|
|