This is a modification of lxyu's code.
This script has multiple modifications, the most relevant ones are listed below:
-
File Opening Mode Correction in
save_clips
Function:- Changed the file opening mode from binary write (
'wb'
) to write ('w'
) to ensure compatibility with thejson.dump()
function, which expects to write strings, not bytes.
- Changed the file opening mode from binary write (
-
Correction of String Encoding in
export_txt
Function:- Removed the
.encode('utf-8')
method from the lines being appended, keeping the data as strings instead of converting them to bytes. - Changed the file opening mode from binary write (
'wb'
) to write ('w'
) to match the string data type used in file operations.
- Removed the
-
Directory Existence Check:
- Added a check in the
export_txt
function to ensure theOUTPUT_DIR
exists before attempting to write files. Usedos.makedirs()
to create the directory if it does not exist.
- Added a check in the
-
Sanitization of File Names:
- Added a
sanitize_filename
function to remove or replace characters that might be problematic in file names, ensuring compatibility with the filesystem.
- Added a
-
Ordering of Clippings (General Advice):
- Suggested adding ordering logic based on available metadata (like position or timestamp) before exporting the clippings to maintain the correct order in the output files.
Clippings are stored in a Python dictionary with this structure:
clips = {'book': {'position': 'clipping'}}
Msgpack was used to serialize clippings for archive.
Each new My Clippings.txt
will add clips to previous archive automatically.
Clips will be exported to the output
directory, find them there.
It's EASY and you don't need to care about anything!
Install msgpack-python
first.
$ pip install msgpack-python
Clone project and put My Clippings.txt
to project's root.
Run kindle.py
:
$ python kindle.py
DONE!
This version of the code returns .md
files instead of .txt
files, because it is intended to be used as an auxiliary code to import your clippings to your webpage easily!