Skip to content

Commit

Permalink
💪 Make to be able to use pro plan endpoint
Browse files Browse the repository at this point in the history
  • Loading branch information
Omochice committed Sep 28, 2021
1 parent cbb5db2 commit 7cf8cea
Showing 1 changed file with 12 additions and 2 deletions.
14 changes: 12 additions & 2 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ type Setting struct {
AuthKey string `json:"-"`
SourceLang string `json:"source_lang"`
TargetLang string `json:"target_lang"`
IsPro bool `json:"-"`
}

type DeepLResponse struct {
Expand Down Expand Up @@ -78,7 +79,6 @@ func LoadSettings(setting Setting, automake bool) (Setting, error) {
}
if setting.SourceLang == "FILLIN" || setting.TargetLang == "FILLIN" {
return setting, fmt.Errorf("Did write config file? (%s)", configPath)

}
}
if setting.SourceLang == setting.TargetLang {
Expand All @@ -95,6 +95,7 @@ func InitializeConfigFile(ConfigPath string) error {
initSetting := Setting{
SourceLang: "FILLIN",
TargetLang: "FILLIN",
IsPro: false,
}

out, err := os.Create(ConfigPath)
Expand All @@ -119,6 +120,11 @@ func Translate(Text string, setting Setting) ([]string, error) {
params.Add("target_lang", setting.TargetLang)
params.Add("text", Text)
endpoint := "https://api-free.deepl.com/v2/translate"

if setting.IsPro {
endpoint = "https://api.deepl.com/v2/translate"
}

resp, err := http.PostForm(endpoint, params)

results := []string{}
Expand Down Expand Up @@ -215,12 +221,17 @@ func main() {
Aliases: []string{"t"},
Usage: "Target `LANG`",
},
&cli.BoolFlag{
Name: "pro",
Usage: "use pro plan's endpoint",
},
},
Action: func(c *cli.Context) error {
setting, err := LoadSettings(Setting{
SourceLang: c.String("source_lang"),
TargetLang: c.String("target_lang"),
AuthKey: os.Getenv("DEEPL_TOKEN"),
IsPro: c.Bool("pro"),
}, true)
if err != nil {
return err
Expand All @@ -239,7 +250,6 @@ func main() {
}
rawSentense = string(pipeIn)
}

} else {
if c.NArg() == 0 {
return fmt.Errorf("No filename is set. And `--stdin` option is not set.\nEither must be set.")
Expand Down

0 comments on commit 7cf8cea

Please sign in to comment.