Created
March 28, 2014 16:51
-
-
Save xysun/9837480 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
''' | |
Manage "My Clippings.txt" from Kindle, split notes into separate files for each book, with custom file name | |
usage: | |
python3 kindle.py <option> <clipping> <name_mapping_file> <dir> | |
<option>: | |
"list": step1; list all books in the clipping file, ask for name of the notes file, store the book name <--> notes file name mapping in <name_mapping_file> | |
"notes": step2; create notes for each book | |
<clipping>: name of the "My Clippings.txt" file | |
<name_mapping_file>: name of the file to store mappings of book names <--> notes file names | |
<dir>: directory to store notes | |
''' | |
import sys, json, os, argparse | |
KINDLE_DELIMITOR = '==========\n' | |
def list_books(clipping, names): | |
books = set() | |
try: | |
with open(names, 'r') as f: | |
mapping = json.load(f) # load existing name mappings | |
except: | |
mapping = {} | |
with open(clipping, 'r') as f: | |
text = f.read() | |
notes = text.split(KINDLE_DELIMITOR) | |
for note in notes: | |
title = note.split('\n')[0] | |
if len(title) > 0: | |
books.add(title) | |
print("= = = = =") | |
print("These are the new books in your clipping file:\n") | |
for book in books: | |
if not book in mapping.keys(): | |
user_bookname = "" | |
while len(user_bookname) == 0: | |
print("Please enter clipping file name for book: " + book) | |
user_bookname = input() | |
mapping[book] = user_bookname | |
with open(names, 'w') as f: | |
f.write(json.dumps(mapping, indent = 4)) | |
print("Book name mapping saved in " + names) | |
print("= = = = =\n") | |
def create_notes(clipping, names, directory = "notes/"): | |
if not os.path.exists(directory): | |
os.makedirs(directory) | |
with open(names, 'r') as f: | |
books = json.load(f) | |
with open(clipping, 'r') as f: | |
text = f.read() | |
notes = text.split(KINDLE_DELIMITOR) | |
for note in notes: | |
_title = note.split('\n')[0] | |
if _title in books.keys(): | |
title = books[_title] | |
notes_file = os.path.join(directory, title) | |
with open(notes_file, 'a') as f: | |
f.write('\n'.join(note.split('\n')[1:])) | |
f.write(KINDLE_DELIMITOR) | |
def main(): | |
usage = "Usage: python3 kindle.py <option> <clipping> <name_mapping_file> <dir>" | |
if len(sys.argv) < 4: | |
print(usage) | |
else: | |
option = sys.argv[1] | |
clipping = sys.argv[2] | |
names = sys.argv[3] | |
if option == "list": | |
list_books(clipping, names) | |
elif option == "notes": | |
if len(sys.argv) > 4: | |
directory = sys.argv[4] | |
create_notes(clipping, names, directory) | |
else: | |
create_notes(clipping, names) | |
else: | |
print(usage) | |
if __name__ == '__main__': | |
main() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment