2021-09-07 20:16:28 +00:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
2021-12-16 19:21:54 +00:00
|
|
|
"encoding/base64"
|
2021-09-07 20:16:28 +00:00
|
|
|
"errors"
|
2021-12-16 19:21:54 +00:00
|
|
|
"log"
|
2021-09-07 20:16:28 +00:00
|
|
|
"net/url"
|
|
|
|
"os"
|
|
|
|
"path"
|
|
|
|
"path/filepath"
|
|
|
|
|
2021-12-26 08:18:08 +00:00
|
|
|
"github.com/carlmjohnson/requests"
|
2021-09-07 20:16:28 +00:00
|
|
|
)
|
|
|
|
|
2021-12-16 19:21:54 +00:00
|
|
|
const ttsParameter = "tts"
|
|
|
|
|
|
|
|
func (a *goBlog) initTTS() {
|
|
|
|
if !a.ttsEnabled() {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
createOrUpdate := func(p *post) {
|
|
|
|
// Automatically create audio for published section posts only
|
|
|
|
if !p.isPublishedSectionPost() {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
// Check if there is already a tts audio file
|
|
|
|
if p.firstParameter(ttsParameter) != "" {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
// Create TTS audio
|
|
|
|
err := a.createPostTTSAudio(p)
|
|
|
|
if err != nil {
|
|
|
|
log.Printf("create post audio for %s failed: %v", p.Path, err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
a.pPostHooks = append(a.pPostHooks, createOrUpdate)
|
|
|
|
a.pUpdateHooks = append(a.pUpdateHooks, createOrUpdate)
|
|
|
|
a.pDeleteHooks = append(a.pDeleteHooks, func(p *post) {
|
|
|
|
// Try to delete the audio file
|
|
|
|
_ = a.deletePostTTSAudio(p)
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
func (a *goBlog) ttsEnabled() bool {
|
|
|
|
tts := a.cfg.TTS
|
|
|
|
// Requires media storage as well
|
|
|
|
return tts != nil && tts.Enabled && tts.GoogleAPIKey != "" && a.mediaStorageEnabled()
|
|
|
|
}
|
|
|
|
|
2021-09-07 20:16:28 +00:00
|
|
|
func (a *goBlog) createPostTTSAudio(p *post) error {
|
|
|
|
// Get required values
|
|
|
|
lang := a.cfg.Blogs[p.Blog].Lang
|
|
|
|
if lang == "" {
|
|
|
|
lang = "en"
|
|
|
|
}
|
|
|
|
text := a.renderMdTitle(p.Title()) + "\n\n" + cleanHTMLText(string(a.postHtml(p, false)))
|
|
|
|
|
|
|
|
// Generate audio file
|
|
|
|
tmpDir, err := os.MkdirTemp("", "")
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
defer func() {
|
|
|
|
_ = os.RemoveAll(tmpDir)
|
|
|
|
}()
|
|
|
|
outputFileName := filepath.Join(tmpDir, "audio.mp3")
|
|
|
|
err = a.createTTSAudio(lang, text, outputFileName)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
// Save new audio file
|
|
|
|
file, err := os.Open(outputFileName)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
fileHash, err := getSHA256(file)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
loc, err := a.saveMediaFile(fileHash+".mp3", file)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2021-12-16 19:21:54 +00:00
|
|
|
if loc == "" {
|
|
|
|
return errors.New("no media location for tts audio")
|
|
|
|
}
|
|
|
|
|
|
|
|
if old := p.firstParameter(ttsParameter); old != "" && old != loc {
|
|
|
|
// Already has tts audio, but with different location
|
|
|
|
// Try to delete the old audio file
|
|
|
|
_ = a.deletePostTTSAudio(p)
|
|
|
|
}
|
2021-09-07 20:16:28 +00:00
|
|
|
|
|
|
|
// Set post parameter
|
2021-12-16 19:21:54 +00:00
|
|
|
err = a.db.replacePostParam(p.Path, ttsParameter, []string{loc})
|
|
|
|
if err != nil {
|
|
|
|
return err
|
2021-09-07 20:16:28 +00:00
|
|
|
}
|
|
|
|
|
2021-12-16 19:21:54 +00:00
|
|
|
// Purge cache
|
|
|
|
a.cache.purge()
|
|
|
|
|
2021-09-07 20:16:28 +00:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2021-12-16 19:21:54 +00:00
|
|
|
// Tries to delete the tts audio file, but doesn't remove the post parameter
|
|
|
|
func (a *goBlog) deletePostTTSAudio(p *post) bool {
|
|
|
|
// Check if post has tts audio
|
|
|
|
audio := p.firstParameter(ttsParameter)
|
|
|
|
if audio == "" {
|
|
|
|
return false
|
2021-09-07 20:16:28 +00:00
|
|
|
}
|
2021-12-16 19:21:54 +00:00
|
|
|
// Get filename and check if file is from the configured media storage
|
|
|
|
fileUrl, err := url.Parse(audio)
|
|
|
|
if err != nil {
|
|
|
|
// Failed to parse audio url
|
|
|
|
log.Println("failed to parse audio url:", err)
|
|
|
|
return false
|
2021-09-07 20:16:28 +00:00
|
|
|
}
|
2021-12-16 19:21:54 +00:00
|
|
|
fileName := path.Base(fileUrl.Path)
|
|
|
|
if a.getFullAddress(a.mediaFileLocation(fileName)) != audio {
|
|
|
|
// File is not from the configured media storage
|
|
|
|
return false
|
2021-09-07 20:16:28 +00:00
|
|
|
}
|
2021-12-16 19:21:54 +00:00
|
|
|
// Try to delete the audio file
|
|
|
|
err = a.deleteMediaFile(fileName)
|
|
|
|
if err != nil {
|
|
|
|
log.Println("failed to delete audio file:", err)
|
|
|
|
return false
|
2021-09-07 20:16:28 +00:00
|
|
|
}
|
2021-12-16 19:21:54 +00:00
|
|
|
return true
|
|
|
|
}
|
2021-09-07 20:16:28 +00:00
|
|
|
|
2021-12-16 19:21:54 +00:00
|
|
|
func (a *goBlog) createTTSAudio(lang, text, outputFile string) error {
|
|
|
|
// Check if Google Cloud TTS is enabled
|
|
|
|
gctts := a.cfg.TTS
|
|
|
|
if !gctts.Enabled || gctts.GoogleAPIKey == "" {
|
|
|
|
return errors.New("missing config for Google Cloud TTS")
|
2021-09-07 20:16:28 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Check parameters
|
|
|
|
if lang == "" {
|
|
|
|
return errors.New("language not provided")
|
|
|
|
}
|
|
|
|
if text == "" {
|
|
|
|
return errors.New("empty text")
|
|
|
|
}
|
|
|
|
if outputFile == "" {
|
|
|
|
return errors.New("output file not provided")
|
|
|
|
}
|
|
|
|
|
2021-12-16 19:21:54 +00:00
|
|
|
// Create request body
|
|
|
|
body := map[string]interface{}{
|
|
|
|
"audioConfig": map[string]interface{}{
|
|
|
|
"audioEncoding": "MP3",
|
|
|
|
},
|
|
|
|
"input": map[string]interface{}{
|
|
|
|
"text": text,
|
|
|
|
},
|
|
|
|
"voice": map[string]interface{}{
|
|
|
|
"languageCode": lang,
|
|
|
|
},
|
|
|
|
}
|
2021-09-07 20:16:28 +00:00
|
|
|
|
|
|
|
// Do request
|
2021-12-26 08:18:08 +00:00
|
|
|
var response map[string]interface{}
|
|
|
|
err := requests.
|
|
|
|
URL("https://texttospeech.googleapis.com/v1beta1/text:synthesize").
|
|
|
|
Param("key", gctts.GoogleAPIKey).
|
|
|
|
Client(a.httpClient).
|
|
|
|
UserAgent(appUserAgent).
|
|
|
|
Post().
|
|
|
|
BodyJSON(body).
|
|
|
|
ToJSON(&response).
|
|
|
|
Fetch(context.Background())
|
2021-09-07 20:16:28 +00:00
|
|
|
if err != nil {
|
2021-12-26 08:18:08 +00:00
|
|
|
return errors.New("tts request failed: " + err.Error())
|
2021-09-07 20:16:28 +00:00
|
|
|
}
|
|
|
|
|
2021-12-16 19:21:54 +00:00
|
|
|
// Decode response
|
2021-12-26 08:18:08 +00:00
|
|
|
if encoded, ok := response["audioContent"]; ok {
|
2021-12-16 19:21:54 +00:00
|
|
|
if encodedStr, ok := encoded.(string); ok {
|
|
|
|
if audio, err := base64.StdEncoding.DecodeString(encodedStr); err == nil {
|
2021-12-20 13:00:11 +00:00
|
|
|
return os.WriteFile(outputFile, audio, os.ModePerm)
|
2021-12-16 19:21:54 +00:00
|
|
|
} else {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
2021-09-07 20:16:28 +00:00
|
|
|
}
|
2021-12-16 19:21:54 +00:00
|
|
|
return errors.New("no audio content")
|
2021-09-07 20:16:28 +00:00
|
|
|
}
|