Skip to content

Commit

Permalink
feat: support multiple authkeys
Browse files Browse the repository at this point in the history
  • Loading branch information
missuo committed Nov 28, 2023
1 parent 5e080f3 commit 9b2d712
Show file tree
Hide file tree
Showing 2 changed files with 63 additions and 18 deletions.
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
* @Author: Vincent Young
* @Date: 2022-10-18 07:32:29
* @LastEditors: Vincent Young
* @LastEditTime: 2023-11-27 15:06:22
* @LastEditTime: 2023-11-28 00:24:20
* @FilePath: /DeepLX/README.md
* @Telegram: https://t.me/missuo
*
Expand Down Expand Up @@ -67,7 +67,7 @@
**The following settings are optional and not required.**
- `-port` or `-p` : Listening port. Default is `1188`.
- `-token` : Access token. If you have set it up, each request will need to include an `Authorization` header.
- `-authkey` : DeepL Official `AuthKey`. If you have set it up, after the 429 response, the official AuthKey will be used for the request.
- `-authkey` : DeepL Official `AuthKey`. If you have set it up, after the 429 response, the official AuthKey will be used for the request. If multiple authKeys are used simultaneously, they need to be separated by commas.

#### Requesting a token-protected **DeepLX API** instance using the `curl`
```
Expand Down
77 changes: 61 additions & 16 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
* @Author: Vincent Young
* @Date: 2023-07-01 21:45:34
* @LastEditors: Vincent Young
* @LastEditTime: 2023-11-27 15:10:06
* @LastEditTime: 2023-11-28 00:23:42
* @FilePath: /DeepLX/main.go
* @Telegram: https://t.me/missuo
*
Expand Down Expand Up @@ -205,6 +205,40 @@ func translateByAPI(text string, targetLang string, sourceLang string, authKey s
return sb.String(), nil
}

type DeepLResponse struct {
CharacterCount int `json:"character_count"`
CharacterLimit int `json:"character_limit"`
}

func checkUsage(authKey string) (bool, error) {
url := "https://api-free.deepl.com/v2/usage"
req, err := http.NewRequest("GET", url, nil)
if err != nil {
return false, err
}

req.Header.Add("Authorization", "DeepL-Auth-Key "+authKey)

client := &http.Client{}
resp, err := client.Do(req)
if err != nil {
return false, err
}
defer resp.Body.Close()

body, err := ioutil.ReadAll(resp.Body)
if err != nil {
return false, err
}

var response DeepLResponse
err = json.Unmarshal(body, &response)
if err != nil {
return false, err
}
return response.CharacterCount < 499900, nil
}

func main() {
cfg := InitConfig()

Expand Down Expand Up @@ -352,22 +386,33 @@ func main() {
}

if resp.StatusCode == http.StatusTooManyRequests {
translatedText, err := translateByAPI(translateText, sourceLang, targetLang, cfg.AuthKey)
if err != nil {
c.JSON(http.StatusTooManyRequests, gin.H{
"code": http.StatusTooManyRequests,
"message": "Too Many Requests",
})
return
authKeyArray := strings.Split(cfg.AuthKey, ",")
for _, authKey := range authKeyArray {
validity, err := checkUsage(authKey)
if err != nil {
continue
} else {
if validity == true {
translatedText, err := translateByAPI(translateText, sourceLang, targetLang, authKey)
if err != nil {
c.JSON(http.StatusTooManyRequests, gin.H{
"code": http.StatusTooManyRequests,
"message": "Too Many Requests",
})
}
c.JSON(http.StatusOK, gin.H{
"code": http.StatusOK,
"id": 1000000,
"data": translatedText,
"source_lang": sourceLang,
"target_lang": targetLang,
"method": "Official API",
})
return
}
}

}
c.JSON(http.StatusOK, gin.H{
"code": http.StatusOK,
"id": 114514,
"data": translatedText,
"source_lang": sourceLang,
"target_lang": targetLang,
"method": "Official API",
})
} else {
var alternatives []string
res.Get("result.texts.0.alternatives").ForEach(func(key, value gjson.Result) bool {
Expand Down

0 comments on commit 9b2d712

Please sign in to comment.