Advertisement
Guest User

Untitled

a guest
Aug 20th, 2016
111
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 3.22 KB | None | 0 0
  1. import os
  2. from io import BytesIO
  3. import socket
  4. import pyvona
  5. import time
  6. import pygame as pg
  7. from gtts import gTTS
  8. from 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
  9. lg = language
  10. vol = float(volume)
  11. c = int(max_message_duration)
  12.  
  13.  
  14. def 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.  
  40. HOST = host_addr
  41. PORT = int(port_addr)
  42. NICK = twitch_user
  43. PASS = oauth
  44. v = pyvona.create_voice(access_key, secret_key)
  45. v.voice_name = voice_pack
  46. def send_message(message):
  47.     s.send(bytes("PRIVMSG #" + NICK + " :" + message + "\r\n", "UTF-8"))
  48. #start the function here for the future
  49. s = socket.socket()
  50. s.connect((HOST, PORT))
  51. s.send(bytes("PASS " + PASS + "\r\n", "UTF-8"))
  52. s.send(bytes("NICK " + NICK + "\r\n", "UTF-8"))
  53. s.send(bytes("JOIN #" + NICK + " \r\n", "UTF-8"))
  54.  
  55.  
  56. while True:
  57.     line = str(s.recv(1024))
  58.     if "End of /NAMES list" in line:
  59.         break
  60.  
  61. while 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)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement