Jump to content

Multiple results in script filter [Python]


Recommended Posts

If you want to add multiple results, just call wf.add_item() multiple times.

It's important to note that when you call wf.send_feedback() the workflow is finished as far as Alfred is concerned. You cannot send more results after that.

You also don't need all that urllib/urllib2/json crap. Alfred-Workflow has its own much more pleasant-to-use HTTP library (modelled on requests):

from workflow import web

r = web.get('http://receitaws.com.br/v1/cnpj/' + query)
r.raise_for_status() # Throw an exception if the web request failed
data = r.json()
output = data['nome']
…
…
Link to comment

Thank you deanishe! In fact on my attempts I called wf.send_feedback() for every wf.add_item(). That was the reason I've got just one result.


 


Besides that, could you offer an explanation for dummies on how r.raise_for_status() works? Thanks again!


Edited by xilopaint
Link to comment

Thank you deanishe! In fact on my attempts I called wf.send_feedback() for every wf.add_item(). That was the reason I've got just one result.

 

Besides that, could you offer a explanation for dummies on how r.raise_for_status() works? Thanks again!

 

If a web request fails (no Internet connection, server error, file not found etc. etc.), instead of exploding immediately, web.py stores the error on the Response object.

 

raise_for_status() simply re-raises the stored error if there is one. This is normally the behaviour you want (if the request failed, you have no data). But you might be doing something else, like checking a bunch of URLs to ensure they're valid (i.e. not 404). In that case, you don't care whether the request succeeded or not, you just need the HTTP status code.

 

The reason for implementing it this way is simply because that's what requests does, and web.py is basically a very lightweight clone of requests to cover the most common use cases without adding megabytes to the size of your workflow (requests is a big library). The idea is that you can replace web.py pretty easily with requests if you need a more full-featured HTTP library.

 

Thanks for the beer money! I'll spend that tonight on drowning my sorrows now my idiot countryfolk have voted to leave the EU and screwed me royally :(

Link to comment
Sorry. That's a bug. I left a print statement in there while I was trying to debug a problem, and the unit tests didn't pick it up :(

 

I've just released a new version (1.18.1) that should fix the problem. Again, very sorry about that and thanks for spotting the bug!

Edited by deanishe
Link to comment

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