<commit_before>import json
import os
import subprocess

from config import config
from hashlib import sha256
from oauthlib.oauth2 import BackendApplicationClient
from requests_oauthlib import OAuth2Session
from urllib import quote_plus

from text_manipulators import split_text
from sounds import SoundPlayer

from speaker_db import SpeakerDB


class ATTTextToSpeech(object):

    CLIENT_ID = config['att_speech']['client_id']
    CLIENT_SECRET = config['att_speech']['client_secret']
    CLIENT_SCOPE = ('TTS',)
    TOKEN_FIELD_NAME = 'att_speech_token'
    TOKEN_URL = 'https://api.att.com/oauth/v4/token'
    TTS_URL = 'https://api.att.com/speech/v3/textToSpeech'
    TTS_HEADERS = {
            'Content-Type': 'text/plain',
            'Accept': 'audio/x-wav',
            'X-Arg': 'VoiceName={},Tempo={}'.format(config['att_speech']['voice_name'],
                                                    config['att_speech']['tempo'])
    }

    def __init__(self):
        self._db = SpeakerDB()
        self.client = self.get_client()

    def get_client(self):
        token = self._db.get_field_value('att_speech_token')
        if token:
            token = json.loads(token)
        else:
            oauth = OAuth2Session(client=BackendApplicationClient(client_id=self.CLIENT_ID))
            token = oauth.fetch_token(token_url=self.TOKEN_URL,
                                      client_id=self.CLIENT_ID,
                                      client_secret=self.CLIENT_SECRET,
                                      scope=self.CLIENT_SCOPE)
            self.save_token(token)

        return OAuth2Session(self.CLIENT_ID,
                             token=token,
                             auto_refresh_url=self.TOKEN_URL,
                             token_updater=self.save_token)

    def save_token(self, token):
        self._db.set_field_value(self.TOKEN_FIELD_NAME, json.dumps(token))

    def say(self, text):
        phrases = [text]
        filenames = []
        if len(text) > 100:
            phrases = split_text(text, 100)

        for phrase in phrases:
            hsh = sha256()
            hsh.update(phrase.lower())
            filename = 'speech/%s.wav' % hsh.hexdigest()
            self.create_sound_file(filename, phrase)
            filenames.append(filename)

        for filename in filenames:
            SoundPlayer(config['wav_player']).play_sound(filename)

    def create_sound_file(self, filename, text):
        if os.path.isfile(filename) and os.path.getsize(filename):
            return

        response = self.client.post(self.TTS_URL, headers=self.TTS_HEADERS, data=text)
        with open(filename, 'wb') as f:
            f.write(response.content)



class GoogleTextToSpeech(object):

    def __init__(self, url_string=None):

        self.url_string = url_string
        if not url_string:
            self.url_string = u"http://translate.google.com/translate_tts?tl=en_gb&ie=UTF-8&q=%s"

    def say(self, text):<commit_msg>Made sound file hash include tempo and voice<commit_after>import json
import os
import subprocess

from config import config
from hashlib import sha256
from oauthlib.oauth2 import BackendApplicationClient
from requests_oauthlib import OAuth2Session
from urllib import quote_plus

from text_manipulators import split_text
from sounds import SoundPlayer

from speaker_db import SpeakerDB


class ATTTextToSpeech(object):

    CLIENT_ID = config['att_speech']['client_id']
    CLIENT_SECRET = config['att_speech']['client_secret']
    CLIENT_SCOPE = ('TTS',)
    TOKEN_FIELD_NAME = 'att_speech_token'
    TOKEN_URL = 'https://api.att.com/oauth/v4/token'
    TTS_URL = 'https://api.att.com/speech/v3/textToSpeech'
    TTS_VOICE= config['att_speech']['voice_name']
    TTS_TEMPO = config['att_speech']['tempo']
    TTS_HEADERS = {
            'Content-Type': 'text/plain',
            'Accept': 'audio/x-wav',
            'X-Arg': 'VoiceName={},Tempo={}'.format(TTS_VOICE, TTS_TEMPO)
    }

    def __init__(self):
        self._db = SpeakerDB()
        self.client = self.get_client()

    def get_client(self):
        token = self._db.get_field_value('att_speech_token')
        if token:
            token = json.loads(token)
        else:
            oauth = OAuth2Session(client=BackendApplicationClient(client_id=self.CLIENT_ID))
            token = oauth.fetch_token(token_url=self.TOKEN_URL,
                                      client_id=self.CLIENT_ID,
                                      client_secret=self.CLIENT_SECRET,
                                      scope=self.CLIENT_SCOPE)
            self.save_token(token)

        return OAuth2Session(self.CLIENT_ID,
                             token=token,
                             auto_refresh_url=self.TOKEN_URL,
                             token_updater=self.save_token)

    def save_token(self, token):
        self._db.set_field_value(self.TOKEN_FIELD_NAME, json.dumps(token))

    def say(self, text):
        phrases = [text]
        filenames = []
        if len(text) > 100:
            phrases = split_text(text, 100)

        for phrase in phrases:
            hsh = sha256()
            hsh.update(phrase.lower() + self.TTS_VOICE + self.TTS_TEMPO)
            filename = 'speech/%s.wav' % hsh.hexdigest()
            self.create_sound_file(filename, phrase)
            filenames.append(filename)

        for filename in filenames:
            SoundPlayer(config['wav_player']).play_sound(filename)

    def create_sound_file(self, filename, text):
        if os.path.isfile(filename) and os.path.getsize(filename):
            return

        response = self.client.post(self.TTS_URL, headers=self.TTS_HEADERS, data=text)
        with open(filename, 'wb') as f:
            f.write(response.content)



class GoogleTextToSpeech(object):

    def __init__(self, url_string=None):

        self.url_string = url_string
        if not url_string:
            self.url_string = u"http://translate.google.com/translate_tts?tl=en_gb&ie=UTF-8&q=%s"

    def say(self, text):
{'fraction_non_alphanumeric': 0.10344827586206896, 'fraction_numerical': 0.0062997347480106105, 'mean_word_length': 2.313014827018122, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 5, 'https://': 4, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 8, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '6183492', 'n_tokens_mistral': 1923, 'n_tokens_neox': 1849, 'n_words': 326}