Instantly share code, notes, and snippets.

Embed
What would you like to do?
Packal.org Alfred Workflow Search
#!/usr/bin/env python
# encoding: utf-8
#
# Copyright © 2014 deanishe@deanishe.net
#
# MIT Licence. See http://opensource.org/licenses/MIT
#
# Created on 2014-03-03
#
"""
Simple search of Packal.org for workflows based on the exported manifest.xml
Uses Alfred-Workflow library:
https://github.com/deanishe/alfred-workflow
"""
from __future__ import print_function, unicode_literals
from datetime import datetime
from operator import itemgetter
try:
from xml.etree import cElementTree as ET
except ImportError:
from xml.etree import ElementTree as ET
from workflow import Workflow, web
MANIFEST_URL = 'https://raw.github.com/packal/repository/master/manifest.xml'
SEARCH_URL = 'http://www.packal.org/search/site/{0}'
WORKFLOW_ICON = '/Applications/Alfred 2.app/Contents/Resources/workflowicon.icns'
def get_workflows():
"""Return list of workflows available on Packal.org"""
workflows = []
r = web.get(MANIFEST_URL)
r.raise_for_status()
manifest = ET.fromstring(r.content)
for workflow in manifest:
d = {}
for elem in workflow:
d[elem.tag] = elem.text
# convert timestamp to datetime
d['updated'] = datetime.fromtimestamp(float(d['updated']))
workflows.append(d)
return workflows
def workflow_key(workflow):
"""Return text search key for workflow"""
# I wish tags were in the manifest :(
elements = [workflow['name']]
elements.append(workflow['author'])
return ' '.join(elements)
def main(wf):
if len(wf.args):
query = wf.args[0]
else:
query = None
workflows = wf.cached_data('workflows', get_workflows, max_age=1200)
workflows.sort(key=itemgetter('updated'), reverse=True)
wf.logger.debug('%d workflows found', len(workflows))
if query:
workflows = wf.filter(query, workflows, key=workflow_key)
wf.logger.debug('%d workflows match query', len(workflows))
else:
wf.add_item('Most recently updated Workflows on Packal.org',
'Hit ENTER on a result to open in your browser',
valid=False,
icon='icon.png')
for workflow in workflows:
subtitle = 'by {0}, updated on {1}'.format(workflow['author'],
workflow['updated'].strftime(
'%d/%m/%Y at %H:%M'))
wf.add_item(workflow['name'],
subtitle,
# Pass bundle ID to Packal.org search
arg=SEARCH_URL.format(workflow['bundle']),
valid=True,
icon=WORKFLOW_ICON
)
wf.send_feedback()
if __name__ == '__main__':
wf = Workflow()
wf.run(main)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment