Skip to content

Instantly share code, notes, and snippets.

@anjiro
Created March 6, 2017 15:43
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save anjiro/63a88d69491ea518c4670c1a3165748f to your computer and use it in GitHub Desktop.
Save anjiro/63a88d69491ea518c4670c1a3165748f to your computer and use it in GitHub Desktop.
Take selected papers from Papers app and put them on a Trello board. Useful for maintaining reading lists.
#!/usr/bin/env python
import Papers
import trello
proxy_strip = 'PROXY_STRIP_HERE' #This prefix will be stripped from the URL that comes from Papers
api_key = 'YOUR_TRELLO_API_KEY_HERE'
token = 'YOUR_TRELLO_TOKEN_HERE'
org_name = 'YOUR_TRELLO_ORG_NAME_HERE' #Or use "me" if you don't have a Trello org
board_name = 'DEFAULT_BOARD_NAME_HERE'
def papers_to_trello(list_names, board_name=board_name,
org_name=org_name, disable_dup_check=False):
papers = Papers.get_papers(proxy_strip).entries
print("Got {} paper{} from Papers".format(
len(papers),
's' if len(papers) > 1 else ''))
client = trello.TrelloClient(api_key, token=token)
orgs = {org.name: org for org in client.list_organizations()}
try:
if org_name == 'me':
boards = {board.name: board for board in client.list_boards()}
else:
boards = {board.name: board for board in orgs[org_name].all_boards()}
except KeyError:
print u"Can't find %s in %s" % (org_name, orgs.keys())
raise
try:
lists = {tlist.name: tlist for tlist in boards[board_name].all_lists()}
except KeyError:
print u"Can't find %s in %s" % (board_name, boards.keys())
raise
add_lists = []
card_titles = {}
for ln in list_names:
try:
add_lists.append(lists[ln])
if not disable_dup_check:
card_titles[lists[ln]] = [c.name for c in lists[ln].list_cards()]
except KeyError:
print u"Can't find %s in %s" % (ln, lists.keys())
raise
for p in papers:
try:
info = u'{author}, "{title}"'.format(**p)
except KeyError:
print u'Key error for paper:\n{}'.format(p)
raise
print info
for l in add_lists:
if not disable_dup_check and p['title'] in card_titles[l]:
print u' already in list {}'.format(l.name)
continue
print u' -> {}'.format(l.name)
card = l.add_card(p['title'], info)
if p['link']:
card.attach(url=p['link'])
print
if __name__ == "__main__":
import sys, argparse
p = argparse.ArgumentParser(
description='Put selected papers from Papers into Trello')
p.add_argument('-b', default=board_name,
help='Board to post to (default: "%(default)s")')
p.add_argument('-o', default=org_name,
help='Organization to post to (default: "%(default)s")')
p.add_argument('-d', action='store_true',
help="Don't check for duplicate cards with duplicate titles")
p.add_argument('list', help='List to post to', nargs='*')
args = p.parse_args()
if not args.list:
p.print_help()
sys.exit(0)
papers_to_trello(args.list, args.b, args.o, args.d)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment