· 9 years ago · Aug 20, 2016, 11:45 AM
1import os
2from io import BytesIO
3import socket
4import pyvona
5import time
6import pygame as pg
7from gtts import gTTS
8from assertions import host_addr, port_addr, twitch_user, oauth, access_key, secret_key, voice_pack, language, max_message_duration, volume, message_delay, log_file, log_enabled, ivona_enabled
9lg = language
10vol = float(volume)
11c = int(max_message_duration)
12
13
14def test_voice(music_file, volume=0.8):
15 '''
16 stream music with mixer.music module in a blocking manner
17 this will stream the sound from disk while playing
18 '''
19 # set up the mixer
20 pg.mixer.init()
21 # volume value 0.0 to 1.0
22 pg.mixer.music.set_volume(volume)
23 clock = pg.time.Clock()
24 try:
25 music_file.seek(0)
26 pg.mixer.music.load(music_file)
27 except pg.error:
28 print("File {} not found! ({})".format(music_file, pg.get_error()))
29 return
30 pg.mixer.music.play()
31
32 while pg.mixer.music.get_busy():
33 print ('yay')
34
35
36
37
38
39
40HOST = host_addr
41PORT = int(port_addr)
42NICK = twitch_user
43PASS = oauth
44v = pyvona.create_voice(access_key, secret_key)
45v.voice_name = voice_pack
46def send_message(message):
47 s.send(bytes("PRIVMSG #" + NICK + " :" + message + "\r\n", "UTF-8"))
48#start the function here for the future
49s = socket.socket()
50s.connect((HOST, PORT))
51s.send(bytes("PASS " + PASS + "\r\n", "UTF-8"))
52s.send(bytes("NICK " + NICK + "\r\n", "UTF-8"))
53s.send(bytes("JOIN #" + NICK + " \r\n", "UTF-8"))
54
55
56while True:
57 line = str(s.recv(1024))
58 if "End of /NAMES list" in line:
59 break
60
61while True:
62 for line in str(s.recv(1024)).split('\\r\\n'):
63 parts = line.split(':')
64 if len(parts) < 3:
65 continue
66
67 if "QUIT" not in parts[1] and "JOIN" not in parts[1] and "PART" not in parts[1]:
68 message = parts[2][:len(parts[2])]
69
70 usernamesplit = parts[1].split("!")
71 username = usernamesplit[0]
72 # Force delay between messages if invoked by the config
73 afix = int(message_delay)
74 time.sleep(afix)
75 print(username + ": " + message)
76 if message == "Hey":
77 send_message("Welcome!," + username)
78
79 # chat log parser: check if logging is enabled
80
81 from assertions import log_enabled
82
83 if log_enabled == "1":
84 from assertions import log_file
85 from datetime import datetime
86 timestamp = datetime.now().strftime('%Y-%m-%d %H:%M:%S')
87 # begin logging
88 # begin function here:
89 file = open(log_file, "a")
90 file.write(timestamp + " " + username + ": " + message + '\n')
91 file.close()
92
93
94 if ivona_enabled == "0":
95
96 wrapper = username + 'said:' + message
97 # give the full file path
98 # (try other sound file formats too)
99 music_file = open('temp.mp3', 'rb')
100 music_file = BytesIO()
101 # optional volume 0 to 1.0
102 volume = vol
103 tts = gTTS(text=wrapper, lang=lg)
104 tts.write_to_fp(music_file)
105 test_voice(music_file, volume)
106
107
108
109
110
111 else:
112 name = username + 'said,'
113 v.speak(name + message)