Jump to content

[SOLVED] WolframAlpha api workflow help


Recommended Posts

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 post
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 post
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 post

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 by vitor
Link to post
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 post
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 post
  • vitor changed the title to [SOLVED] WolframAlpha api workflow help

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 account

Sign in

Already have an account? Sign in here.

Sign In Now
×
×
  • Create New...