-
Notifications
You must be signed in to change notification settings - Fork 3
/
MacJournaltoDayOne.py
executable file
·122 lines (98 loc) · 2.84 KB
/
MacJournaltoDayOne.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
#!/usr/bin/env python2.6
# encoding: utf-8
"""
untitled.py
Created by kdm on 2010-02-10.
Copyright (c) 2010 __MyCompanyName__. All rights reserved.
"""
import sys
#import getopt
import time
from datetime import date
from datetime import timedelta
from time import strptime
import os
import string
import subprocess
import shlex
import optparse
import logging
from shutil import copyfileobj
def main(argv=None):
if argv is None:
argv = sys.argv
#Parse input variables
main_logger = logging.getLogger('main')
usage = "usage: %prog [options] <MacJournal File to import>"
parser = optparse.OptionParser(usage=usage)
parser.add_option("--debug", dest="debug", default=False, action = "store_true", help = "Turn on debug output (default false)")
(options, args) = parser.parse_args()
if len(args) != 1:
parser.error("File to import required")
else:
journal_name = args[0]
if options.debug:
logging.basicConfig(level="logging.debug")
else:
logging.basicConfig(level="logging.error")
try:
journal = open(journal_name,'r')
except:
main_logger.error("Could not open %s" % journal_name)
return 1 # Could not open journal
numEntries = 0
prevEntryDate = None
journalEntry = None
curEntryLines = 0
prevEntryLines = 0
continueLoop = True
entryDone = False
curEntryDate = None
prevEntry = ''
curEntry = ''
#Due to python's lack of a DO/UNTIL statement, have to do things a bit differently
while continueLoop is True:
entryLine = journal.readline()
if not entryLine: #Reached EOF
entryDone = True
continueLoop = False
prevEntryDate = curEntryDate
prevEntry = curEntry
prevEntryLines = curEntryLines
curEntry = ''
curEntryLines = 0
print("Reached EOF")
if entryLine.startswith("\tDate:\t"): # An entry is finished
prevEntryDate = curEntryDate
prevEntry = curEntry
prevEntryLines = curEntryLines
curEntry = ''
curEntryLines = 0
curEntryDate = entryLine.replace("\tDate:\t",'').rstrip()
entryDone = True
print("Found entry " + curEntryDate)
elif entryLine is not "":
curEntry += entryLine
curEntryLines += 1
if (entryDone is True) and (prevEntryLines is not 0):
#Write output to DayOne
print '{0} has {1} lines'.format(prevEntryDate,repr(prevEntryLines))
addCommandParsed = ['/Applications/Day One.app/Contents/MacOS/dayone', '-d="' + format(prevEntryDate) +'"', 'new']
print addCommandParsed
try:
addCommandProcess = subprocess.Popen(addCommandParsed,stdin=subprocess.PIPE,stdout=subprocess.PIPE,stderr=subprocess.PIPE)
except:
None
addCommandOut = addCommandProcess.communicate(prevEntry)
print addCommandOut[0]
print addCommandOut[1]
numEntries += 1
entryDone = False
prevEntryDate = None
prevEntry = None
prevEntryLines = 0
print("Found %s entries" % numEntries)
journal.close()
return 0
if __name__ == "__main__":
sys.exit(main())