Displaying Kindle clippings for the web
Tags: programming, projects
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
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: blocks.append() else: blocks[-1].append(line) blocks.pop() return blocks def splitClipping(clipping): publication = clipping quote = "" for line in clipping[2:]: quote = quote + line return publication, quote if __name__ == "__main__": print("Content-type: text/html; charset=UTF-8") print() 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) print(''' <html> <head> <title>Spark: A random quote from Kindle clippings</title> </head> <body>''') print(''' <p> <blockquote> %s </blockquote> </p>''' % cgi.escape(quote)) print(''' <p style="text-align:right"> — %s </p> ''' % cgi.escape(publication)) print(''' </body> </html>''')