Commits

Anonymous committed a406518 Draft

Add feature to load multiple enex files in the same folder (for notebook in Evernote)

Comments (0)

Files changed (6)

+#!/usr/bin/env python
+import json
+from VizEvernote import EvernoteAnalyzer, EvernoteVisualizer
+from VizNotebooks import NotebooksAnalyzer
+import matplotlib.pyplot as plt
+
+
+def count():
+    # notes = json.load(open('../data.json', 'r'))
+    notes = json.load(open('/home/wangjing/Evernote-2013-07-24.json', 'r'))
+    # notes = json.load(open('/home/wangjing/Public/Evernote-2013-July.json', 'r'))
+    ve = EvernoteAnalyzer(notes)
+    # ve.count('created', 'week')
+    # ve.count('created', 'year')
+    # ve.count('created', 'month')
+    # ve.count('updated', 'week')
+    # ve.count('updated', 'year')
+    # ve.count('updated', 'month')
+    ve.group_by_tags('created')
+    ve.dump(open('./Evernote-2013-07-24-count.json', 'w'))
+    # ve.dump(open('./Evernote-2013-July-count.json', 'w'))
+    # ve.dump(open('./Evernote-2013-07-24-count2.json', 'w'))
+    # import ipdb;ipdb.set_trace()
+    # ve.monthly_count()
+    # daily_evernotes(notes)
+
+
+def viz():
+    ve = EvernoteVisualizer()
+    ve.load(open('./Evernote-2013-07-24-count.json', 'r'))
+    # ve.load(open('./Evernote-2013-July-count.json', 'r'))
+    # ve.plot()
+    plt.figure()
+    ve.plot_tags_t('created', 'month', 70)
+    plt.show()
+
+
+def test_notebooks():
+    na = NotebooksAnalyzer('/home/wangjing/Public/EvernoteJSON/')
+    na._print()
+
+if __name__ == "__main__":
+    # count()
+    # viz()
+    test_notebooks()
 #!/usr/bin/env python
-from __future__ import print_function, division
+from __future__ import print_function, division, absolute_import
 import time
 import itertools
 import json
 import numpy as np
 from datetime import datetime
 from dateutil import rrule
-
-
-def bar(left, height, names, color, width, tick_num, rotation):
-    """  bar plot
-
-    Parameters
-    ---------------
-    left : left coordinates
-    height : heights of the bar
-    names : names for each bar
-    color : color
-    width : width of bar
-    tick_num : number of xticks. xticks are evenly distributed.
-    rotation : rotation of the xtick_labels
-
-    Returns
-    --------------
-    None
-    """
-    N = len(left)
-    dur = left[-1] - left[0]
-    plt.bar(left, height, color='r', width=width)
-    if tick_num is not None:
-        ticks = [names[pos] for pos in
-                 range(0, N, N // tick_num)]
-        tick_pos = np.arange(left[0], left[-1], int(dur / tick_num))
-        plt.xticks(tick_pos + width / 2., ticks, rotation=rotation)
-    else:
-        plt.xticks(np.array(left) + width / 2., names, rotation=rotation)
+from util import bar
 
 
 def get_time_diff(start_date, end_date, resolution):
         plt.show()
 
 
-def count():
-    # notes = json.load(open('../data.json', 'r'))
-    notes = json.load(open('/home/wangjing/Evernote-2013-07-24.json', 'r'))
-    # notes = json.load(open('/home/wangjing/Public/Evernote-2013-July.json', 'r'))
-    ve = EvernoteAnalyzer(notes)
-    # ve.count('created', 'week')
-    # ve.count('created', 'year')
-    # ve.count('created', 'month')
-    # ve.count('updated', 'week')
-    # ve.count('updated', 'year')
-    # ve.count('updated', 'month')
-    ve.group_by_tags('created')
-    ve.dump(open('./Evernote-2013-07-24-count.json', 'w'))
-    # ve.dump(open('./Evernote-2013-July-count.json', 'w'))
-    # ve.dump(open('./Evernote-2013-07-24-count2.json', 'w'))
-    # import ipdb;ipdb.set_trace()
-    # ve.monthly_count()
-    # daily_evernotes(notes)
 
-
-def viz():
-    ve = EvernoteVisualizer()
-    ve.load(open('./Evernote-2013-07-24-count.json', 'r'))
-    # ve.load(open('./Evernote-2013-July-count.json', 'r'))
-    # ve.plot()
-    plt.figure()
-    ve.plot_tags_t('created', 'month', 70)
-    plt.show()
-
-
-if __name__ == "__main__":
-    # count()
-    viz()
+from __future__ import print_function, division, absolute_import
+from VizEvernote import EvernoteAnalyzer
+import os
+import os.path
+import json
+
+
+class NotebooksAnalyzer(object):
+    def __init__(self, folder):
+        self.notebooks = dict()
+        self.analyzers = dict()
+        self._load_notes(folder)
+
+    def _load_notes(self, folder):
+        for f in os.listdir(folder):
+            print('load [%s]' % (f))
+            f_path = os.path.join(folder, f)
+            if os.path.isfile(f_path) and f.endswith('.json'):
+                with open(f_path, 'r') as note_f:
+                    notes = json.load(note_f)
+                    nb_name = os.path.basename(f)
+                    self.notebooks[nb_name] = notes
+                    self.analyzers[nb_name] = EvernoteAnalyzer(notes)
+    def _print(self):
+        for k, v in self.notebooks.iteritems():
+            print('notebook: ', k)
 #!/usr/bin/env python
 import argparse
+import os
+import os.path
+import json
+
 from parseNoteXML import parseNoteXML
 parser = argparse.ArgumentParser(description='parse Evernote XML to Json Format')
 parser.add_argument(
-    'xml_name', help='XML file name')
+    'xml_name', help='XML file name or a path of folder with xml files')
 
 parser.add_argument(
-    'json_name', help='json file name')
+    'json_name', help='json file name or path of output folder')
 args = parser.parse_args()
 # notes = parseNoteXML('/home/wangjing/Documents/Evernote-2013-07-24.enex')
