From 37c06825ba112d16a1f7d7c572195eb3dcf414f2 Mon Sep 17 00:00:00 2001 From: Maddison Hellstrom Date: Sat, 21 Oct 2017 14:28:26 -0700 Subject: [PATCH] Add WolframAlpha completion --- conf.js | 82 ++++++++++++++++++++++++++++++++++++++++++++ conf.priv.example.js | 11 +++--- 2 files changed, 88 insertions(+), 5 deletions(-) diff --git a/conf.js b/conf.js index 31e35ec..7596d56 100644 --- a/conf.js +++ b/conf.js @@ -180,6 +180,88 @@ var search = [ Omnibar.listWords(JSON.parse(response.text)); } }, + { alias: 'wa' + , name: 'wolframalpha' + , search: 'http://www.wolframalpha.com/input/?i=' + , compl: `http://api.wolframalpha.com/v2/query?appid=${keys.wolframalpha}&format=plaintext&output=json&reinterpret=true&input=%s` + , callback: function(response) { + var res = JSON.parse(response.text).queryresult; + + if (res.error) { + Omnibar.listResults([""], function() { + var li = $('
  • ').html(` +
    +
    Error (Code ${res.error.code})
    +
    ${res.error.msg}
    +
    + `); + return li; + }); + return; + } + + if (!res.success) { + if (res.tips) { + Omnibar.listResults([""], function() { + var li = $('
  • ').html(` +
    +
    No Results
    +
    ${res.tips.text}
    +
    + `); + return li; + }); + } + if (res.didyoumeans) { + Omnibar.listResults(res.didyoumeans, function(s) { + var li = $('
  • ').html(` +
    +
    Did you mean...?
    +
    ${s.val}
    +
    + `); + return li; + }); + } + return; + } + + var results = []; + res.pods.map(function(p){ + var result = { + title: p.title, + values: [], + url: "http://www.wolframalpha.com/input/?i=", + }; + if (p.numsubpods > 0) { + result.url += encodeURIComponent(p.subpods[0].plaintext); + p.subpods.map(function(sp) { + if (!sp.plaintext) return; + var v = ""; + if (sp.title) { + v += `${sp.title}: `; + } + v += sp.plaintext; + result.values.push(`
    ${v}
    `); + }); + } + if (result.values.length > 0) { + results.push(result); + } + }); + + Omnibar.listResults(results, function(r) { + var li = $('
  • ').html(` +
    +
    ${r.title}
    + ${r.values.join("\n")} +
    + `); + li.data('url', r.url); + return li; + }); + } + }, { alias: 'co' , name: 'crunchbase-orgs' , search: 'https://www.crunchbase.com/app/search/?q=' diff --git a/conf.priv.example.js b/conf.priv.example.js index b938154..b2ef401 100644 --- a/conf.priv.example.js +++ b/conf.priv.example.js @@ -21,9 +21,10 @@ // You will need to modify the code in conf.js to work with the Mashape API if desired. // Open an issue if you can't figure this out and I'll help out. var keys = - { crunchbase: "foo" // https://about.crunchbase.com/crunchbase-basic-access/ - , domainr: "bar" // https://domainr.build/docs/overview#section-try-it-for-free - , google_ex: "qux" // https://developers.google.com/custom-search/json-api/v1/overview?hl=en_US - , google_ex_cx: "ham" // This is the Search Engine ID (cx) from your Google Custom Search - , google_yt: "spam" // https://developers.google.com/youtube/v3/docs/ + { crunchbase: "foo" // https://about.crunchbase.com/crunchbase-basic-access/ + , domainr: "bar" // https://domainr.build/docs/overview#section-try-it-for-free + , google_ex: "qux" // https://developers.google.com/custom-search/json-api/v1/overview?hl=en_US + , google_ex_cx: "ham" // This is the Search Engine ID (cx) from your Google Custom Search + , google_yt: "spam" // https://developers.google.com/youtube/v3/docs/ + , wolframalpha: "lorem" // https://products.wolframalpha.com/api/ };