alfred.py 1.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344
  1. # -*- coding: utf-8 -*-
  2. import sys
  3. # the workflow package below can be downloaded from:
  4. # https://github.com/deanishe/alfred-workflow/releases
  5. from workflow import Workflow, ICON_WEB, web
  6. def get_subtitle(item):
  7. content = item.get('content', '')
  8. return content.partition('\n')[0].strip()
  9. def main(wf):
  10. url = 'http://127.0.0.1:50761/api/list'
  11. r = web.get(url)
  12. # throw an error if request failed
  13. # Workflow will catch this and show it to the user
  14. r.raise_for_status()
  15. # Parse the JSON returned by pinboard and extract the posts
  16. result = r.json()
  17. items = result['data']
  18. # Loop through the returned posts and add an item for each to
  19. # the list of results for Alfred
  20. for item in items:
  21. on = item.get('on', False)
  22. wf.add_item(
  23. title=item.get('title', 'untitled'),
  24. subtitle=get_subtitle(item),
  25. arg=item.get('id'),
  26. valid=True,
  27. icon='on.png' if on else 'off.png',
  28. )
  29. # Send the results to Alfred as XML
  30. wf.send_feedback()
  31. if __name__ == '__main__':
  32. my_wf = Workflow()
  33. sys.exit(my_wf.run(main))