Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add and Example Script #20

Open
wants to merge 14 commits into
base: master
Choose a base branch
from
15 changes: 13 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ kindle.books
]
```

Each `Book` object has it's `asin`, `author`, and `title` as attributes:
Each `Book` object has it's `asin`, `author`, `title`, `page` and `note` as attributes:

```ruby
book = kindle.books.first
Expand All @@ -65,6 +65,10 @@ book.author
#=> "James R. Mcdonough"
book.title
#=> "Platoon Leader: A Memoir of Command in Combat"
book.page
#=> "Page: 7"
book.note
#=> "This is a note!"
```

### Fetching all highlights for a single book
Expand All @@ -81,11 +85,13 @@ kindle.highlights_for("B005CQ2ZE6")
@asin="B005CQ2ZE6",
@text="One of the most dangerous things you can believe in this world is that technology is neutral.",
@location="197"
@page="Page:7"
@note="This is a note"
>
]
```

Each `Highlight` object has the book's `asin`, the `text` of the highlight, and it's `location` as attributes:
Each `Highlight` object has the book's `asin`, the `text` of the highlight, the `page` number if available (it returns "Location: xx" if not available), it's `location`, and any `note` associated with the highlight (this will return _null_ if there is no note) as attributes:

```ruby
highlight = kindle.highlights_for("B005CQ2ZE6").first
Expand All @@ -94,8 +100,12 @@ highlight.asin
#=> "B005CQ2ZE6"
highlight.text
#=> "One of the most dangerous things you can believe in this world is that technology is neutral."
highlight.page
#=> "Page: 7"
highlight.location
#=> "197"
highlight.note
#=> "This is a note"
```

Additionally, each book has it's own `highlights_from_amazon` method:
Expand Down Expand Up @@ -145,6 +155,7 @@ kindle = KindleHighlights::Client.new(

### In The Wild
* [tobi/highlights](https://github.com/tobi/highlights) - Download your Kindle highlights and email random ones to your inbox
* These is an example script ('/examples/Kindle2Folder.rb') which can be used to download all your highlights to a collection of .txt files in markdown format.

### Contributing to kindle-highlights (PRs welcome)

Expand Down
35 changes: 35 additions & 0 deletions examples/Kindle2Folder.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
# Kindle2Folder.rb Script
# This example script uses the kindle-highlights script to create a seperate .txt file for each book in your collection in the
# Amazon Kindle Notebook. Each file contains the name of the book and author, and then adds each highlight below it with the
# page number/location and any associated notes. Finally it adds a link to the highlight in the desktop version of the kindle
# app using the "kindle://" url scheme.

require 'kindle_highlights'

def sanitize_filename(filename) #Function to remove invalid symbols from filenames
fn = filename.split /(?<=.)\.(?=[^.])(?!.*\
fn.map! { |s| s.gsub /[^a-z0-9\-]+/i, '_' }
return fn.join '.'
end

kindle = KindleHighlights::Client.new( #Add amazon login details below
email_address: "[email protected]",
password: "password"
)

folder = File.expand_path('~/Kindle') #Folder you want to store the txt files (in markdown format) in...

kindle.books.each do |book|
fname = folder+"/"+sanitize_filename(book.title)+".txt"
print book.title+"\r\n" #Print name of Book to console so you can track progress
bookfile = File.open(fname, "w")
bookfile.puts "# #{book.title} by #{book.author} \r\n" #Header of the Book Title and Author
book.highlights_from_amazon.each do |highlight|
bookfile.puts "#{highlight.text} _#{highlight.page}_ \r\n" #Insert Highlighted Text and the Page Number
if highlight.note.present?
bookfile.puts "**Note:** #{highlight.note} \r\n" #If there's a Note attached to the highlight add this below the highlighted text
end
bookfile.puts "\r\n [Open in Kindle App](kindle://book?action=open&asin=#{book.asin}&location=#{highlight.location})\r\n\r\n" #Add a link to the highlight in the Kindle App
end
bookfile.close
end
8 changes: 6 additions & 2 deletions lib/kindle_highlights/highlight.rb
Original file line number Diff line number Diff line change
@@ -1,18 +1,22 @@
module KindleHighlights
class Highlight
attr_accessor :asin, :text, :location
attr_accessor :asin, :text, :note, :page, :location

def self.from_html_elements(book:, html_elements:)
new(
asin: book.asin,
text: html_elements.children.search("div.kp-notebook-highlight").first.text.squish,
note: html_elements.children.search("span#note").first.text,
page: html_elements.children.search("span#annotationHighlightHeader").first.text.partition('|').last.lstrip,
location: html_elements.children.search("input#kp-annotation-location").first.attributes["value"].value,
)
end

def initialize(asin:, text:, location:)
def initialize(asin:, text:, note:, page:, location:)
@asin = asin
@text = text
@note = note
@page = page
@location = location
end

Expand Down
9 changes: 9 additions & 0 deletions test/fetching_books_and_highlights_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,8 @@ def test_fetching_highlights_for_a_book
assert_equal "306", highlight.location
assert_equal "Destiny is not born of decision; it is born of uncontrollable circumstances.", highlight.text
assert_equal "B000XUAETY", highlight.asin
assert_equal "Page: 7", highlight.page
assert_equal "This is a note!", highlight.note
end

def test_fetching_highlights_for_a_non_existing_asin
Expand Down Expand Up @@ -93,6 +95,9 @@ def raw_quotes_page
<div class="a-column a-span10 kp-notebook-row-separator">
<div class="a-row">
<input type="hidden" name="" value="306" id="kp-annotation-location">
<div class="a-column a-span8">
<span id="annotationHighlightHeader">Yellow highlight | Page: 7</span>
</div>
<div class="a-column a-span4 a-text-right a-span-last">
<div class="a-row a-spacing-top-medium">
<div class="a-column a-span10 a-spacing-small kp-notebook-print-override">
Expand All @@ -101,6 +106,10 @@ def raw_quotes_page
Destiny is not born of decision; it is born of uncontrollable circumstances.
</span>
</div>
<div id="note-" class="a-row a-spacing-top-base kp-notebook-note aok-hidden kp-notebook-selectable">
<span id="note-label" class="a-size-small a-color-secondary">Note:</span>
<span id="note" class="a-size-base-plus a-color-base">This is a note!</span>
</div>
</div>
</div>
</div>
Expand Down