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