*Bump* After staying up successfully for well over 24 hours, Decbot3 is now live in the channel. !calc is now in Python instead of Javascript.
public class WolframAlpha {
private const string WolframURL = "http://api.wolframalpha.com/v2/query?appid=<PUT YOUR APPID HERE>&input={0}&format=plaintext";
public static string Calculate(string math) {
math = Uri.EscapeDataString(math);
HttpWebRequest req = (HttpWebRequest)WebRequest.Create(string.Format(WolframURL, math));
XmlDocument doc = new XmlDocument();
using (Stream s = req.GetResponse().GetResponseStream()) {
doc.Load(s);
}
XmlNode root = doc.SelectSingleNode("//queryresult");
bool success;
if (!bool.TryParse(root.Attributes["success"].Value, out success) || !success) {
return "Could not calculate. Perhaps try ~calc.";
}
StringBuilder ret = new StringBuilder();
XmlNodeList pods;
pods = root.SelectNodes("//pod[contains(@primary,'true')]");
if (pods.Count == 0) {
pods = root.SelectNodes("//pod[@id!='Input']"); // and position() < 6]");
}
if (pods.Count == 0) { return "Could not calculate. Perhaps try ~calc."; }
if (pods.Count == 1) { return pods[0].ChildNodes[0].InnerText.Replace("\r\n", " / ").Replace("\n", " / "); }
foreach (XmlNode pod in pods) {
string title = pod.Attributes["title"].Value;
string data = pod.ChildNodes[0].InnerText.Replace("\r\n", " / ").Replace("\n", " / ");
if (string.IsNullOrWhiteSpace(data)) { continue; }
string formattedString = string.Format("{0}: {1} \\ ", title, data);
if (ret.Length + formattedString.Length > 150) { ret.Append("Rest of output too long \\ "); break; }
ret.AppendFormat(formattedString);
}
ret.Length -= 2;
return ret.ToString();
}
}
Advertisement
Advertisement