Browse Source

no message

chenyuanyang 6 years ago
parent
commit
98f2ec6592
1 changed files with 111 additions and 1 deletions
  1. 111
    1
      cmd/job/job.go

+ 111
- 1
cmd/job/job.go View File

@@ -2,17 +2,127 @@ package job
2 2
 
3 3
 import (
4 4
 	"fmt"
5
+	"git.links123.net/Slate/CorpusAI/service"
6
+	"git.links123.net/Slate/CorpusAI/service/store/cache"
7
+	"git.links123.net/Slate/CorpusAI/service/store/mysql"
8
+	"git.links123.net/Slate/CorpusAI/config"
9
+	"github.com/jinzhu/gorm"
10
+	_ "github.com/go-sql-driver/mysql"
5 11
 	"github.com/spf13/cobra"
12
+	"net/url"
13
+	"strconv"
6 14
 	"strings"
7 15
 )
8 16
 
17
+type TtsRaw struct {
18
+	ID                     int64
19
+	Text                   string		//翻译的文本
20
+	UniqKey                string 	//唯一键值
21
+	Status				   int64
22
+	Remark                 string
23
+}
24
+
25
+func (TtsRaw) TableName() string {
26
+	return "lnk_corpus_tts_raw"
27
+}
28
+
29
+type langType struct {
30
+	languageCode string;
31
+	voiceName string;
32
+}
33
+
9 34
 func RunCommand() *cobra.Command {
10 35
 	cmd := &cobra.Command{
11 36
 		Use:   "job",
12 37
 		Short: "Run the job service",
13 38
 		Run: func(cmd *cobra.Command, args []string) {
14
-			fmt.Println("Echo: " + strings.Join(args, " "))
39
+			//fmt.Println("Echo: " + strings.Join(args, " "))
40
+
41
+			minId := args[0]
42
+			maxId := args[1]
43
+
44
+			dbConfig := config.C.DB
45
+			settings := dbConfig.User+":"+dbConfig.Password+"@tcp("+dbConfig.Host+")/"+dbConfig.Name+"?charset=utf8&parseTime=True&loc=Local"
46
+
47
+			var ttsRawList []TtsRaw
48
+
49
+			db, err := gorm.Open("mysql", settings)
50
+			if err != nil {
51
+				panic("failed to connect database")
52
+			}
53
+
54
+			speed, _ := strconv.ParseFloat( "1.00", 64)
55
+			pitch, _ := strconv.ParseFloat( "0.00", 64)
56
+
57
+			db.Where("id <= ? and id >= ?", maxId, minId).Find(&ttsRawList)
58
+
59
+			for _, ttsRaw := range ttsRawList {
60
+
61
+				result, msg := SyncTtsOss(ttsRaw.Text, speed, pitch)
62
+
63
+				if result == true {
64
+					ttsRaw.Status = 1
65
+					//更新成功
66
+				} else {
67
+					ttsRaw.Status = -1
68
+					//更新错误状态和msg
69
+					ttsRaw.Remark = msg
70
+				}
71
+				fmt.Println(ttsRaw)
72
+				db.Save(&ttsRaw)
73
+			}
74
+
75
+			db.Close()
15 76
 		},
16 77
 	}
17 78
 	return cmd
18 79
 }
80
+
81
+func SyncTtsOss(text string, speed float64, pitch float64) (bool, string) {
82
+
83
+	text, _ = url.QueryUnescape(text)
84
+
85
+	text = strings.Trim(text , "")
86
+
87
+	if text == "" {
88
+		return false, "Error: text null"
89
+	}
90
+
91
+	typeMap := make(map[int]langType)
92
+	typeMap[1] = langType{"en-US","en-US-Wavenet-B"}
93
+	typeMap[2] = langType{"en-US","en-US-Wavenet-C"}
94
+	typeMap[3] = langType{"en-GB","en-GB-Wavenet-B"}
95
+	typeMap[4] = langType{"en-GB","en-GB-Wavenet-C"}
96
+
97
+	for _, lang := range typeMap {
98
+
99
+		ossObjectKey := service.GetTtsOssKey(text, lang.voiceName, lang.languageCode, speed, pitch)
100
+
101
+		textKey := cache.GetTextKey(ossObjectKey)
102
+
103
+		AudioContent, err := service.TextToSpeech(text, lang.voiceName, lang.languageCode, speed, pitch)
104
+		if err != nil {
105
+
106
+			return false, "TextToSpeech Error:" + err.Error()
107
+		}
108
+
109
+		uploadResult, err := service.UploadHkOss(ossObjectKey, AudioContent)
110
+
111
+		if uploadResult == true {
112
+			uploadResult, err = service.UploadOss(ossObjectKey, AudioContent)
113
+
114
+			if uploadResult == true {
115
+
116
+				//hk&cn节点oss都同步成功, set db
117
+				mysql.CreateCorpusTts(text, textKey, lang.languageCode, lang.voiceName, ossObjectKey, speed, pitch)
118
+			}
119
+		}
120
+
121
+		if err != nil {
122
+
123
+			return false, "UploadHkOss Error" + err.Error()
124
+		}
125
+	}
126
+
127
+	return true, ""
128
+}