示例#1
0
func Google(result chan string, mes string) {
	// catch errors
	defer func() {
		if r := recover(); r != nil {
			log.Log.Warning("Google Input: " + mes)
			log.Log.Warning("Google Error: " + fmt.Sprint(r))
			result <- "There was an error while trying to process that command."
		}
	}()

	res := GoogReg.FindAllStringSubmatch(mes, -1)
	input := strings.TrimSpace(res[0][2])

	fullUrl, err := url.Parse(googUrl + input)
	if err != nil {
		panic(err)
		return
	}

	str := input + " | " + util.GetShortURL(fullUrl.String())

	log.Log.Info("Google Input: " + mes)
	log.Log.Info("Google Output: " + str)

	result <- str
	return
}
示例#2
0
func ShortenURL(result chan string, mes string) {
	// catch errors
	defer func() {
		if r := recover(); r != nil {
			log.Log.Warning("Shorten URL Input: " + mes)
			log.Log.Warning("Shorten URL Error: " + fmt.Sprint(r))
			result <- "There was an error while trying to process that command."
		}
	}()

	res := ShortenReg.FindAllStringSubmatch(mes, -1)
	input := strings.TrimSpace(res[0][2])

	str := input + " | " + util.GetShortURL(input)

	log.Log.Info("Shorten URL Input: " + mes)
	log.Log.Info("Shorten URL Output: " + str)

	result <- str
	return
}
示例#3
0
func WolframQuery(result chan string, mes string) {
	// catch errors
	defer func() {
		if r := recover(); r != nil {
			log.Log.Warning("Wolfram Input: " + mes)

			log.Log.Warning("Wolfram Error: " + fmt.Sprint(r))
			result <- "I'm sorry, I couldn't find the answer to that query."
		}
	}()

	res := WolfReg.FindAllStringSubmatch(mes, -1)
	input := strings.TrimSpace(res[0][1])

	body := util.GetDataFromURL(
		wolframUrl,
		[]string{wolframBasePath},
		map[string]string{
			"input": input,
			"appid": config.GetConfig().ApiInfo.Wolfram,
		})

	var queryResult QueryResult
	err := xml.Unmarshal(body, &queryResult)
	if err != nil {
		panic(err)
	}

	var subpod *SubPod = nil
	var visualPod *Pod = nil
	var convPod *Pod = nil
	for _, pod := range queryResult.Pods {
		if pod.Title == "Result" || pod.Title == "Current result" || pod.Title == "Results" ||
			pod.Title == "Average result" || pod.Title == "Value" || pod.Title == "Alternate forms" ||
			pod.Title == "Solution" || pod.Title == "Alternate form" || pod.Title == "Plot" {
			subpod = &pod.SubPods[0]
		}
		if pod.Title == "Visual representation" || strings.HasPrefix(pod.Title, "Plots") {
			// for range copies values, so we can't use memory location here
			visualPod = new(Pod)
			visualPod.Title = pod.Title
			visualPod.SubPods = pod.SubPods
		}
		if pod.Title == "Unit conversions" {
			convPod = new(Pod)
			convPod.Title = pod.Title
			convPod.SubPods = pod.SubPods
		}
	}
	if subpod == nil {
		subpod = &queryResult.Pods[0].SubPods[0]
	}

	text := newLnReg.ReplaceAllString(html.UnescapeString(subpod.Text.Value), " ")
	img := util.GetShortURL(subpod.Img.Src)

	str := text + " | " + img

	append := func(pod *Pod) {
		for _, subpod := range pod.SubPods {
			subText := newLnReg.ReplaceAllString(html.UnescapeString(subpod.Text.Value), " ")
			subImg := util.GetShortURL(subpod.Img.Src)
			if strings.TrimSpace(subText) != "" {
				str += " | " + subText
			}
			if strings.TrimSpace(subImg) != "" {
				str += " | " + subImg
			}
		}
	}

	if visualPod != nil {
		append(visualPod)
	}
	if convPod != nil {
		append(convPod)
	}

	log.Log.Info("Wolfram Input: " + mes)
	log.Log.Info("Wolfram Output: " + str)

	result <- str
	return
}