Example #1
0
func urban(command *bot.Cmd, matches []string) (msg string, err error) {
	var i int64 = 0
	if matches[1] != "" {
		i, _ = strconv.ParseInt(matches[1], 10, 64)
		i = i - 1
	}

	results := &DefinitionResults{}
	err = web.GetJSON(fmt.Sprintf(urbanURL, url.QueryEscape(matches[2])), results)
	if err != nil {
		return fmt.Sprintf("Urban Dictionary | %s | (No definition found)", matches[2]), nil
	}
	if results.ResultType == "no_results" {
		return fmt.Sprintf("Urban Dictionary | %s | (No definition found)", matches[2]), nil
	}
	if int(i+1) > len(results.List) {
		return fmt.Sprintf("Urban Dictionary | %s | (No definition found)", matches[2]), nil
	}

	word := results.List[i].Word
	definition := results.List[i].Definition
	permalink := results.List[i].Permalink
	short := web.ShortenURL(permalink)

	reg := regexp.MustCompile("\\s+")
	definition = reg.ReplaceAllString(definition, " ") // Strip tabs and newlines

	if len(definition) > 180 {
		definition = fmt.Sprintf("%s...", definition[0:180])
	}

	output := fmt.Sprintf("Urban Dictionary | %s | %s | %s", word, short, definition)

	return output, nil
}
Example #2
0
func wolfram(command *bot.Cmd, matches []string) (msg string, err error) {
	doc, _ := http.Get(fmt.Sprintf(wolframURL, bot.Config.Wolfram, url.QueryEscape(matches[1])))
	defer doc.Body.Close()
	root, err := xmlpath.Parse(doc.Body)

	if err != nil {
		return "Wolfram | Stephen Wolfram doesn't know the answer to this", nil
	}

	short := web.ShortenURL(fmt.Sprintf("https://www.wolframalpha.com/input/?i=%s", url.QueryEscape(matches[1])))

	success := xmlpath.MustCompile("//queryresult/@success")
	input := xmlpath.MustCompile("//pod[@position='100']//plaintext[1]")
	output := xmlpath.MustCompile("//pod[@position='200']/subpod[1]/plaintext[1]")

	suc, _ := success.String(root)

	if suc != "true" {
		return fmt.Sprintf("Wolfram | Stephen Wolfram doesn't know the answer to this | %s", short), nil
	}

	in, _ := input.String(root)
	out, _ := output.String(root)

	in = strings.Replace(in, `\:`, `\u`, -1)
	out = strings.Replace(out, `\:`, `\u`, -1)

	reg := regexp.MustCompile("\\s+")
	in = reg.ReplaceAllString(in, " ")
	out = reg.ReplaceAllString(out, " ")

	in, _ = strconv.Unquote(`"` + in + `"`)
	out, _ = strconv.Unquote(`"` + out + `"`)

	return fmt.Sprintf("Wolfram | %s >>> %s | %s", in, out, short), nil
}