Example #1
0
func GetWeather(result chan string, mes string) {
	// catch errors
	defer func() {
		if r := recover(); r != nil {
			log.Log.Warning("Weather Input: " + mes)
			log.Log.Warning("Weather Error: " + fmt.Sprint(r))
			result <- "I'm sorry, I couldn't find that location."
		}
	}()

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

	body := util.GetDataFromURL(weatherUrl, []string{
		weatherBasePath,
		config.GetConfig().ApiInfo.Weather,
		weatherSecondPath,
		input,
		".json",
	}, map[string]string{})

	var jsonDat map[string]interface{}
	err := json.Unmarshal(body, &jsonDat)
	if err != nil {
		panic(err)
	}

	currentObs := jsonDat["current_observation"].(map[string]interface{})
	displayLoc := currentObs["display_location"].(map[string]interface{})

	city := displayLoc["city"].(string)
	state := displayLoc["state"].(string)

	temp := currentObs["temperature_string"].(string)
	humidity := currentObs["relative_humidity"].(string)
	wind := currentObs["wind_string"].(string)
	conditions := currentObs["weather"].(string)
	lastUpdate := currentObs["observation_time"].(string)

	str := "Current weather for " + city + ", " + state + ": " + conditions + " with a temperature of " + temp + " and winds " + wind + ". Humidity is " + humidity + ". " + lastUpdate

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

	result <- str
	return
}
Example #2
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
}