mozhi/utils/tts.go

154 lines
2.9 KiB
Go
Raw Normal View History

2023-07-06 16:24:45 +05:30
package utils
import (
2023-07-22 19:54:15 +05:30
"context"
"encoding/base64"
2023-07-06 16:24:45 +05:30
"github.com/carlmjohnson/requests"
"github.com/google/go-querystring/query"
2023-07-06 16:24:45 +05:30
"os"
)
type ReversoTTS struct {
2023-07-22 19:54:15 +05:30
Id string
2023-07-06 16:24:45 +05:30
Voice string
}
func TTSGoogle(lang string, text string) string {
type Options struct {
2023-07-22 19:54:15 +05:30
Lang string `url:"tl"`
Text string `url:"q"`
Client string `url:"client"`
}
2023-07-22 19:54:15 +05:30
opt := Options{lang, text, "tw-ob"}
v, _ := query.Values(opt)
2023-07-06 16:24:45 +05:30
var file string
2023-07-22 19:54:15 +05:30
url := "https://translate.google.com/translate_tts?" + v.Encode()
2023-07-06 16:24:45 +05:30
err := requests.
URL(url).
ToString(&file).
Fetch(context.Background())
if err != nil {
file = ""
}
return file
}
func TTSReverso(lang string, text string) string {
2023-07-06 16:24:45 +05:30
var TTSData = []ReversoTTS{
// http://voice.reverso.net/RestPronunciation.svc/v1/output=json/GetAvailableVoices with randomized deduplication
ReversoTTS{
2023-07-22 19:54:15 +05:30
Id: "ar",
2023-07-06 16:24:45 +05:30
Voice: "Mehdi22k",
},
ReversoTTS{
2023-07-22 19:54:15 +05:30
Id: "zh",
2023-07-06 16:24:45 +05:30
Voice: "Lulu22k",
},
ReversoTTS{
2023-07-22 19:54:15 +05:30
Id: "cz",
2023-07-06 16:24:45 +05:30
Voice: "Eliska22k",
},
ReversoTTS{
2023-07-22 19:54:15 +05:30
Id: "dk",
2023-07-06 16:24:45 +05:30
Voice: "Mette22k",
},
ReversoTTS{
2023-07-22 19:54:15 +05:30
Id: "nl",
2023-07-06 16:24:45 +05:30
Voice: "Daan22k",
},
ReversoTTS{
2023-07-22 19:54:15 +05:30
Id: "en",
2023-07-06 16:24:45 +05:30
Voice: "Will22k",
},
ReversoTTS{
2023-07-22 19:54:15 +05:30
Id: "fr",
2023-07-06 16:24:45 +05:30
Voice: "Margaux22k",
},
ReversoTTS{
2023-07-22 19:54:15 +05:30
Id: "de",
2023-07-06 16:24:45 +05:30
Voice: "Andreas22k",
},
ReversoTTS{
2023-07-22 19:54:15 +05:30
Id: "gr",
2023-07-06 16:24:45 +05:30
Voice: "Dimitris22k",
},
ReversoTTS{
2023-07-22 19:54:15 +05:30
Id: "heb",
2023-07-06 16:24:45 +05:30
Voice: "he-IL-Asaf",
},
ReversoTTS{
2023-07-22 19:54:15 +05:30
Id: "it",
2023-07-06 16:24:45 +05:30
Voice: "Chiara22k",
},
ReversoTTS{
2023-07-22 19:54:15 +05:30
Id: "jp",
2023-07-06 16:24:45 +05:30
Voice: "Sakura22k",
},
ReversoTTS{
2023-07-22 19:54:15 +05:30
Id: "kr",
2023-07-06 16:24:45 +05:30
Voice: "Minji22k",
},
ReversoTTS{
2023-07-22 19:54:15 +05:30
Id: "pl",
2023-07-06 16:24:45 +05:30
Voice: "Monika22k",
},
ReversoTTS{
2023-07-22 19:54:15 +05:30
Id: "pt",
2023-07-06 16:24:45 +05:30
Voice: "Celia22k",
},
ReversoTTS{
2023-07-22 19:54:15 +05:30
Id: "ro",
2023-07-06 16:24:45 +05:30
Voice: "ro-RO-Andrei",
},
ReversoTTS{
2023-07-22 19:54:15 +05:30
Id: "ru",
2023-07-06 16:24:45 +05:30
Voice: "Alyona22k",
},
ReversoTTS{
2023-07-22 19:54:15 +05:30
Id: "es",
2023-07-06 16:24:45 +05:30
Voice: "Antonio22k",
},
ReversoTTS{
2023-07-22 19:54:15 +05:30
Id: "se",
2023-07-06 16:24:45 +05:30
Voice: "Erik22k",
},
ReversoTTS{
2023-07-22 19:54:15 +05:30
Id: "tr",
2023-07-06 16:24:45 +05:30
Voice: "Ipek22k",
},
}
// https://voice.reverso.net/RestPronunciation.svc/v1/output=json/GetVoiceStream/voiceName=Lulu22k?voiceSpeed=80&inputText=6K%20V6aqM Base64 input text
text2 := base64.StdEncoding.EncodeToString([]byte(text))
2023-07-22 19:54:15 +05:30
var voice string
for _, s := range TTSData {
if s.Id == lang {
voice = s.Voice
break
}
}
2023-07-06 16:24:45 +05:30
type Options struct {
2023-07-22 19:54:15 +05:30
VoiceSpeed int `url:"voiceSpeed"`
Text string `url:"inputText"`
}
2023-07-22 19:54:15 +05:30
opt := Options{80, text2}
v, _ := query.Values(opt)
2023-07-06 16:24:45 +05:30
var file string
2023-07-22 19:54:15 +05:30
url := "https://voice.reverso.net/RestPronunciation.svc/v1/output=json/GetVoiceStream/voiceName=" + voice + "?" + v.Encode()
2023-07-06 16:24:45 +05:30
UserAgent, ok := os.LookupEnv("SIMPLYTRANSLATE_USER_AGENT")
if !ok {
UserAgent = "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/110.0.0.0 Safari/537.36"
}
err := requests.
URL(url).
ToString(&file).
UserAgent(UserAgent).
Fetch(context.Background())
if err != nil {
file = ""
}
return file
}