Skip to content

Instantly share code, notes, and snippets.

@drnikki drnikki/
Last active Dec 18, 2017

What would you like to do?
# -*- coding: utf-8 -*-
# Replace this string with your API key.
api_key = '----'
# Replace this string with your Library ID
library_id = ----
library_type = 'user'
# Import the libraries we'll need and initialize
from HTMLParser import HTMLParser
from pyzotero import zotero
import pprint # for testing because I'm bad at python.
pp = pprint.PrettyPrinter(indent=4)
zot = zotero.Zotero(library_id, library_type, api_key)
htmlparser = HTMLParser()
#items =
# params
# This is configured to search the library for all items with a given tag.
# Every item with this tag will be included in the bibliography
# You can modify this by changing the search parameter. There is a great
# reference in the PyZotero docs:
items = zot.items(tag='592-ethics')
# items() returns a list of dicts, but we need a dict of dicts because we're going
# to be doing two searches and merging the results
allItems = dict()
# Put them into a dict keyed by their zotero key.
for item in items:
# item[data] entries look like this:
allItems[item['data']['key']] = item['data']
# if you want to see just item types and their keys, you can run:
# print('Item: %s | Key: %s') % (item['data']['title'], item['data']['key'])
# Make a second API call. This time, grab all notes tagged "annotation."
# This works because in my library Items and Notes
# don't share tags, so I know that _only_ Notes will be returned.
items = zot.items(tag='annotation')
# Do the same thing so that we have a dict of notes. These two dicts will have
# items sharing the same item key. This is how we'll match the note to the item.
allNotes = dict()
for item in items:
allNotes[item['data']['parentItem']] = item['data']
# This is NOT performant AT ALL. I give zero fs about this fact.
# I am very sorry for the Zotero API, but these aren't _too_ big. 2 + #of item API calls per run
# For each Item, we're going to go grab the formatted citation
# and then if there's a corresponding Annotation, append that.
# At this point, we have two dicts (or associative arrays, if you come from PHP land)
# one with all items in it, and one with all notes in it. We're going to go
# BACK to the zotero API to get a formatted citation.
bib = dict()
for key, item in allItems.iteritems():
bib[key] = dict()
# this adds the formatted citation
bib[key]['item'] = zot.item(key)
if key in allNotes:
# this attaches the note to the item.
bib[key]['note'] = allNotes[key]['note']
# ----- fancy HTML part -----
import dominate
from dominate.tags import *
doc = dominate.document(title='Annotated Bibliography')
with doc.head:
link(rel='stylesheet', href='styles.css')
# in the HTML document
with doc:
# for each entry that we created above:
for key, entry in bib.iteritems():
# we're going to make a div to hold the whole thing
with div(id='one-entry'):
# then another div to hold the citation
# then, if there's a note, add that too.
if 'note' in entry:
#p(entry['note']) <---- this is the ishhhhhh
with div(id='header').add(ol()):
for i in ['home', 'about', 'contact']:
li(a(i.title(), href='/%s.html' % i))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.