Skip to content

Commit

Permalink
Merge pull request #22 from Halimao/feat/halimao/proxy
Browse files Browse the repository at this point in the history
feat: support using proxy for downloading file
  • Loading branch information
hegedustibor authored Apr 2, 2023
2 parents e6bf5eb + 1397428 commit cd8d1a1
Show file tree
Hide file tree
Showing 3 changed files with 53 additions and 2 deletions.
13 changes: 13 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,18 @@ speech := htgotts.Speech{Folder: "audio", Language: voices.English, Handler: &ha
speech.Speak("Your sentence.")
```

### Use with Proxy
```go
import (
htgotts "github.com/hegedustibor/htgo-tts"
handlers "github.com/hegedustibor/htgo-tts/handlers"
voices "github.com/hegedustibor/htgo-tts/voices"
)

speech := htgotts.Speech{Folder: "audio", Language: voices.English, Proxy: "https://..."}
speech.Speak("Your sentence.")
```

### Support and Contributions

If you encounter issues using HTGO-TTS or would like to suggest improvements to the source code, you can create an issue on the ["Issues"](https://github.com/hegedustibor/htgo-tts/issues) tab. If you'd like to contribute to the HTGO-TTS source code, please submit a pull request.
Expand All @@ -68,4 +80,5 @@ If you encounter issues using HTGO-TTS or would like to suggest improvements to

HTGO-TTS is free software and is available under the MIT license. For more information, please see the LICENSE file in the source code repository.


Have Fun!
32 changes: 30 additions & 2 deletions htgotts.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package htgotts

import (
"crypto/md5"
"crypto/tls"
"encoding/hex"
"fmt"
"io"
Expand All @@ -25,6 +26,7 @@ import (
type Speech struct {
Folder string
Language string
Proxy string
Handler handlers.PlayerInterface
}

Expand Down Expand Up @@ -87,8 +89,10 @@ func (speech *Speech) createFolderIfNotExists(folder string) error {
func (speech *Speech) downloadIfNotExists(fileName string, text string) error {
f, err := os.Open(fileName)
if err != nil {
url := fmt.Sprintf("http://translate.google.com/translate_tts?ie=UTF-8&total=1&idx=0&textlen=32&client=tw-ob&q=%s&tl=%s", url.QueryEscape(text), speech.Language)
response, err := http.Get(url)
dlURL := fmt.Sprintf("http://translate.google.com/translate_tts?ie=UTF-8&total=1&idx=0&textlen=32&client=tw-ob&q=%s&tl=%s", url.QueryEscape(text), speech.Language)

response, err := speech.urlResponse(dlURL, f)

if err != nil {
return err
}
Expand All @@ -111,3 +115,27 @@ func (speech *Speech) generateHashName(name string) string {
hash := md5.Sum([]byte(name))
return fmt.Sprintf("%s_%s", speech.Language, hex.EncodeToString(hash[:]))
}

func (speech *Speech) urlResponse(dlUrl string, f *os.File) (resp *http.Response, err error) {
var (
response *http.Response
)

if speech.Proxy != "" {
var proxyURL *url.URL
proxyURL, err = url.Parse(speech.Proxy)

if err != nil {
return response, err
}

httpCli := &http.Client{Transport: &http.Transport{
Proxy: http.ProxyURL(proxyURL),
TLSClientConfig: &tls.Config{InsecureSkipVerify: true},
}}

return httpCli.Get(dlUrl)
}

return http.Get(dlUrl)
}
10 changes: 10 additions & 0 deletions htgotts_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"github.com/hegedustibor/htgo-tts/handlers"
"github.com/hegedustibor/htgo-tts/voices"

"fmt"
"testing"
)

Expand Down Expand Up @@ -48,3 +49,12 @@ func TestSpeech_(t *testing.T) {
}
speech.PlaySpeechFile(f)
}

func TestSpeech_WithProxy(t *testing.T) {
speech := Speech{
Folder: "audio",
Language: voices.English,
Proxy: fmt.Sprintf("http://translate.google.com/translate_tts?ie=UTF-8&total=1&idx=0&textlen=32&client=tw-ob&q=%s&tl=%s", "Test", voices.English),
}
speech.Speak("Test")
}

0 comments on commit cd8d1a1

Please sign in to comment.