Last active
June 14, 2020 00:51
-
-
Save pklaus/32e904e39fe35b56a6333939eba1ea4c to your computer and use it in GitHub Desktop.
A Python script for backing up your simplenote notes.
This file contains hidden or 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/bin/env python | |
import simplenote | |
import os, json | |
from datetime import date, datetime as dt | |
def main(): | |
import argparse, getpass | |
parser = argparse.ArgumentParser() | |
parser.add_argument('user', help='The email address of the Simplenote user.') | |
parser.add_argument('--password', help='If not stated, you will be asked for it.') | |
parser.add_argument('--folder', default='./{user}_{date}', | |
help="Specify the folder to store the backup in. " | |
"The variables '{user}', '{date}' and '{datetime}' will be expanded. " | |
"Defaults to '{user}_{date}'.") | |
args = parser.parse_args() | |
d = date.today().isoformat() | |
datetime = dt.now().replace(microsecond=0).isoformat().replace(':', '-') | |
args.folder = args.folder.format(user=args.user, date=d, datetime=datetime) | |
if not args.password: | |
args.password = getpass.getpass() | |
backup(args.folder, args.user, args.password) | |
def backup(folder, user, password): | |
os.makedirs(folder, exist_ok=True) | |
sn = simplenote.Simplenote(user, password) | |
note_list = sn.get_note_list() | |
note_list = note_list[0] | |
note_list_path = os.path.join(folder, 'note_list.json') | |
with open(note_list_path, 'w') as f: | |
json.dump(note_list, f, indent=2) | |
for count, note in enumerate(note_list, start=1): | |
print("Backing up note # ", count, " of ", len(note_list)) | |
if note['deleted']: continue | |
individual_note = sn.get_note(note['key']) | |
individual_note = individual_note[0] | |
note_path_json = os.path.join(folder, note['key'] + '.json') | |
note_path_plain = os.path.join(folder, note['key'] + '.txt') | |
with open(note_path_json, 'w') as f: | |
json.dump(individual_note, f, indent=2) | |
with open(note_path_plain, 'w') as f: | |
f.write(individual_note['content']) | |
if __name__ == "__main__": | |
main() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment