From 9520608d5615d8d0d4e9c0981ceded71ad734627 Mon Sep 17 00:00:00 2001 From: Arne Tarara Date: Tue, 2 Jun 2020 17:38:29 +0200 Subject: [PATCH 1/3] - correct tags andcan handle empty tags - can handle empty title - correct created_at and updated_at - close the root note --- standard-notes-to-enex.py | 58 ++++++++++++++++++++++++++++++++++++--- 1 file changed, 54 insertions(+), 4 deletions(-) diff --git a/standard-notes-to-enex.py b/standard-notes-to-enex.py index d77eb44..6a565e6 100755 --- a/standard-notes-to-enex.py +++ b/standard-notes-to-enex.py @@ -1,5 +1,7 @@ import json import html +import datetime +import re from sys import argv source = argv[1] if len(argv) > 1 else 'notes.txt' @@ -21,14 +23,62 @@ final_file = ''' ''' +tag_notes_links = {} +# first we build the tag hash for key, item in enumerate(data['items']): - if 'title' in item['content'] and 'text' in item['content']: + if item['content_type'] == 'Tag': + for inner_key, reference in enumerate(item['content']['references']): + if 'uuid' not in reference: + print(f"Missing UUID for one entry in {reference} ... this can happen sometimes ... reason so far unclear") + continue + if reference['uuid'] in tag_notes_links: + tag_notes_links[reference['uuid']].append(html.escape(item['content']['title'])) + else: + tag_notes_links[reference['uuid']] = [html.escape(item['content']['title'])] + + +#print (json.dumps(tag_notes_links, indent=2)) +#quit() + +counter = 0 +for key, item in enumerate(data['items']): + if item['content_type'] == 'Note': + +# if 'title' in item['content'] and 'text' in item['content']: # tís this if really needed? + + if 'title' in item['content']: title = item['content']['title'] title = html.escape(title) - text = item['content']['text'] - text = text_from_html(title, text) - final_file += '''{title}{text}]]>20171228T194130Z20171228T194141Z0'''.format(title=title, text=text) + # title = re.sub(r'\\([^\w])', r'\1', title) # if you have mal-imported notes from earliner in SN, then this de-escaping may help you + else: + title = "empty title" + + text = item['content']['text'] + text = text_from_html(title, text) + #text = re.sub(r'\\([^\w])', r'\1', text) # if you have mal-imported notes from earliner in SN, then this de-escaping may help you + + if item['uuid'] not in tag_notes_links: + print(f"Whoops .... {title} had no tag ... if this is expected, then safely ignore ... Setting tag: 'Missing-Standard-Notes-Tag'.") + tag = 'Missing-Standard-Notes-Tag' + else: + tag = ''.join(tag_notes_links[item['uuid']]) + + # remove miliseconds and timezone ... example: 2020-04-14T14:30:09.256Z => 2020-04-14T14:30:09 + # we assume zulu time for all dates (UTC) + created_at = datetime.datetime.strptime(item['created_at'][0:19], '%Y-%m-%dT%H:%M:%S').strftime('%Y%m%dT%H%M%SZ') + updated_at = datetime.datetime.strptime(item['updated_at'][0:19], '%Y-%m-%dT%H:%M:%S').strftime('%Y%m%dT%H%M%SZ') + + + final_file += '''{title}{text}]]>{created_at}{updated_at}{tag}0'''.format(title=title, text=text, tag=tag, created_at=created_at, updated_at=updated_at) + + counter += 1 # uncommet following lines if you want to limit import + #if counter > 500: # only process 500 notes + # break + +final_file += '''''' # close root tag + # Save with open("notes.enex", "w") as text_file: text_file.write(final_file) + -- 2.54.0 From cfaadb06025da34562d5443b9dcaaf79cda0198b Mon Sep 17 00:00:00 2001 From: Arne Tarara Date: Thu, 4 Jun 2020 15:47:30 +0200 Subject: [PATCH 2/3] Removed all commented lines and annotations that were optional --- standard-notes-to-enex.py | 14 -------------- 1 file changed, 14 deletions(-) diff --git a/standard-notes-to-enex.py b/standard-notes-to-enex.py index 6a565e6..ab39ff2 100755 --- a/standard-notes-to-enex.py +++ b/standard-notes-to-enex.py @@ -24,7 +24,6 @@ final_file = ''' ''' tag_notes_links = {} -# first we build the tag hash for key, item in enumerate(data['items']): if item['content_type'] == 'Tag': for inner_key, reference in enumerate(item['content']['references']): @@ -36,26 +35,17 @@ for key, item in enumerate(data['items']): else: tag_notes_links[reference['uuid']] = [html.escape(item['content']['title'])] - -#print (json.dumps(tag_notes_links, indent=2)) -#quit() - -counter = 0 for key, item in enumerate(data['items']): if item['content_type'] == 'Note': -# if 'title' in item['content'] and 'text' in item['content']: # tís this if really needed? - if 'title' in item['content']: title = item['content']['title'] title = html.escape(title) - # title = re.sub(r'\\([^\w])', r'\1', title) # if you have mal-imported notes from earliner in SN, then this de-escaping may help you else: title = "empty title" text = item['content']['text'] text = text_from_html(title, text) - #text = re.sub(r'\\([^\w])', r'\1', text) # if you have mal-imported notes from earliner in SN, then this de-escaping may help you if item['uuid'] not in tag_notes_links: print(f"Whoops .... {title} had no tag ... if this is expected, then safely ignore ... Setting tag: 'Missing-Standard-Notes-Tag'.") @@ -71,10 +61,6 @@ for key, item in enumerate(data['items']): final_file += '''{title}{text}]]>{created_at}{updated_at}{tag}0'''.format(title=title, text=text, tag=tag, created_at=created_at, updated_at=updated_at) - counter += 1 # uncommet following lines if you want to limit import - #if counter > 500: # only process 500 notes - # break - final_file += '''''' # close root tag -- 2.54.0 From 3a068ede6f24013d9712081a7f7fbeeb2fc42d59 Mon Sep 17 00:00:00 2001 From: Arne Tarara Date: Thu, 4 Jun 2020 15:59:22 +0200 Subject: [PATCH 3/3] Fixed indentation --- standard-notes-to-enex.py | 36 ++++++++++++++++++------------------ 1 file changed, 18 insertions(+), 18 deletions(-) diff --git a/standard-notes-to-enex.py b/standard-notes-to-enex.py index ab39ff2..5b270f6 100755 --- a/standard-notes-to-enex.py +++ b/standard-notes-to-enex.py @@ -36,30 +36,30 @@ for key, item in enumerate(data['items']): tag_notes_links[reference['uuid']] = [html.escape(item['content']['title'])] for key, item in enumerate(data['items']): - if item['content_type'] == 'Note': + if item['content_type'] == 'Note': - if 'title' in item['content']: - title = item['content']['title'] - title = html.escape(title) - else: - title = "empty title" + if 'title' in item['content']: + title = item['content']['title'] + title = html.escape(title) + else: + title = "empty title" - text = item['content']['text'] - text = text_from_html(title, text) + text = item['content']['text'] + text = text_from_html(title, text) - if item['uuid'] not in tag_notes_links: - print(f"Whoops .... {title} had no tag ... if this is expected, then safely ignore ... Setting tag: 'Missing-Standard-Notes-Tag'.") - tag = 'Missing-Standard-Notes-Tag' - else: - tag = ''.join(tag_notes_links[item['uuid']]) + if item['uuid'] not in tag_notes_links: + print(f"Whoops .... {title} had no tag ... if this is expected, then safely ignore ... Setting tag: 'Missing-Standard-Notes-Tag'.") + tag = 'Missing-Standard-Notes-Tag' + else: + tag = ''.join(tag_notes_links[item['uuid']]) - # remove miliseconds and timezone ... example: 2020-04-14T14:30:09.256Z => 2020-04-14T14:30:09 - # we assume zulu time for all dates (UTC) - created_at = datetime.datetime.strptime(item['created_at'][0:19], '%Y-%m-%dT%H:%M:%S').strftime('%Y%m%dT%H%M%SZ') - updated_at = datetime.datetime.strptime(item['updated_at'][0:19], '%Y-%m-%dT%H:%M:%S').strftime('%Y%m%dT%H%M%SZ') + # remove miliseconds and timezone ... example: 2020-04-14T14:30:09.256Z => 2020-04-14T14:30:09 + # we assume zulu time for all dates (UTC) + created_at = datetime.datetime.strptime(item['created_at'][0:19], '%Y-%m-%dT%H:%M:%S').strftime('%Y%m%dT%H%M%SZ') + updated_at = datetime.datetime.strptime(item['updated_at'][0:19], '%Y-%m-%dT%H:%M:%S').strftime('%Y%m%dT%H%M%SZ') - final_file += '''{title}{text}]]>{created_at}{updated_at}{tag}0'''.format(title=title, text=text, tag=tag, created_at=created_at, updated_at=updated_at) + final_file += '''{title}{text}]]>{created_at}{updated_at}{tag}0'''.format(title=title, text=text, tag=tag, created_at=created_at, updated_at=updated_at) final_file += '''''' # close root tag -- 2.54.0