Writing a command line Zotero client in 9 lines of code

I wrote a small tutorial on how to create a custom Zotero client (in 9 lines of Python code, as the title suggests).

http://www.cogsci.nl/blog/tutorials/97-writing-a-command-line-zotero-client-in-9-lines-of-code

I figured this might be of some use to people who want to access Zotero in a specific way, or anyone who simply enjoys playing around with Zotero (like me)!
  • I get a MySQL error on your post.

    Might be good to put this on GitHub and expand it as the write API gets done.
  • edited January 13, 2011
    Update: Blog online again

    I know :-( My site went down right after I posted it here. I pasted the text below. It's just a third party Python library, btw, it doesn't use the official Zotero api.

    POST:

    I recently received an email asking if there is a command line version of Gnotero, the standalone sidekick to the Zotero reference manager. There is not, but it's extremely easy to create a Python script which accesses the Zotero database.

    To start with, you need the pygnotero Python libraries. If you have installed Gnotero using the .deb installer, you already have these libraries installed. Otherwise, you can get them from the Gnotero source code. You can find the most recent version of Gnotero here. Simply extract the source .tar.gz file and copy the pygnotero folder into the folder that contains your script (or any other location in the Python path).

    So let's begin! First we import the two modules that we are going to use: libzotero (from pygnotero), to access Zotero, and sys, to access arguments passed on the command line.

    1. from pygnotero import libzotero

    2. import sys

    You need to know where your Zotero folder is. Here we assume that it is located in /home/sebastiaan/Zotero, but obviously this depends on your system. If you don't know and have Gnotero installed, you can look in the .gnotero file, which contains a link to the Zotero folder (/home/[user]/.gnotero under Linux or [drive]:\Documents and Settings\[user]\.gnotero under Windows).

    3. zotero_folder = "/home/sebastiaan/Zotero"

    Now we connect to Zotero, using libzotero.

    4. zotero = libzotero.libzotero(zotero_folder)

    We assume that the search term has been specified as the first argument on the command line.

    5. term = sys.argv[1]

    Search!

    6. results = zotero.search(term)

    How many results are there?

    7. print "%d results for %s" % (len(results), term)

    Loop through the results and print them in simple format. libzotero.search() returns a list of zotero_item objects. (For more information about the zotero_item class, see pygnotero.zotero_item in the source code.)

    8. for item in results:

    9. print item.simple_format()

    There you go, a Zotero command line client in 9 lines of code! Of course, this script offers very basic functionality, but it should be enough to get you started writing your own Zotero client. You can download a (slightly) more elaborate example here. (This script also suppresses the unwanted debugging output generated by pygnotero.)
Sign In or Register to comment.