Skip to content

Instantly share code, notes, and snippets.

@chrishavlin
Created October 18, 2024 17:35
Show Gist options
  • Save chrishavlin/8bf3665b7e568dc8d615cee732984bde to your computer and use it in GitHub Desktop.
Save chrishavlin/8bf3665b7e568dc8d615cee732984bde to your computer and use it in GitHub Desktop.
'''
this takes the backup file from the Fast Notepad android app and saves every
note as an individual file. loses info on folder hierarchy, it probably is
buggy and may need tweaking but it was sufficient for my purposes...
Requirements:
python 3+
To use:
1. edit the fname, output_dir and output_format vars below
2. run as a script:
$ python split_fastnotepad_backup.py
Copyright 2024 Chris Havlin
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and
associated documentation files (the “Software”), to deal in the Software without restriction, including
without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to
the following conditions:
The above copyright notice and this permission notice shall be included in all copies or substantial
portions of the Software.
THE SOFTWARE IS PROVIDED “AS IS”, WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT L
IMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
'''
import json
import os
# edit these lines
fname = 'FastNotepad_2024-10-18'
output_dir = 'entries'
output_format = '.txt'
# business starts here
substrings_to_replace = [
('{[!*|@]}', ''),
('{\\"reminder\\":0}', ''),
]
with open(fname, 'r') as fi:
raw_str = fi.read()
json_str = raw_str[raw_str.find("#")+1:]
for sstr, fill in substrings_to_replace:
print(sstr)
json_str = json_str.replace(sstr, fill)
# split the subjsons
json_str = json_str.replace('}{','}THISISAPLACEHOLDER{')
sub_jsons =json_str.split('THISISAPLACEHOLDER')
# sub_jsons[0] is the index
# sub_jsons[1] does not seem meaningful
# sub_jsons[2] are all the individual notes
# could work out the index/note mapping but whatever...
entries = json.loads(sub_jsons[2])
entries = [post for post in entries.values() if not isinstance(post, int)]
# these chars will be replaced when generating filenames
badstrngs = ':,/()"+?|!'
if not os.path.isdir(output_dir):
os.mkdir(output_dir)
for entry in entries:
# title base will be the string up to the first linebreak
title = entry[:entry.find('\n')].replace(" ","_")
for bstr in badstrngs:
title = title.replace(bstr,"")
title = title.replace('.',"_")
title = title.replace("'","_")
title = title.replace("__","_")
# dont like when the filename starts with a number...
try:
_ = int(title[0])
title = 's_' + title
except:
pass
if len(title)>20:
title=title[:20]
fname = os.path.join(output_dir, title + output_format)
print(f"writing {fname}")
with open(fname, 'w') as fi:
fi.write(entry)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment