elnadav12 Posted November 20, 2020 Share Posted November 20, 2020 Hello, After some looking around I found this workflow that uses WolframAlpha's api to get direct search results. https://www.packal.org/workflow/wolframalpha This workflow is very cool but takes ages to load. WolframAlpha has a cool short answer api that returns answer to a url with the question. https://products.wolframalpha.com/short-answers-api/documentation/ Since it's that simple I imagine that making a workflow that fetches the url, for example "http://api.wolframalpha.com/v1/result?appid=DEMO&i={query}", and returns the result as an Alfred title wouldn't be hard to make, and should run fast... What's the right language for the job? PHP? Python? I'm not really sure. I have no experience with both so where should I begin? Thanks for your time! Link to comment
deanishe Posted November 21, 2020 Share Posted November 21, 2020 9 hours ago, elnadav12 said: What's the right language for the job? PHP? Python? I'm not really sure. Either. Or Ruby. Pretty much any general-purpose language has the features you need (URL encoding, HTTP client & JSON). Link to comment
elnadav12 Posted November 21, 2020 Author Share Posted November 21, 2020 1 hour ago, deanishe said: Either. Or Ruby. Pretty much any general-purpose language has the features you need (URL encoding, HTTP client & JSON). Thanks for the help! Is this code any good? Quote require("workflows.php"); $wf = new Workflows(); // $word = "{query}"; $word = str_replace(' ', '+', "{query}"); $url = "http://api.wolframalpha.com/v1/result?appid=DEMO&i=".$word; // replace file_get_contents with $wf->request // it's not better, it's cleaner IMHO $answer = $wf->request( $url ); // $raw = file_get_contents( $url ); $wf->result( time(), $url, $answer, 'WolframAlpha search result', 'icon.png', 'yes', $url); echo $wf->toxml(); Link to comment
vitor Posted November 21, 2020 Share Posted November 21, 2020 (edited) Make a Script Filter with ruby as the language and this code: require 'cgi' require 'json' require 'net/http' query = ARGV[0] escaped_query = CGI.escape(query) simple_api = 'https://api.wolframalpha.com/v1/simple?i=' + escaped_query + '&appid=' + ENV['api_key'] short_answers_api = 'https://api.wolframalpha.com/v1/result?i=' + escaped_query + '&appid=' + ENV['api_key'] result = Net::HTTP.get_response(URI.parse(short_answers_api)).body puts({ items: [ title: result, subtitle: query, arg: result, quicklookurl: simple_api ] }.to_json) Results are inline and you can press ⇧ or ⌘Y to get a quicklook preview of the page. It assumes you’ve got an API key and placed it in an api_key Workflow Environment Variable. Edited November 21, 2020 by vitor Link to comment
elnadav12 Posted November 21, 2020 Author Share Posted November 21, 2020 1 minute ago, vitor said: Make a Script Filter with ruby as the language and this code: require 'cgi' require 'json' require 'net/http' query = ARGV[0] escaped_query = CGI.escape(query) simple_api = 'https://api.wolframalpha.com/v1/simple?i=' + escaped_query + '&appid=' + ENV['api_key'] short_answers_api = 'https://api.wolframalpha.com/v1/result?i=' + escaped_query + '&appid=' + ENV['api_key'] result = Net::HTTP.get_response(URI.parse(short_answers_api)).body puts({ items: [ title: result, subtitle: query, arg: result, quicklookurl: simple_api ] }.to_json) Results are inline and you can press ⇧ or ⌘Y to get a quicklook preview of the page. It assumed you’ve got an API key and placed it in a api_key Workflow Environment Variable. Thanks a lot! The API is limited to 2000 calls a month, will this script call the api each time I write a new letter? Link to comment
vitor Posted November 21, 2020 Share Posted November 21, 2020 Just now, elnadav12 said: The API is limited to 2000 calls a month, will this script call the api each time I write a new letter? Up to you. In the Script Filter click Run Behaviour and define Queue Delay however you prefer. Link to comment
vitor Posted November 21, 2020 Share Posted November 21, 2020 1 minute ago, elnadav12 said: Placed the API key instead of api_key I have no idea what that means. Don’t change the code, just add a Workflow Environment Variable named api_key and set the Value to your API key. Link to comment
elnadav12 Posted November 21, 2020 Author Share Posted November 21, 2020 14 minutes ago, vitor said: I have no idea what that means. Don’t change the code, just add a Workflow Environment Variable named api_key and set the Value to your API key. Ohh I see... Thanks a lot! Great workflow for conversions, stock prices and quick general search. Link to comment
Recommended Posts
Create an account or sign in to comment
You need to be a member in order to leave a comment
Create an account
Sign up for a new account in our community. It's easy!
Register a new accountSign in
Already have an account? Sign in here.
Sign In Now