Added Yandex translation API
This commit is contained in:
parent
b849c2a8cf
commit
5f6eb05881
18
rss.go
18
rss.go
|
@ -14,7 +14,7 @@ import (
|
|||
|
||||
var text_template = `
|
||||
[*%s*](%s)
|
||||
_by %s_
|
||||
_%s_
|
||||
|
||||
%s
|
||||
`
|
||||
|
@ -50,11 +50,21 @@ func ParseRSS(api *echotron.API) {
|
|||
if len(chats) == 0 {
|
||||
return
|
||||
}
|
||||
title := item.Title
|
||||
author := "by " + item.Author.Name
|
||||
desc := item.Description
|
||||
translator := InitTranslator()
|
||||
translates, err := translator.Translate("ru", item.Title, "by "+item.Author.Name, item.Description)
|
||||
if err == nil {
|
||||
title = translates[0]
|
||||
author = translates[1]
|
||||
desc = translates[2]
|
||||
}
|
||||
text := fmt.Sprintf(text_template,
|
||||
nechotron.EscapeMd2(item.Title),
|
||||
nechotron.EscapeMd2(title),
|
||||
item.Link,
|
||||
nechotron.EscapeMd2(item.Authors[0].Name),
|
||||
nechotron.EscapeMd2(item.Description),
|
||||
nechotron.EscapeMd2(author),
|
||||
nechotron.EscapeMd2(desc),
|
||||
)
|
||||
sent := false
|
||||
for _, chat := range chats {
|
||||
|
|
|
@ -0,0 +1,69 @@
|
|||
package main
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"encoding/json"
|
||||
"errors"
|
||||
"io"
|
||||
"net/http"
|
||||
"os"
|
||||
)
|
||||
|
||||
type Translator struct {
|
||||
Key string
|
||||
FolderID string
|
||||
}
|
||||
|
||||
func InitTranslator() *Translator {
|
||||
return &Translator{
|
||||
Key: os.Getenv("YA_KEY"),
|
||||
FolderID: os.Getenv("YA_FOLDER"),
|
||||
}
|
||||
}
|
||||
|
||||
type TranslateBody struct {
|
||||
FolderID string `json:"folderId"`
|
||||
Texts []string `json:"texts"`
|
||||
TargetLang string `json:"targetLanguageCode"`
|
||||
}
|
||||
|
||||
type Translation struct {
|
||||
Text string `json:"text"`
|
||||
}
|
||||
|
||||
type TranslateResponse struct {
|
||||
Translations []Translation `json:"translations"`
|
||||
}
|
||||
|
||||
func (t *Translator) Translate(lang string, texts ...string) ([]string, error) {
|
||||
requestURL := "https://translate.api.cloud.yandex.net/translate/v2/translate"
|
||||
|
||||
translateBody := TranslateBody{
|
||||
FolderID: t.FolderID,
|
||||
Texts: texts,
|
||||
TargetLang: lang,
|
||||
}
|
||||
body, err := json.Marshal(translateBody)
|
||||
if err != nil {
|
||||
return nil, errors.Join(errors.New("can't marshal translation body"), err)
|
||||
}
|
||||
req, _ := http.NewRequest(http.MethodPost, requestURL, bytes.NewBuffer(body))
|
||||
req.Header.Set("Authorization", "Api-Key "+t.Key)
|
||||
req.Header.Set("Content-Type", "application/json")
|
||||
res, err := http.DefaultClient.Do(req)
|
||||
if err != nil {
|
||||
return nil, errors.Join(errors.New("can't access translate api"), err)
|
||||
}
|
||||
defer res.Body.Close()
|
||||
response, _ := io.ReadAll(res.Body)
|
||||
var translations TranslateResponse
|
||||
err = json.Unmarshal(response, &translations)
|
||||
if err != nil {
|
||||
return nil, errors.Join(errors.New("can't unmarshal translate response"), err)
|
||||
}
|
||||
results := []string{}
|
||||
for _, tr := range translations.Translations {
|
||||
results = append(results, tr.Text)
|
||||
}
|
||||
return results, nil
|
||||
}
|
Loading…
Reference in New Issue