Displaying Kindle clippings for the web

Tags: programming, projects

Published on
« Previous post: Return value optimization in C++ — Next post: All hail Octocat! »

Since I got my Kindle, the number of quotes in my quote data base started to increase exponentially. Clicking “Highlight” to save an interesting passage is so tempting. Now, a few months later, I of course wanted some way to display this treasure trove. I basically wanted a version of the good old fortune command, but a bit more modern—for those Web 2.0 times we are living in.

Luckily, the clippings format is very straightforward and only involves a little bit of text parsing. Below you can find my first feeble attempt at displaying a random quote from my clippings file. To use the script, you first need your clippings file. You can usually find it under the root directory of wherever your Kindle is mounted, for example Kindle/documents/My Clippings.txt. Rename the file to Clippings.txt. Now place the script below and the clippings file in a folder that is being served by a web server that enables you to execute CGI scripts. And you are done.

Since the script is barely doing anything, I am releasing this version into the public domain. I would love to provide an example installation for the script but I am currently migrating between servers, so I do not want to dabble with any CGI scripts as well. I will write a follow-up post if the script becomes more polished.

Happy reading until then.

#!/usr/bin/env python3

import cgi
import random

def readClippings(filename):
  separator = "=========="
  blocks    = []

  with open(filename) as f:
    for line in f:
      line = line.rstrip()
      line = line.replace("\ufeff", "") # Replace broken BOM

      if line == separator:

  return blocks

def splitClipping(clipping):
  publication = clipping[0]
  quote       = ""

  for line in clipping[2:]:
    quote = quote + line

  return publication, quote

if __name__ == "__main__":

  print("Content-type: text/html; charset=UTF-8")

  clippings = readClippings("Clippings.txt")
  clippings = [splitClipping(x) for x in clippings]
  clippings = [(x,y) for x,y in clippings if y]

  publication, quote = random.choice(clippings)

  <title>Spark: A random quote from Kindle clippings</title>

</p>''' % cgi.escape(quote))

<p style="text-align:right">
  &mdash; %s
''' % cgi.escape(publication))