-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
0 parents
commit a180ca2
Showing
6 changed files
with
235 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
# Binaries for programs and plugins | ||
*.exe | ||
*.exe~ | ||
*.dll | ||
*.so | ||
*.dylib | ||
|
||
# Test binary, built with `go test -c` | ||
*.test | ||
|
||
# Output of the go coverage tool, specifically when used with LiteIDE | ||
*.out | ||
|
||
# Dependency directories (remove the comment below to include it) | ||
# vendor/ | ||
# | ||
.idea/ |
Binary file not shown.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
module github.com/hzenginx/tureng | ||
|
||
require ( | ||
github.com/deanishe/awgo v0.22.1 | ||
golang.org/x/text v0.3.2 | ||
) | ||
|
||
go 1.14 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
github.com/bmatcuk/doublestar v1.1.5/go.mod h1:wiQtGV+rzVYxB7WIlirSN++5HPtPlXEo9MEoZQC/PmE= | ||
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= | ||
github.com/deanishe/awgo v0.22.1 h1:N61eAOqc2u2zMQjY0Srd3mAn1gaj/9nU4YaTe3aaHU0= | ||
github.com/deanishe/awgo v0.22.1/go.mod h1:FIvY0LMPTrqEqSDy79rcSqL6+oFUhFL9qlXBi+qQKks= | ||
github.com/deanishe/go-env v0.4.0 h1:tpu14o16gJGTN/w2gxntwxu2l5Eby30jSrnlgOfjzwk= | ||
github.com/deanishe/go-env v0.4.0/go.mod h1:RgEcGAqdRnt8ybQteAbv1Ys2lWIRE7TlgON/sbdjuaY= | ||
github.com/docopt/docopt-go v0.0.0-20180111231733-ee0de3bc6815/go.mod h1:WwZ+bS3ebgob9U8Nd0kOddGdZWjyMGR8Wziv+TBNwSE= | ||
github.com/jessevdk/go-flags v1.4.0/go.mod h1:4FA24M0QyGHXBuZZK/XkWh8h0e1EYbRYJSGM75WSRxI= | ||
github.com/kr/pretty v0.1.0/go.mod h1:dAy3ld7l9f0ibDNOQOHHMYYIIbhfbHSm3C4ZsoJORNo= | ||
github.com/kr/pty v1.1.1/go.mod h1:pFQYn66WHrOpPYNljwOMqo10TkYh1fy3cYio2l3bCsQ= | ||
github.com/kr/text v0.1.0/go.mod h1:4Jbv+DJW3UT/LiOwJeYQe1efqtUx/iVham/4vfdArNI= | ||
github.com/pkg/errors v0.9.1 h1:FEBLx1zS214owpjy7qsBeixbURkuhQAwrK5UwLGTwt4= | ||
github.com/pkg/errors v0.9.1/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINEl0= | ||
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4= | ||
github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME= | ||
github.com/stretchr/testify v1.4.0/go.mod h1:j7eGeouHqKxXV5pUuKE4zz7dFj8WfuZ+81PSLYec5m4= | ||
golang.org/x/text v0.3.2 h1:tW2bmiBqwgJj/UpqtC8EpXEZVYOwU0yG4iWbprSVAcs= | ||
golang.org/x/text v0.3.2/go.mod h1:bEr9sfX3Q8Zfm5fL9x+3itogRgK3+ptLWKqgva+5dAk= | ||
golang.org/x/tools v0.0.0-20180917221912-90fa682c2a6e/go.mod h1:n7NCudcB/nEzxVGmLbDWY5pfWTLqBcC2KZ6jyYvM4mQ= | ||
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0= | ||
gopkg.in/check.v1 v1.0.0-20180628173108-788fd7840127/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0= | ||
gopkg.in/yaml.v2 v2.2.1/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI= | ||
gopkg.in/yaml.v2 v2.2.2/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI= | ||
howett.net/plist v0.0.0-20181124034731-591f970eefbb/go.mod h1:vMygbs4qMhSZSc4lCUl2OEE+rDiIIJAIdR4m7MiMcm0= |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,95 @@ | ||
package main | ||
|
||
import ( | ||
"fmt" | ||
"github.com/deanishe/awgo" | ||
"github.com/hzenginx/tureng/tureng" | ||
"golang.org/x/text/unicode/norm" | ||
"regexp" | ||
"strings" | ||
) | ||
|
||
var wf *aw.Workflow | ||
|
||
func init() { | ||
wf = aw.New() | ||
} | ||
|
||
func splitInput(query string) (string, []string) { | ||
splited := strings.Split(query, ":") | ||
return splited[0], splited[1:] | ||
} | ||
|
||
func getIcon(category string) *aw.Icon { | ||
var icon string | ||
matched, _ := regexp.MatchString(`en->tr`, category) | ||
print(category) | ||
if matched { | ||
icon = "en_tr.png" | ||
} else { | ||
icon = "tr_en.png" | ||
} | ||
return &aw.Icon{ | ||
Value: icon, | ||
} | ||
} | ||
|
||
func handleSearchResponse(response *tureng.SearchResponse, wf *aw.Workflow) { | ||
if response.IsSuccessful { | ||
if response.Result.IsFound == 1 { | ||
for _, result := range response.Result.Results { | ||
icon := getIcon(result.Category) | ||
wf.NewItem(result.Term).Subtitle(result.Category).Icon(icon).Arg(result.Term).Valid(true) | ||
} | ||
} else { | ||
for _, suggestion := range response.Result.Suggestions { | ||
wf.NewWarningItem(suggestion, "Did you mean this?").Autocomplete(fmt.Sprintf("translate:%s", suggestion)) | ||
} | ||
} | ||
} else { | ||
wf.Fatal(response.Exception) | ||
} | ||
wf.SendFeedback() | ||
} | ||
|
||
func handleAutocompleteResponse(response *tureng.AutoCompleteResponse, wf *aw.Workflow) { | ||
for _, word := range response.Words { | ||
wf.NewItem(word).Autocomplete(fmt.Sprintf("translate:%s", word)).Icon(&aw.Icon{Value: "tureng.png"}) | ||
} | ||
wf.SendFeedback() | ||
} | ||
|
||
func run() { | ||
var query = wf.Args()[0] | ||
query = norm.NFC.String(query) | ||
command, args := splitInput(query) | ||
|
||
if command == "translate" { | ||
if len(args) > 0 { | ||
word := args[0] | ||
response, err := tureng.Search(word) | ||
if err != nil { | ||
wf.FatalError(err) | ||
} else { | ||
handleSearchResponse(response, wf) | ||
} | ||
} | ||
} else { | ||
response, err := tureng.AutoComplete(command) | ||
if err != nil { | ||
wf.FatalError(err) | ||
} else { | ||
handleAutocompleteResponse(response, wf) | ||
} | ||
} | ||
} | ||
|
||
func translate() error { | ||
wf.NewItem("item") | ||
wf.SendFeedback() | ||
return nil | ||
} | ||
|
||
func main() { | ||
wf.Run(run) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,91 @@ | ||
package tureng | ||
|
||
import ( | ||
"bytes" | ||
"crypto/md5" | ||
"encoding/hex" | ||
"encoding/json" | ||
"fmt" | ||
"io/ioutil" | ||
"net/http" | ||
"net/url" | ||
) | ||
|
||
type SearchResponse struct { | ||
Exception string `json:"ExceptionMessage"` | ||
IsSuccessful bool `json:"IsSuccessful"` | ||
|
||
Result struct { | ||
IsFound int `json:"IsFound"` | ||
IsEnglishToTurkish int `json:"IsTRToEN"` | ||
Suggestions []string `json:"Suggestions"` | ||
Results []struct { | ||
Category string `json:"CategoryEN"` | ||
Term string `json:"Term"` | ||
Type string `json:"TypeEN"` | ||
} `json:"Results"` | ||
} `json:"MobileResult"` | ||
} | ||
|
||
type AutoCompleteResponse struct { | ||
Words []string | ||
} | ||
|
||
type SearchRequest struct { | ||
Term string `json:"Term"` | ||
Code string `json:"Code"` | ||
} | ||
|
||
func Search(word string) (*SearchResponse, error) { | ||
code := md5.Sum([]byte(fmt.Sprintf("%s%s", word, SECRET))) | ||
req := SearchRequest{word, hex.EncodeToString(code[:])} | ||
|
||
requestJson, err := json.Marshal(req) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
resp, err := http.Post(SEARCH_URL, BODY_TYPE, bytes.NewBuffer(requestJson)) | ||
if err != nil { | ||
return nil, err | ||
} | ||
defer resp.Body.Close() | ||
|
||
body, _ := ioutil.ReadAll(resp.Body) | ||
response := &SearchResponse{} | ||
|
||
err = json.Unmarshal(body, response) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
return response, nil | ||
} | ||
|
||
func AutoComplete(term string) (*AutoCompleteResponse, error) { | ||
term = url.QueryEscape(term) | ||
requestUrl := fmt.Sprintf(AUTOCOMPLETE_URL, term) | ||
|
||
resp, err := http.Get(requestUrl) | ||
if err != nil { | ||
return nil, err | ||
} | ||
defer resp.Body.Close() | ||
|
||
body, _ := ioutil.ReadAll(resp.Body) | ||
var words []string | ||
err = json.Unmarshal(body, &words) | ||
|
||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
return &AutoCompleteResponse{Words: words}, nil | ||
} | ||
|
||
const ( | ||
SEARCH_URL = "http://ws.tureng.com/TurengSearchServiceV4.svc/Search" | ||
AUTOCOMPLETE_URL = "https://ac.tureng.co?t=%s" | ||
SECRET = "46E59BAC-E593-4F4F-A4DB-960857086F9C" | ||
BODY_TYPE = "application/json" | ||
) |