· 9 years ago · Sep 04, 2016, 11:36 AM
1import os
2import re
3from io import BytesIO
4import socket
5import pyvona
6import time
7import pygame as pg
8from gtts import gTTS
9from datetime import datetime
10from assertions import host_addr, port_addr, twitch_user, oauth, access_key, secret_key, voice_pack, language, max_message_duration, \
11volume, message_delay, log_file, log_enabled, ivona_enabled
12lg = language
13vol = float(volume)
14c = int(max_message_duration)
15
16#----------------------------------------------MUSIC PLAYER FOR GTTS------------------------------------------------------------------#
17def test_voice(music_file, volume=0.8):
18 '''
19 stream music with mixer.music module in a blocking manner
20 this will stream the sound from disk while playing
21 '''
22 # set up the mixer
23 pg.mixer.init()
24 # volume value 0.0 to 1.0
25 pg.mixer.music.set_volume(volume)
26 clock = pg.time.Clock()
27 try:
28 music_file.seek(0)
29 pg.mixer.music.load(music_file)
30 except pg.error:
31 print("File {} not found! ({})".format(music_file, pg.get_error()))
32 return
33 pg.mixer.music.play()
34
35 while pg.mixer.music.get_busy():
36 print ('yay')
37
38#------------------------------------------------------END------------------------------------------------------------------------------#
39
40
41#-----------------------------------------------------FUNCTIONS-------------------------------------------------------------------------#
42
43host = host_addr
44port = int(port_addr)
45nickname = twitch_user
46password = oauth
47server = twitch_user
48
49class chatmodule():
50
51
52 def twitch_login(self, nickname, password, server):
53 s = socket.socket()
54 s.connect((host, port))
55 s.send(bytes("NICK " + self.nickname + "\r\n", "UTF-8"))
56 s.send(bytes("PASS " + self.password + "\r\n", "UTF-8"))
57 s.send(bytes("JOIN #" + self.server + " \r\n", "UTF-8"))
58
59 def send_pong(msg):
60 con.send(bytes('PONG %s\r\n' % msg, 'UTF-8'))
61
62
63
64
65class voice_engine():
66
67 def contextuality(self):
68
69 if message.endswith('?'):
70 sender =+ 'asked,'
71 elif message.endswith('!'):
72 sender =+ 'shouted,'
73 else:
74 sender =+ 'said,'
75
76 def ivona_engine(self):
77
78 v = pyvona.create_voice(access_key, secret_key)
79 v.voice_name = voice_pack
80
81 v.speak(sender + self.message)
82
83 def picker(self):
84 if ivona_enabled == "1":
85 engine = ivona_engine()
86 else:
87 print ("Under construction!")
88
89
90
91p = chatmodule()
92p.twitch_login(nickname, password, server)
93
94data = ""
95
96while True:
97 try:
98 data = data + con.recv(1024).decode('UTF-8')
99 data_split = re.split(r"[~\r\n]+", data)
100 data = data_split.pop()
101
102 for line in data_split:
103 line = str.rstrip(line)
104 line = str.split(line)
105
106 if len(line) >= 1:
107 if line[0] == 'PING':
108 send_pong(line[1])
109
110 if line[1] == 'PRIVMSG':
111 sender = get_sender(line[0])
112 message = get_message(line)
113 parse_message(message)
114 afix = int(message_delay)
115 time.sleep(afix)
116 print(sender + ": " + message)
117
118 except socket.error:
119 print("Socket died")
120
121 except socket.timeout:
122 print("Socket timeout")
123 # Force delay between messages if invoked by the config
124
125
126
127
128
129
130
131 if message == "Hey!":
132 send_message("Hello, welcome to my awesome stream!," + sender)
133 elif message == '!mods':
134 send_message(sender + ', You can find all the game mods here: http://www.google.com')
135
136 # chat log parser: check if logging is enabled
137
138
139 if log_enabled == "1":
140 timestamp = datetime.now().strftime('%Y-%m-%d %H:%M:%S')
141 # begin logging
142 # begin function here:
143 file = open(log_file, "a")
144 file.write(timestamp + " " + sender + ": " + message + '\n')
145 file.close()
146
147
148 if ivona_enabled == "0":
149 if message.endswith('?'):
150 wrapper = sender + 'asked' + message
151 elif message.endswith('!'):
152 wrapper = sender + 'shouted' + message
153 else:
154 wrapper = sender + 'said' + message
155
156 music_file = BytesIO()
157 # optional volume 0 to 1.0
158 volume = vol
159 tts = gTTS(text=wrapper, lang=lg)
160 tts.write_to_fp(music_file)
161 test_voice(music_file, volume)
162
163
164
165
166
167
168def send_text():
169 while True:
170 option = str(input("Waiting for input:"))
171 send_message(option)