aboutsummaryrefslogtreecommitdiff
path: root/src/hooks/wa.rs
diff options
context:
space:
mode:
authorLorenz Leitner <lrnz.ltnr@gmail.com>2021-10-10 14:16:03 +0200
committerLorenz Leitner <lrnz.ltnr@gmail.com>2021-10-12 12:06:57 +0200
commitf964e2c3c2338434b57b6adf25dfbc346bb89b33 (patch)
tree6c5685be99b79b80d7e89a4574887af770b23b23 /src/hooks/wa.rs
parentadd yaml file sepperate to ci file (diff)
Basic (re)implementation of gonzobot wolfram alpha
Diffstat (limited to '')
-rw-r--r--src/hooks/wa.rs84
1 files changed, 84 insertions, 0 deletions
diff --git a/src/hooks/wa.rs b/src/hooks/wa.rs
new file mode 100644
index 0000000..b6d4eff
--- /dev/null
+++ b/src/hooks/wa.rs
@@ -0,0 +1,84 @@
+use anyhow::{bail, Context, Error, Result};
+use reqwest::{get, Url};
+use serde::{Deserialize, Serialize};
+use serde_json::Result as SerdeJsonResult;
+use tracing::trace;
+
+#[derive(Serialize, Deserialize, Debug)]
+struct WaResult {
+ queryresult: QueryResult,
+}
+
+#[derive(Serialize, Deserialize, Debug)]
+struct QueryResult {
+ pods: Vec<Pod>,
+}
+
+#[derive(Serialize, Deserialize, Debug)]
+struct Pod {
+ title: String,
+ id: String,
+ subpods: Vec<SubPod>,
+}
+
+#[derive(Serialize, Deserialize, Debug)]
+struct SubPod {
+ title: String,
+ plaintext: String,
+}
+
+fn parse_json(str_data: &str) -> SerdeJsonResult<WaResult> {
+ let w: WaResult = serde_json::from_str(str_data)?;
+ Ok(w)
+}
+
+#[tracing::instrument]
+async fn wa_query(query_str: &str) -> Result<String, Error> {
+ let app_id = "XXX"; // TODO: Get from env
+ let api_url = format!(
+ "http://api.wolframalpha.com/v2/query?input={}&appid={}&output=json",
+ query_str, app_id
+ );
+
+ let body = get(Url::parse(&api_url).context("Failed to parse url")?)
+ .await
+ .context("Failed to make request")?
+ .text()
+ .await
+ .context("failed to get request response text")?;
+
+ let full_wa_res = parse_json(&body)?;
+ trace!("got full_wa_res: {:?}", full_wa_res);
+
+ let pod_plaintexts = full_wa_res.queryresult.pods
+ .iter()
+ .filter(|it| it.id.to_lowercase() != "input")
+ .map(|pod| {
+ let subpod_texts = pod
+ .subpods
+ .iter()
+ .map(|subpod| subpod.plaintext.clone())
+ .collect::<Vec<String>>()
+ .join(", ");
+
+ format!("{}: {}", &pod.title, subpod_texts)
+ })
+ .collect::<Vec<String>>()
+ .join(" - ");
+
+ Ok(pod_plaintexts)
+}
+
+#[cfg(test)]
+mod tests {
+
+ use super::wa_query;
+ use anyhow::{Error, Result};
+
+ #[tokio::test]
+ // async fn test_query_result_json_parsing() -> Result<(), Error> {
+ async fn test_query_result_json_parsing() {
+ let res = wa_query("weather graz").await.unwrap();
+ assert_eq!(res, "asdf");
+ }
+}
t/src/hooks/wolfram_alpha.rs?id=b6350162b4c70abb896613e4ebea65ca1661450d&follow=1'>update dependenciesMax Audron-691/+1659 2024-08-12add nix build and moduleMax Audron-5/+427 2022-02-19remove jsonnet lock fileMax Audron-36/+0 2022-02-19fix deploy to work with gitlab agentMax Audron-4/+4 2021-10-22write tons of documentation and reorganize some modulesMax Audron-65/+300 2021-10-22remove wolfram alpha url shorteningMax Audron-1/+2 2021-10-20remove failing wolfram alpha test casesMax Audron-105/+55 this is due to the url shorterner dying randomly and also just generally bad idea to call external services during unit tests. 2021-10-20bump version to 1.6.2Max Audron-3/+2 2021-10-20prepare for release on crates.ioMax Audron-39/+65 2021-10-20add async docs to macro crate and bump versionMax Audron-9/+10 2021-10-20change hook errors to be logged as warningsMax Audron-3/+3 they in nearly all cases aren't critical enough to warrant an actual error messages 2021-10-20fix configuration not loading correctly on release buildsMax Audron-8/+23 2021-10-19replace sedregex crate8-rework-sedMax Audron-20/+358 This replaces the sedregex crate with our own implementation for multiple reasons: 1. We required to access the parsed regex, this required a patch to the sedregex crate which did not get merged due to an inactive dev, blocking us from publishing on crates.Io 2. We wanted to highlight the changes done in bold 3. We want to add execution of multiple chained sed commands in the future which would require more modification 2021-10-19add formatting trait for irc codesMax Audron-0/+129 add an impl off the formatting trait on String to format Strings with the typical irc formatting codes for bold, italic etc 2021-10-17fix links in readmeMax Audron-2/+2