-
Notifications
You must be signed in to change notification settings - Fork 0
/
server.go
127 lines (110 loc) · 3.16 KB
/
server.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
package main
import (
"fmt"
"net/http"
"github.com/atselvan/ankiconnect"
"github.com/atselvan/dictionaryapi"
"github.com/gin-gonic/gin"
"github.com/privatesquare/bkst-go-utils/utils/errors"
"github.com/privatesquare/bkst-go-utils/utils/httputils"
"github.com/privatesquare/bkst-go-utils/utils/slice"
)
type server struct {
cnf *Config
}
func (s *server) GetWordsWord(c *gin.Context, word string) {
meaning, restErr := getMeaningOfWord(word)
if restErr != nil {
c.JSON(restErr.StatusCode, restErr)
return
}
c.JSON(http.StatusOK, meaning)
}
func (s *server) PostWordsWord(c *gin.Context, word string) {
// check deck status
restErr := checkDeck(s.cnf)
if restErr != nil {
c.JSON(restErr.StatusCode, restErr)
return
}
// get meaning of the word
meaning, restErr := getMeaningOfWord(word)
if restErr != nil {
c.JSON(restErr.StatusCode, restErr)
return
}
// add word to anki deck
if restErr = addWordToAnki(s.cnf, meaning);restErr != nil {
c.JSON(restErr.StatusCode, restErr)
return
}
// sync data to Anki Web
if restErr = syncAnki(s.cnf);restErr != nil {
c.JSON(restErr.StatusCode, restErr)
return
}
c.JSON(http.StatusOK, httputils.RestMsg{Message: "Word added to anki successfully"})
}
// getMeaningOfWord gets the meaning of a word using dictionaryapi
func getMeaningOfWord(word string) (*dictionaryapi.Word, *errors.RestErr) {
client := dictionaryapi.NewClient()
return client.Word.Get(word)
}
// checkDeck checks if the deck exists, if not the functions creates the deck.
func checkDeck(cnf *Config) *errors.RestErr {
client := ankiconnect.NewClient().SetURL(cnf.AnkiConnectURL)
decks, restErr := client.Decks.GetAll()
if restErr != nil {
return restErr
}
if !slice.EntryExists(*decks, cnf.AnkiDeckName) {
if restErr := client.Decks.Create(cnf.AnkiDeckName); restErr != nil {
return restErr
}
}
return nil
}
// addWordToAnki adds the word to the anki deck
func addWordToAnki(cnf *Config, word *dictionaryapi.Word) *errors.RestErr {
meanings := ""
for _, meaning := range word.Meanings {
for _, definition := range meaning.Definitions {
meanings += fmt.Sprintf("<br><b>Part of Speech:</b> %s", meaning.PartOfSpeech)
meanings += fmt.Sprintf("<br><b>Definition :</b> %s", definition.Definition)
if definition.Example != "" {
meanings += fmt.Sprintf("<br><b>Example :</b> %s", definition.Example)
}
if len(definition.Synonyms) > 0 {
meanings += fmt.Sprintf("<br><b>Synonyms :</b> %v", definition.Synonyms)
}
meanings += "<br>"
}
}
note := ankiconnect.Note{
DeckName: cnf.AnkiDeckName,
ModelName: cnf.AnkiDeckModel,
Fields: ankiconnect.Fields{
Front: word.Word,
Back: meanings,
},
Audio: []ankiconnect.Audio{
{
URL: word.Phonetics[0].Audio,
Fields: []string{word.Phonetics[0].Text},
},
},
}
client := ankiconnect.NewClient().SetURL(cnf.AnkiConnectURL)
if restErr := client.Notes.Add(note); restErr != nil {
return restErr
}
return nil
}
// syncAnki sync local data to Anki web
func syncAnki(cnf *Config) *errors.RestErr {
client := ankiconnect.NewClient().SetURL(cnf.AnkiConnectURL)
if restErr := client.Sync.Trigger(); restErr != nil {
return restErr
}
return nil
}