/
main.go
124 lines (109 loc) · 2.32 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
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
package main
import (
"encoding/json"
"fmt"
"io/ioutil"
"log"
"net/http"
"os"
"strconv"
"github.com/fatih/color"
)
const help = `Usage:
# Shows one def.
urban [word]
# Limits def. to number you specify
urban [word] [limit]
# Shows all def.
urban [word] all
`
type Results struct {
Tags []string `json:"tags"`
ResultType string `json:"result_type"`
List []struct {
Definition string `json:"definition"`
Permalink string `json:"permalink"`
ThumbsUp int `json:"thumbs_up"`
Author string `json:"author"`
Word string `json:"word"`
Defid int `json:"defid"`
CurrentVote string `json:"current_vote"`
Example string `json:"example"`
ThumbsDown int `json:"thumbs_down"`
} `json:"list"`
Sounds []string `json:"sounds"`
}
const api_endpoint = "http://api.urbandictionary.com/v0/define?term="
/* Fetches results */
func GetResults(word string) Results {
//get json data
res, err := http.Get(api_endpoint + word)
if err != nil {
log.Fatalf("Failed to fetch api.")
}
defer res.Body.Close()
body, err := ioutil.ReadAll(res.Body)
if err != nil {
log.Fatalf("err: %v", err)
}
//unmarshal json
results := Results{}
json.Unmarshal(body, &results)
return results
}
func (r Results) print(limit int) {
i := 0
if r.ResultType == "exact" {
for _, def := range r.List {
if limit == -1 || i < limit {
// ugly code for windows!
color.Set(color.FgBlue)
fmt.Printf("%d)\n", i+1)
color.Unset()
color.Set(color.FgGreen)
fmt.Printf("%s\n", "Def:")
color.Unset()
fmt.Println(def.Definition)
color.Set(color.FgGreen)
fmt.Printf("%s\n", "Eg:")
color.Unset()
fmt.Printf("%s\n\n", def.Example)
i++
}
}
} else {
color.Red("No definitions.\n")
return
}
}
/* Parses commandline arguments and prints meanings */
func ParseArg() {
var limit int
// argument parsing
args, arglen := os.Args, len(os.Args)
if arglen == 2 {
// no limit
limit = 1
} else if arglen == 3 {
// limit specified
var err error
// int or all
if args[2] == "all" {
limit = -1
} else {
limit, err = strconv.Atoi(args[2])
if err != nil {
fmt.Println("Limit should be a number.")
}
}
} else {
// for invalid args.
fmt.Printf(help)
return
}
results := GetResults(args[1])
results.print(limit)
}
func main() {
ParseArg()
}