alfred.py 1.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142
  1. # -*- coding: utf-8 -*-
  2. import sys
  3. # the workflow package below is download 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(title=item['title'],
  23. subtitle=get_subtitle(item),
  24. arg=item['id'],
  25. valid=True,
  26. icon='on.png' if on else 'off.png')
  27. # Send the results to Alfred as XML
  28. wf.send_feedback()
  29. if __name__ == '__main__':
  30. my_wf = Workflow()
  31. sys.exit(my_wf.run(main))