2015-01-23 15:10:12 -08:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
|
|
|
"encoding/json"
|
2015-01-24 02:19:03 -08:00
|
|
|
"flag"
|
2015-01-23 15:10:12 -08:00
|
|
|
"fmt"
|
|
|
|
"go/doc"
|
|
|
|
"log"
|
|
|
|
"net/http"
|
|
|
|
"os"
|
|
|
|
"sort"
|
|
|
|
"strings"
|
|
|
|
)
|
|
|
|
|
|
|
|
const url = "http://api.urbandictionary.com/v0/define?term="
|
2015-01-23 15:43:16 -08:00
|
|
|
const usage = "ud: confirm that the term you're thinking of has already been added to urban dictionary\n\nusage: ud <term>\n"
|
2015-01-23 15:10:12 -08:00
|
|
|
|
2015-01-24 02:19:03 -08:00
|
|
|
var all bool
|
|
|
|
|
|
|
|
func init() {
|
|
|
|
flag.BoolVar(&all, "all", false, "more than one")
|
|
|
|
flag.BoolVar(&all, "a", false, "more than one")
|
|
|
|
}
|
|
|
|
|
2015-01-23 15:10:12 -08:00
|
|
|
type response struct {
|
|
|
|
Results []result `json:"list"`
|
|
|
|
}
|
|
|
|
|
|
|
|
type result struct {
|
|
|
|
Definition string `json:"definition"`
|
|
|
|
Example string `json:"example"`
|
|
|
|
Link string `json:"permalink"`
|
|
|
|
Author string `json:"author"`
|
|
|
|
|
|
|
|
Up int `json:"thumbs_up"`
|
|
|
|
Down int `json:"thumbs_down"`
|
|
|
|
}
|
|
|
|
|
2016-02-13 23:50:09 -08:00
|
|
|
// ByVotes aids in sorting result based on votes
|
2015-01-23 15:10:12 -08:00
|
|
|
type ByVotes []result
|
|
|
|
|
|
|
|
func (bv ByVotes) Len() int {
|
|
|
|
return len(bv)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (bv ByVotes) Swap(i, j int) {
|
|
|
|
bv[i], bv[j] = bv[j], bv[i]
|
|
|
|
}
|
2015-01-25 01:13:29 -08:00
|
|
|
|
2015-01-23 15:10:12 -08:00
|
|
|
func (bv ByVotes) Less(i, j int) bool {
|
2015-01-24 02:15:17 -08:00
|
|
|
return float64(bv[i].Up-bv[i].Down) > float64(bv[j].Up-bv[j].Down)
|
2015-01-23 15:10:12 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
func main() {
|
2015-01-24 02:19:03 -08:00
|
|
|
flag.Parse()
|
|
|
|
if len(flag.Args()) < 1 {
|
2015-01-23 15:10:12 -08:00
|
|
|
fmt.Fprintf(os.Stderr, "%s\n", usage)
|
|
|
|
os.Exit(1)
|
|
|
|
}
|
|
|
|
|
2015-01-24 02:19:03 -08:00
|
|
|
u := fmt.Sprintf("%s%s", url, strings.Join(flag.Args(), "+"))
|
2015-01-23 15:10:12 -08:00
|
|
|
|
|
|
|
resp, err := http.Get(u)
|
|
|
|
if err != nil {
|
|
|
|
log.Fatal(err)
|
|
|
|
}
|
|
|
|
data := response{}
|
|
|
|
err = json.NewDecoder(resp.Body).Decode(&data)
|
|
|
|
resp.Body.Close()
|
|
|
|
if err != nil {
|
|
|
|
log.Fatal(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
sort.Sort(ByVotes(data.Results))
|
|
|
|
|
2015-01-24 02:19:03 -08:00
|
|
|
if !all {
|
|
|
|
if len(data.Results) > 0 {
|
|
|
|
data.Results = data.Results[:1]
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-01-23 15:10:12 -08:00
|
|
|
for i, res := range data.Results {
|
2015-01-24 02:19:03 -08:00
|
|
|
if i > 0 {
|
|
|
|
fmt.Printf("\n\n")
|
|
|
|
}
|
|
|
|
if all {
|
|
|
|
fmt.Printf(
|
|
|
|
"Definition %d:\n",
|
|
|
|
i+1,
|
|
|
|
)
|
|
|
|
}
|
|
|
|
doc.ToText(os.Stdout, res.Definition, "", "", 80)
|
2015-01-24 02:26:38 -08:00
|
|
|
res.Example = strings.Replace(res.Example, "\r\n", "\n", -1)
|
|
|
|
res.Example = strings.Replace(res.Example, "\n", "\n ", -1)
|
|
|
|
fmt.Printf("\n\n Example:\n %s\n", res.Example)
|
2015-01-23 15:10:12 -08:00
|
|
|
fmt.Printf(
|
2015-01-24 02:19:03 -08:00
|
|
|
"\nSource: %s\nUp: %d, Down: %d\nAuthor: %s\n",
|
2015-01-23 15:10:12 -08:00
|
|
|
res.Link,
|
|
|
|
res.Up,
|
|
|
|
res.Down,
|
|
|
|
res.Author,
|
|
|
|
)
|
|
|
|
}
|
|
|
|
}
|