Skip to content

Instantly share code, notes, and snippets.

@yk-tanigawa
Last active January 14, 2019 05:46
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save yk-tanigawa/419831a12cd6dfd12c1ba98b74ba4738 to your computer and use it in GitHub Desktop.
Save yk-tanigawa/419831a12cd6dfd12c1ba98b74ba4738 to your computer and use it in GitHub Desktop.

Save Kindle annotations to google drive

Electronic books (eBooks) are changing our way of reading books by providing us enhanced functionalities to interact with books. Amazon Kindle is one of the successful example of eBook servieces populated with millions of books. One of the notable functionality of Kindle is that readers can "highlight" the sentences in books and send the highlighted texts via email. While this enable users to easily export sections of the books, they need to manually organize their exported texts. Here I present a small Google Apps Script that automatically transfers the kindle annotations to Google drive. Given the search functionality of Google Drive, users can easily look up sections of books.

This Google Apps Script performs the following tasks: (1) checks your Gmail account; (2) identify email messages from Amazon Kindle service with your annotations (attachment files); and (3) save them to your google drive folder of your choice.

Note that some publishers has a maximum amount of highlights that can be exported via email. Given that this script just copies files from Gmail to Google Drive, it doesn't address this limit. You may browse the highlighted texts on Amazon's service: https://read.amazon.co.jp/notebook

Reference

https://developers.google.com/apps-script/reference/

var TIME_LOCALE = 'UTC';
function searchSubStrDate(){
var e = new Date();
e.setMonth(e.getMonth() - 1);
var searchSubStr = ' after: ' + Utilities.formatDate(e, TIME_LOCALE, "yyyy/MM/dd");
return(searchSubStr);
}
function getName(message){
var subject = message.getSubject();
var bookTitle = subject.slice(1, subject.length - 4).replace(RegExp('\\s'), '_');
var date = message.getDate();
var dateStr = Utilities.formatDate(date, TIME_LOCALE, 'yyyyMMdd_HHmmss');
var name = dateStr + '_' + bookTitle;
return(name);
}
function saveAmazonKindleMemoToDrive(search_term, gdrive_folder_id){
/*
This script finds gmail messages containing Kindle memo files (as attachment) and
save the files to google drive.
*/
var saveFolder = DriveApp.getFolderById(gdrive_folder_id);
var threads = GmailApp.search(search_term + ' ' + searchSubStrDate(), 0, 100);
var messages = GmailApp.getMessagesForThreads(threads);
for(var i in messages){
for(var j in messages[i]){
var name = getName(messages[i][j]);
var attachments = messages[i][j].getAttachments();
try {
// Check if the same message is already processed by the script
var folder = saveFolder.getFoldersByName(name).next();
}
catch(e) {
var folder = saveFolder.createFolder(name);
for(var k in attachments){
folder.createFile(attachments[k]);
}
}
}
}
}
function main(){
var SEARCH_TERM = 'from: "amazon kindle" to:me has:attachment ';
var GDRIVE_FOLDER_ID = <<Your_Google_Drive_Folder_ID>>;
saveAmazonKindleMemoToDrive(SEARCH_TERM, GDRIVE_FOLDER_ID);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment