Created
April 22, 2020 03:03
-
-
Save maxandron/2305eeee0220e4e8c439e651e8385159 to your computer and use it in GitHub Desktop.
Parses kindle "My Clippings.txt" (the book highlights) file to markdown format
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
#!/usr/local/bin/python3 | |
import argparse | |
def clippings_to_dict(clip_file): | |
quotes = open(clip_file, 'r').read().split('==========') | |
split_quotes = [] | |
for quote in quotes: | |
# Remove empty lines | |
split_quotes.append(list(filter(lambda x: x != '', quote.split('\n')))) | |
quotes_dict = {} | |
for quote in split_quotes: | |
if len(quote) != 3: | |
# Remove empty highlights | |
continue | |
# Remove <feff> character | |
key = quote[0].strip('\ufeff') | |
if key not in quotes_dict.keys(): | |
quotes_dict[key] = [] | |
quotes_dict[key].append((quote[1], quote[2])) | |
return quotes_dict | |
def dict_to_markdown(quotes_dict, output_path): | |
with open(output_path, 'a') as output: | |
for book, quotes in quotes_dict.items(): | |
output.write('# {}\n'.format(book)) | |
for quote in quotes: | |
output.write('*{}*\n{}\n\n'.format(quote[0], quote[1])) | |
def main(): | |
parser = argparse.ArgumentParser() | |
parser.add_argument('clip_file', type=str, help='The original Clippings.txt file from the kindle') | |
parser.add_argument('output_path', type=str, help='The output markdown file to create') | |
args = parser.parse_args() | |
quotes_dict = clippings_to_dict(args.clip_file) | |
dict_to_markdown(quotes_dict, args.output_path) | |
if __name__ == '__main__': | |
main() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment