Skip to content

Instantly share code, notes, and snippets.

Last active Jun 5, 2022
What would you like to do?
Download your Kindle Highlights to local markdown files. Great for
#!/usr/bin/env ruby
# gem install active_support
require 'active_support/inflector'
require 'active_support/core_ext/string'
# gem install webrick (only ruby3)
require 'webrick'
# gem install mechanize
require 'mechanize'
# set this variable to if you are in Canada (or likewise elsewere)
root_url = ENV['KINDLE_URL'] || ""
login = ENV['KINDLE_USER'] || raise("Need KINDLE_USER env var")
pass = ENV['KINDLE_PASS'] || raise("Need KINDLE_PASS env var")
output_dir = if ARGV[0].nil? or ![0])
STDERR.puts "#{File.basename(__FILE__)} [valid output directory]"
m =
m.user_agent_alias = 'Mac Safari'
m.cookie_jar.load("cookies.yaml") if File.exist?("cookies.yaml")
at_exit {"cookies.yaml", session: true) }
response = m.get("#{root_url}/notebook")
if form = response.form("signIn")
# attempt to login
form = response.form("signIn") = ENV['KINDLE_USER']
form.password = ENV['KINDLE_PASS']
response = m.submit(form, form.buttons.first)
if response.title != "Kindle: Your Notes and Highlights"
STDERR.puts "Unexpected page. Additional security or captcha? Check your phone."
file ='/tmp/amazon.html', "w+") do |f|
f << response.body
STDERR.puts "Inspect: #{file.path}"
# Get all books
books ="div#kp-notebook-library") { |child| child.attributes["id"].present? }
# Write each out as formatted markdown file to
books.each do |book|
full_title = title ="h2").first.text.squish
# If the book title has a colon in it we terminate the title there
title = $1 if title =~ /^([^\:]+)\:/
filename = title.strip.gsub(/[^\w]/, '_')
filename = title.parameterize(preserve_case: true, separator: '_')
puts "* writing #{title} to #{filename}.md""#{output_dir}/#{filename}.md", "w+") do |f|
asin = book.attributes["id"].value.squish
author ="p").first.text.split(":").last.strip.squish
# Write markdown header
f.puts "# #{full_title}"
f.puts " by [[#{author}]], asin: [#{asin}](kindle://book?action=openasin=#{asin})"
f.puts " #Book"
highlights = m.get("#{root_url}/kp/notebook?captcha_verified=1&asin=#{asin}&contentLimitState=&")
.select { |child| == "div" }
.select { |child|"").first.present? }
highlights.each do |highlight|
text ="").first.text.squish
location ="input#kp-annotation-location").first.attributes["value"].value
# Write markdown highlights
f.puts "---"
f.print "#{text}"
f.puts " - location: [#{location}](kindle://book?action=open&location=#{location}&asin=#{asin})"
Copy link

jaiamo commented Jan 26, 2021

Here's a python version inspired by your code that works for multiple pages (relevant lines are 76-81, and 99-103):

Also added support for command line flags which let you list all your books, download notes for all your books, or download notes just for a specified title.

Copy link

e-gons commented Feb 5, 2021

This is great. Another tool that gets the job done is Readwise and their Markdown exporter.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment