Skip to content

Commit

Permalink
Updated for API changed. Same format english->russian and russian->en…
Browse files Browse the repository at this point in the history
…glish translate response
  • Loading branch information
Ihar Niamilentsau committed Jul 12, 2020
1 parent a907fee commit f41b98d
Show file tree
Hide file tree
Showing 5 changed files with 51 additions and 192 deletions.
7 changes: 3 additions & 4 deletions internal/fakeapi/api.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,15 +14,14 @@ var (
"word_forms":[{"word":"accommodation","type":"прил."}],
"pic_url":"http:\/\/contentcdn.lingualeo.com\/uploads\/picture\/3589594.png",
"translate":[
{"id":33404925,"value":"размещение; жильё","votes":6261,"is_user":0,"pic_url":"http:\/\/contentcdn.lingualeo.com\/uploads\/picture\/3589594.png"},
{"id":2569250,"value":"жильё","votes":5703,"is_user":0,"pic_url":"http:\/\/contentcdn.lingualeo.com\/uploads\/picture\/31064.png"},
{"id":2718711,"value":"проживание","votes":1589,"is_user":0,"pic_url":"http:\/\/contentcdn.lingualeo.com\/uploads\/picture\/335521.png"},
{"id":185932,"value":"размещение","votes":880,"is_user":0,"pic_url":"http:\/\/contentcdn.lingualeo.com\/uploads\/picture\/374830.png"},
{"id":2735899,"value":"помещение","votes":268,"is_user":0,"pic_url":"http:\/\/contentcdn.lingualeo.com\/uploads\/picture\/620779.png"}
],
"transcription":"əkəədˈeɪːʃən","word_id":102085,"word_top":0,
"sound_url":"http:\/\/audiocdn.lingualeo.com\/v2\/3\/102085-631152000.mp3"}`
Expected = []string{"размещение", "жильё", "проживание", "помещение"}
Expected = []string{"жильё", "проживание", "размещение", "помещение"}
SearchWord = "accommodation"
)

Expand All @@ -31,13 +30,13 @@ type FakeAPI struct {
}

func (f *FakeAPI) TranslateWord(word string) api.OpResult {
res := api.EnglishResult{Word: word}
res := api.TranslationResult{Word: word}
err := res.FromResponse(responseData)
return api.OpResult{Result: &res, Error: err}
}

func (f *FakeAPI) AddWord(word string, _ []string) api.OpResult {
res := api.EnglishResult{Word: word}
res := api.TranslationResult{Word: word}
err := res.FromResponse(responseData)
return api.OpResult{Result: &res, Error: err}
}
Expand Down
24 changes: 15 additions & 9 deletions pkg/api/api.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,20 @@ type API struct {
client *http.Client
}

func checkAuthError(body *string) error {
if body == nil || *body == "" {
return nil
}
res := apiError{}
if err := getJSONFromString(*body, &res); err != nil {
return err
}
if res.ErrorCode != 0 {
return fmt.Errorf("%s: Status code: %d", res.ErrorMsg, res.ErrorCode)
}
return nil
}

func NewAPI(email string, password string, debug bool) (*API, error) {
client, err := prepareClient()
if err != nil {
Expand Down Expand Up @@ -103,15 +117,7 @@ func (api *API) auth() error {
if err != nil {
return err
}
res := apiError{}
err = getJSONFromString(*responseBody, &res)
if err != nil {
return err
}
if res.ErrorCode != 0 {
return fmt.Errorf("%s: Status code: %d", res.ErrorMsg, res.ErrorCode)
}
return nil
return checkAuthError(responseBody)
}

func debugRequest(request *http.Request) {
Expand Down
19 changes: 4 additions & 15 deletions pkg/api/constants.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,29 +2,18 @@ package api

import (
"net/http"
"regexp"
"strings"
)

const (
authURL = "https://lingualeo.com/api/login"
translateURL = "https://api.lingualeo.com/gettranslates?port=1001"
addWordURL = "https://api.lingualeo.com/addword?port=1001"
bigRussianAlphabet = "АБВГДЕЁЖЗИЙКЛМНОПРСТУФХЦЧШЩЪЫЬЭЮЯ"
apiVersion = "1.0.1"
authURL = "https://lingualeo.com/api/login"
translateURL = "https://api.lingualeo.com/gettranslates?port=1001"
addWordURL = "https://api.lingualeo.com/addword?port=1001"
apiVersion = "1.0.1"
)

var (
agentHeaders = http.Header{
"User-Agent": []string{"Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/34.0.1847.116 Safari/537.36"},
"Accept": []string{"application/json"},
}
splitRegex = regexp.MustCompile(`\s*?[:,;]+\s*?`)
blankSymbolsRegex = regexp.MustCompile(`\s+`)
blankSymbolsWithPointRegex = regexp.MustCompile(`\s+\.\s*`)
alphabet = strings.Join(
[]string{symbols, bigRussianAlphabet, strings.ToLower(bigRussianAlphabet)},
"",
)
symbols = "-. "
)
163 changes: 29 additions & 134 deletions pkg/api/items.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ type Result interface {
PrintTranslation()
PrintAddedTranslation()
InDictionary() bool
IsRussian() bool
}

type apiError struct {
Expand All @@ -56,194 +57,88 @@ type OpResult struct {
}

func opResultFromBody(word string, body string) OpResult {
engOp := opEnglishResultFromBody(word, body)
if engOp.Error != nil {
rusOp := opRussianResultFromBody(word, body)
if rusOp.Error != nil {
return engOp
}
return rusOp
}
return engOp
}

func opEnglishResultFromBody(word string, body string) OpResult {
res := EnglishResult{Word: word}
err := res.FromResponse(body)
return OpResult{
Error: err,
Result: &res,
}
}

func opRussianResultFromBody(word string, body string) OpResult {
res := RussianResult{Word: word}
res := TranslationResult{Word: word}
err := res.FromResponse(body)
return OpResult{
Error: err,
Result: &res,
}
}

type WordForm struct {
PictureURL string `json:"pic_url"`
SoundURL string `json:"sound_url"`
Transcription string `json:"transcription"`
Votes int `json:"votes"`
Word string `json:"word"`
}

type EnglishResult struct {
Word string `json:"-"`
Words []string `json:"-"`
Exists bool `json:"-"`
SoundURL string `json:"sound_url"`
Transcription string `json:"transcription"`
Translate []Word `json:"translate"`
ErrorMsg string `json:"error_msg"`
DirectionEnglish bool `json:"directionEnglish"`
type TranslationResult struct {
Word string `json:"-"`
Words []string `json:"-"`
Exists convertibleBoolean `json:"is_user"`
SoundURL string `json:"sound_url"`
Transcription string `json:"transcription"`
Translate []Word `json:"translate"`
ErrorMsg string `json:"error_msg"`
DirectionEnglish bool `json:"directionEnglish"`
}

func (result *EnglishResult) FromResponse(body string) error {
func (result *TranslationResult) FromResponse(body string) error {
return fromResponse(result, body)
}

func (result *EnglishResult) GetTranscription() []string {
func (result *TranslationResult) GetTranscription() []string {
return []string{result.Transcription}
}

func (result *EnglishResult) PrintTranslation() {
func (result *TranslationResult) PrintTranslation() {
printTranslation(result)
}

func (result *EnglishResult) PrintAddedTranslation() {
func (result *TranslationResult) PrintAddedTranslation() {
printAddedTranslation(result)
}

func (result *EnglishResult) parse() {
isUsed := false
func (result *TranslationResult) parse() {
sort.Slice(result.Translate, func(i, j int) bool {
return result.Translate[i].Votes > result.Translate[j].Votes
})
for _, translate := range result.Translate {
if !isUsed {
isUsed = bool(translate.Exists)
}
result.Words = append(result.Words, sanitizeWords(translate.Value)...)
result.Words = append(result.Words, translate.Value)
}
result.Exists = isUsed
result.Words = utils.Unique(result.Words)
}

func (result *EnglishResult) GetWord() string {
func (result *TranslationResult) GetWord() string {
return result.Word
}

func (result *EnglishResult) SetWord(word string) {
func (result *TranslationResult) SetWord(word string) {
result.Word = word
}

func (result *EnglishResult) GetTranslate() []string {
func (result *TranslationResult) GetTranslate() []string {
return result.Words
}

func (result *EnglishResult) SetTranslate(translates []string, replace bool) {
func (result *TranslationResult) SetTranslate(translates []string, replace bool) {
if replace {
result.Words = utils.Unique(translates)
} else {
result.Words = utils.Unique(append(result.Words, translates...))
}
}

func (result *EnglishResult) GetSoundURLs() []string {
func (result *TranslationResult) GetSoundURLs() []string {
return []string{result.SoundURL}
}

func (result *EnglishResult) InDictionary() bool {
return result.Exists
func (result *TranslationResult) InDictionary() bool {
return bool(result.Exists)
}

func (result *EnglishResult) Error() string {
func (result *TranslationResult) Error() string {
return result.ErrorMsg
}

func (result *TranslationResult) IsRussian() bool {
return result.Transcription == ""
}

type NoResult struct {
Translate []string `json:"translate"`
ErrorMsg string `json:"error_msg"`
}

type RussianResult struct {
Word string `json:"-"`
Words []string `json:"-"`
Exists convertibleBoolean `json:"is_user"`
Translate []string `json:"translate"`
ErrorMsg string `json:"error_msg"`
DirectionEnglish bool `json:"directionEnglish"`
WordForms []WordForm `json:"word_forms"`
}

func (result *RussianResult) FromResponse(body string) error {
return fromResponse(result, body)
}

func (result *RussianResult) parse() {
sort.Slice(result.WordForms, func(i, j int) bool {
return result.WordForms[i].Votes > result.WordForms[j].Votes
})
for _, form := range result.WordForms {
result.Words = append(result.Words, form.Word)
}
result.Words = utils.Unique(result.Words)
}

func (result *RussianResult) Error() string {
return result.ErrorMsg
}

func (result *RussianResult) InDictionary() bool {
return bool(result.Exists)
}

func (result *RussianResult) GetSoundURLs() []string {
urls := make([]string, 0)
for _, form := range result.WordForms {
urls = append(urls, form.SoundURL)
}
return urls
}

func (result *RussianResult) GetWord() string {
return result.Word
}

func (result *RussianResult) SetWord(word string) {
result.Word = word
}

func (result *RussianResult) GetTranslate() []string {
return result.Words
}

func (result *RussianResult) GetTranscription() []string {
transcriptions := make([]string, 0)
for _, form := range result.WordForms {
transcriptions = append(transcriptions, form.Transcription)
}
return transcriptions
}

func (result *RussianResult) SetTranslate(translates []string, replace bool) {
if replace {
result.Words = utils.Unique(translates)
} else {
result.Words = utils.Unique(append(result.Words, translates...))
}
}

func (result *RussianResult) PrintTranslation() {
printTranslation(result)
}

func (result *RussianResult) PrintAddedTranslation() {
printAddedTranslation(result)
}
30 changes: 0 additions & 30 deletions pkg/api/parsing.go

This file was deleted.

0 comments on commit f41b98d

Please sign in to comment.