From 4807a19a7c1051838ae751881c1e14519133bdd1 Mon Sep 17 00:00:00 2001 From: AmedeeBulle Date: Fri, 1 Jul 2016 11:25:31 +0200 Subject: [PATCH 1/3] Make strftime() string portable %h is not a valid keyword in python strftime() format. Use %b instead --- ever2simple/converter.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/ever2simple/converter.py b/ever2simple/converter.py index 4a3f849..ca6dfb2 100644 --- a/ever2simple/converter.py +++ b/ever2simple/converter.py @@ -13,7 +13,7 @@ class EverConverter(object): """ fieldnames = ['createdate', 'modifydate', 'content', 'tags'] - date_fmt = '%h %d %Y %H:%M:%S' + date_fmt = '%b %d %Y %H:%M:%S' def __init__(self, enex_filename, simple_filename=None, fmt='json'): self.enex_filename = os.path.expanduser(enex_filename) From 40b5c6805b1c9b89adfe30c5ff93cd6bb3eb5229 Mon Sep 17 00:00:00 2001 From: Philippe Vanhaesendonck Date: Fri, 1 Jul 2016 12:18:23 +0200 Subject: [PATCH 2/3] Add option to write dates in UNIX Timestamp format. This is useful for JSON import --- ever2simple/converter.py | 13 ++++++++++--- ever2simple/core.py | 3 ++- setup.py | 1 + 3 files changed, 13 insertions(+), 4 deletions(-) diff --git a/ever2simple/converter.py b/ever2simple/converter.py index ca6dfb2..d56946f 100644 --- a/ever2simple/converter.py +++ b/ever2simple/converter.py @@ -1,6 +1,8 @@ import json import os import sys +import datetime +import pytz from csv import DictWriter from cStringIO import StringIO from dateutil.parser import parse @@ -15,7 +17,7 @@ class EverConverter(object): fieldnames = ['createdate', 'modifydate', 'content', 'tags'] date_fmt = '%b %d %Y %H:%M:%S' - def __init__(self, enex_filename, simple_filename=None, fmt='json'): + def __init__(self, enex_filename, simple_filename=None, fmt='json', epoch=False): self.enex_filename = os.path.expanduser(enex_filename) self.stdout = False if simple_filename is None: @@ -24,6 +26,7 @@ def __init__(self, enex_filename, simple_filename=None, fmt='json'): else: self.simple_filename = os.path.expanduser(simple_filename) self.fmt = fmt + self.epoch = epoch def _load_xml(self, enex_file): try: @@ -49,8 +52,12 @@ def prepare_notes(self, xml_tree): updated_string = created_string if note.xpath('updated'): updated_string = parse(note.xpath('updated')[0].text) - note_dict['createdate'] = created_string.strftime(self.date_fmt) - note_dict['modifydate'] = updated_string.strftime(self.date_fmt) + if self.epoch: + note_dict['createdate'] = int((created_string - datetime.datetime(1970,1,1,tzinfo=pytz.utc)).total_seconds()) + note_dict['modifydate'] = int((updated_string - datetime.datetime(1970,1,1,tzinfo=pytz.utc)).total_seconds()) + else: + note_dict['createdate'] = created_string.strftime(self.date_fmt) + note_dict['modifydate'] = updated_string.strftime(self.date_fmt) tags = [tag.text for tag in note.xpath('tag')] if self.fmt == 'csv': tags = " ".join(tags) diff --git a/ever2simple/core.py b/ever2simple/core.py index cfdcf33..4ae0b82 100644 --- a/ever2simple/core.py +++ b/ever2simple/core.py @@ -9,6 +9,7 @@ def main(): parser.add_argument('enex-file', help="the path to the Evernote.enex file") parser.add_argument('-o', '--output', help="the path to the output file or directory, leave black to output to the terminal (stdout)") parser.add_argument('-f', '--format', help="the output format, json, csv or a directory", choices=['json', 'csv', 'dir'], default='json') + parser.add_argument('-e', '--epoch', help="Write dates UNIX timestamp format", action="store_true") args = parser.parse_args() enex_file = vars(args)['enex-file'] output = args.output @@ -17,7 +18,7 @@ def main(): if not os.path.exists(filepath): print 'File does not exist: %s' % filepath sys.exit(1) - converter = EverConverter(filepath, simple_filename=output, fmt=fmt) + converter = EverConverter(filepath, simple_filename=output, fmt=fmt, epoch=args.epoch) converter.convert() sys.exit() diff --git a/setup.py b/setup.py index 07936af..0ea5063 100644 --- a/setup.py +++ b/setup.py @@ -26,6 +26,7 @@ 'lxml', 'python-dateutil<2.0', 'html2text', + 'pytz' ], entry_points=""" [console_scripts] From 7986a5eb0c86fb2a14ee0a967b28f464f6efce6b Mon Sep 17 00:00:00 2001 From: Philippe Vanhaesendonck Date: Fri, 1 Jul 2016 13:04:49 +0200 Subject: [PATCH 3/3] Make JSON output human-friendly --- ever2simple/converter.py | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/ever2simple/converter.py b/ever2simple/converter.py index d56946f..d513c8c 100644 --- a/ever2simple/converter.py +++ b/ever2simple/converter.py @@ -113,14 +113,14 @@ def _convert_csv(self, notes): def _convert_json(self, notes): if self.simple_filename is None: - sys.stdout.write(json.dumps(notes)) + sys.stdout.write(json.dumps(notes, sort_keys=True, indent=4, separators=(',', ': '))) else: with open(self.simple_filename, 'w') as output_file: - json.dump(notes, output_file) + json.dump(notes, output_file, sort_keys=True, indent=4, separators=(',', ': ')) def _convert_dir(self, notes): if self.simple_filename is None: - sys.stdout.write(json.dumps(notes)) + sys.stdout.write(json.dumps(notes, sort_keys=True, indent=4, separators=(',', ': '))) else: if os.path.exists(self.simple_filename) and not os.path.isdir(self.simple_filename): print '"%s" exists but is not a directory. %s' % self.simple_filename