123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384 |
- package main
-
- import (
- "bytes"
- "crypto/md5"
- "encoding/base64"
- "encoding/json"
- "fmt"
- "io"
- "io/ioutil"
- "log"
- "net/http"
- "net/url"
- "os"
- "strconv"
- "strings"
- "time"
- )
-
- func handler(w http.ResponseWriter, r *http.Request) {
- x_appid := "5c7fcc38"
- key := "57d53967332e21d23cf3389bf8129e28"
- curtime := strconv.FormatInt(time.Now().Unix(), 10)
-
- tt := make(map[string]string)
- tt["aue"] = "lame"
- tt["auf"] = "audio/L16;rate=16000"
- // tt["voice_name"] = "xiaoyan"
- tt["voice_name"] = "aisjiuxu"
- param, _ := json.Marshal(tt)
- base64_param := base64.StdEncoding.EncodeToString(param)
-
- md5Inst := md5.New()
- io.WriteString(md5Inst, key+curtime+base64_param)
- checksum := fmt.Sprintf("%x", md5Inst.Sum(nil))
-
- if r.Form == nil {
- r.ParseForm()
- }
-
- txt := r.FormValue("t")
- os.Stdout.WriteString("[" + time.Now().Local().Format("2006-01-02 15:04:05") + "][INFO] 接收到文字: 【" + txt + "】 \n")
- if txt == "" {
- w.Write([]byte(""))
- return
- }
-
- var data = url.Values{}
- data.Add("text", txt)
- req_body := data.Encode()
-
- client := &http.Client{}
- req, _ := http.NewRequest("POST", "http://api.xfyun.cn/v1/service/v1/tts", strings.NewReader(req_body))
-
- req.Header.Set("Content-Type", "application/x-www-form-urlencoded")
- req.Header.Set("X-CurTime", curtime)
- req.Header.Set("X-Appid", x_appid)
- req.Header.Set("X-Param", base64_param)
- req.Header.Set("X-CheckSum", checksum)
-
- resp, _ := client.Do(req)
- defer resp.Body.Close()
- resp_body, _ := ioutil.ReadAll(resp.Body)
-
- w.Header().Add("Content-Type", "audio/mp3")
- w.Header().Add("Content-Transfer-Encoding", "binary")
- w.Header().Add("Expires", "0")
- w.Header().Add("Cache-Control", "no-cache")
-
- respType := resp.Header.Get("content-type")
- if strings.Index(respType, "text/plain") > -1 {
- os.Stderr.WriteString("[" + time.Now().Local().Format("2006-01-02 15:04:05") + "][ERROR]: " + string(resp_body) + "\n")
- w.Write([]byte(""))
- return
- }
-
- rd := bytes.NewReader(resp_body)
- http.ServeContent(w, r, "yinbin.mp3", time.Now().Local(), rd)
- }
-
- func main() {
- http.HandleFunc("/", handler)
- log.Fatal(http.ListenAndServe(":8090", nil))
- }
|