Jump to content

Filtering feedback with Alfred [Python]


Recommended Posts

Hi all,

 

I've been working on a Alfred workflow to display Basecamp projects. So far so good! i've got all the results being pulled and displayed by feedback.

 

My problem is how do I use {query} to filter the results? for example if I type 'bc' it lists all the projects, I want to be able to type for example 'bc arg' and for it to filter the results based on query, so i'd get results beginning with 'arg' (pretty basic I know!).

 

I don't have much experience with Python of Alfred so any help would be appreciated!

 

See below the python code and the full workflow also:

#!/usr/bin/env python

import requests
import alfred
import os.path

request = requests.get('https://basecamp.com/XXXXXXX/api/v1/projects.json', auth=("USERNAME", PASSWORD'))
query = alfred.argv(1)

alfred.log(str('QUERY: '+query))

try:
    set_cache = alfred.cache.set('cache', request.json())
    cache = alfred.cache.get('cache')
except IOError:
    alfred.log('IOError!')

feedback = alfred.Feedback()


def projects():
    """Function to receive json and output Alfred feedback"""

    i = 0

    for list in cache:

        # define attribute locations as variables
        name = cache[i]['name']
        id = cache[i]['id']
        url = cache[i]['url']
        starred = cache[i]['starred']
        desc = cache[i]['description']

        # manipulating the string to remove 'api', 'v1' and '.json'
        url = url.split('/')
        del url[4]
        del url[4]
        url = '/'.join(url)
        url = os.path.splitext(url)[0]

        feedback.addItem(title=name, subtitle=desc, uid=('bc' + str(id)), valid=True, icon="basecamp.png", arg=url)
        alfred.log('FEEDBACK: ' + name)

        i += 1

    feedback.output()

projects()

alfred.exit('EXIT')
alfred.log('EXIT')

As you can see I'm using the rather wonderful Alfred-Python library!

 

http://cl.ly/Ovrf

 

Thanks in advance!

Edited by LkeMitchll
Link to comment

Although I don't know Alfred-Python, and so I'm not quite sure what the data you get from cache looks like, I think you just need to do something like this:

name = cache[i]['name']
desc = cache[i]['description']

if query in name or query in desc:
    # do your string manipulation
    feedback.addItem(title=name, subtitle=desc, uid='bc' + str(id), valid=True, icon='basecamp.png', arg=url)

feedback.output()

My own Alfred module, alp, contains a fuzzy searching method contributed by Github user jlegewie that makes the searching a bit smoother, but it's hard to see how it would be implemented here without knowing more about the data you get back from cache—the iterator is throwing me for, as it were, a loop.

Link to comment

Although I don't know Alfred-Python, and so I'm not quite sure what the data you get from cache looks like, I think you just need to do something like this:

name = cache[i]['name']
desc = cache[i]['description']

if query in name or query in desc:
    # do your string manipulation
    feedback.addItem(title=name, subtitle=desc, uid='bc' + str(id), valid=True, icon='basecamp.png', arg=url)

feedback.output()

My own Alfred module, alp, contains a fuzzy searching method contributed by Github user jlegewie that makes the searching a bit smoother, but it's hard to see how it would be implemented here without knowing more about the data you get back from cache—the iterator is throwing me for, as it were, a loop.

 

Thanks for the reply, cache returnS simple JSON. I might try importing the fuzzy search from alp! how would you use it to manipulate JSON? e.g.

[
   {
      u'archived':False,
      u'description':u'Wordpress site for the church project',
      u'url':      u'https://basecamp.com/1818846/api/v1/projects/285717-xxxxxxx.json',
      u'created_at':      u'2012-04-11T08:25:59.000      Z',
      u'last_event_at':      u'2013-05-02T11:12:27.000      Z',
      u'updated_at':      u'2013-05-02T11:12:27.000      Z',
      u'starred':False,
      u'id':285717,
      u'name':u'xxxxxxxxxxxxxxx'
   },
   {
      u'archived':False,
      u'description':u'Lemonstand site',
      u'url':      u'https://basecamp.com/1818846/api/v1/projects/xxxxxxxxxxxx.json',
      u'created_at':      u'2012-07-26T08:51:14.000      Z',
      u'last_event_at':      u'2013-05-14T15:18:06.000      Z',
      u'updated_at':      u'2013-05-14T15:18:06.000      Z',
      u'starred':False,
      u'id':834090,
      u'name':u'xxxxxxxxxxxxx'
   }
]
Edited by LkeMitchll
Link to comment

The documentation explains it in more detail, but with alp you wind up giving it a list—which you have—and a key function—which accesses a search string for each element in the list. If you wanted to fuzzily search names and descriptions, for example, you'd do this:

alp.fuzzy_search(query, cache, key=lambda x: "{0} - {1}".format(x['name'], x['description']))

I should also note that your original code has the potential to generate unreliable results. There's no guarantee, in Python, that the for…in loop is moving through things sequentially, so iterating i within it could break it under certain circumstances. I think you could replace it with something like this, if that's the JSON form of the objects you're getting:

for list in cache:
    name= list['name']
    id = list['id']
    # etc.
Link to comment

 

The documentation explains it in more detail, but with alp you wind up giving it a list—which you have—and a key function—which accesses a search string for each element in the list. If you wanted to fuzzily search names and descriptions, for example, you'd do this:

alp.fuzzy_search(query, cache, key=lambda x: "{0} - {1}".format(x['name'], x['description']))

I should also note that your original code has the potential to generate unreliable results. There's no guarantee, in Python, that the for…in loop is moving through things sequentially, so iterating i within it could break it under certain circumstances. I think you could replace it with something like this, if that's the JSON form of the objects you're getting:

for list in cache:
    name= list['name']
    id = list['id']
    # etc.

 

Thanks for this! I'm using the fuzzy search from your module.

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