-notes = parseNoteXML(args.xml_name)
-import json
-with open(args.json_name, 'wb') as fp:
-    json.dump(notes, fp)
+
+
+def parse_and_save(xml_name, json_name):
+    notes = parseNoteXML(xml_name)
+    with open(json_name, 'wb') as fp:
+        json.dump(notes, fp)
+
+if os.path.isfile(args.xml_name):
+    parse_and_save(args.xml_name, args.json_name)
+else:
+    if not os.path.exists(args.json_name):
+        os.makedirs(args.json_name)
+    for f in os.listdir(args.xml_name):
+        print 'write [%s]' % (f)
+        f_path = os.path.join(args.xml_name,f)
+        if os.path.isfile(f_path) and f.endswith('.enex'):
+            parse_and_save(f_path, os.path.join(args.json_name,
+                                                f.rsplit('.enex')[0] + '.json'))
+from __future__ import print_function, division, absolute_import
+import sys
 from lxml.html import etree
 from StringIO import StringIO
 #http://www.hanxiaogang.com/writing/parsing-evernote-export-file-enex-using-python/
                 try:
                     text.append(e.text)
                 except:
-                    print 'cannot print'
+                    print('cannot print', file=sys.stderr)
+
         note_dict[elem.tag] = text
         # NixNote use "Note"; Evernote Windows & Mac Client use "note"
         # if elem.tag == "Note" or elem.tag == 'note':
+import matplotlib.pyplot as plt
+import numpy as np
+
+
+def bar(left, height, names, color, width, tick_num, rotation):
+    """  bar plot
+
+    Parameters
+    ---------------
+    left : left coordinates
+    height : heights of the bar
+    names : names for each bar
+    color : color
+    width : width of bar
+    tick_num : number of xticks. xticks are evenly distributed.
+    rotation : rotation of the xtick_labels
+
+    Returns
+    --------------
+    None
+    """
+    N = len(left)
+    dur = left[-1] - left[0]
+    plt.bar(left, height, color='r', width=width)
+    if tick_num is not None:
+        ticks = [names[pos] for pos in
+                 range(0, N, N // tick_num)]
+        tick_pos = np.arange(left[0], left[-1], int(dur / tick_num))
+        plt.xticks(tick_pos + width / 2., ticks, rotation=rotation)
+    else:
+        plt.xticks(np.array(left) + width / 2., names, rotation=rotation)