Advertisement
Guest User

Untitled

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