-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
92 lines (76 loc) · 1.85 KB
/
main.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
package main
import (
"encoding/json"
"encoding/xml"
"flag"
"fmt"
"io"
"net/http"
"strings"
)
const (
XML_HEADER = `<?xml version="1.0"?>` + "\n"
WOOORDHUNT_SITE = "https://wooordhunt.ru"
)
type Tip struct {
Word string `json:"w"`
Translate string `json:"t"`
}
type Tips struct {
Tips []Tip `json:"tips"`
}
type ItemIcon struct {
Path string `json:"path" xml:"path,attr"`
}
type Item struct {
Title string `xml:"title" json:"title"`
SubTitle string `xml:"subtitle" json:"subtitle"`
Arg string `xml:"arg,attr" json:"arg"`
Icon ItemIcon `xml:"icon" json:"icon"`
}
type Items struct {
XMLName xml.Name `xml:"items" json:"-"`
Item []Item `xml:"item" json:"items"`
}
func main() {
var targetQuery *string = flag.String("q", "", "Search word")
var format *string = flag.String("f", "xml", "Output type")
flag.Parse()
uri := fmt.Sprintf(
"%s/openscripts/forjs/get_tips.php?abc=%s",
WOOORDHUNT_SITE,
strings.ToLower(*targetQuery),
)
resp, _ := http.Get(uri)
defer resp.Body.Close()
body, _ := io.ReadAll(resp.Body)
var jsonResponse Tips
json.Unmarshal(body, &jsonResponse)
var items []Item
for _, v := range jsonResponse.Tips {
query := strings.ToLower(v.Word)
items = append(items, Item{
Title: query,
SubTitle: v.Translate,
Arg: WOOORDHUNT_SITE + "/word/" + query,
Icon: ItemIcon{Path: "icon.png"},
})
}
if len(items) == 0 {
items = append(items, Item{
Title: strings.ToLower(*targetQuery),
SubTitle: "The requested word was not found.",
Arg: WOOORDHUNT_SITE,
Icon: ItemIcon{Path: "icon.png"},
})
}
output := ""
if *format == "json" {
result, _ := json.Marshal(Items{Item: items})
output = string(result)
} else if *format == "xml" {
result, _ := xml.Marshal(Items{Item: items})
output = XML_HEADER + string(result)
}
fmt.Println(output)
}