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