-
Notifications
You must be signed in to change notification settings - Fork 0
/
transfer.py
78 lines (58 loc) · 2.15 KB
/
transfer.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
import argparse
import datetime
import os
def migrate(filename, dry_run=True):
date = None
tmp_file = 'data/tmp.txt'
for line in filename:
if is_date(line):
if date:
write_to_dayone(date, tmp_file, dry_run) # Write to dayone
open(tmp_file, 'w').close() # flush the temporary file
date = strip_to_date(line)
else:
write_to_tmp(line, tmp_file) # clean up the line and append to tmp.txt
write_to_dayone(date, tmp_file, dry_run)
os.remove(tmp_file)
def write_to_dayone(date, tmp_file, dry_run):
if dry_run:
with open(tmp_file, 'r') as content:
print date + '\n' + content.read()
else:
print 'Logging entry for {0} in dayone'.format(date)
command = 'dayone -d="{0}" -s=false new < {1}'.format(date, tmp_file)
os.system(command)
def write_to_tmp(text, tmp_file):
text = clean_text(text)
if text is not None:
with open(tmp_file, 'a') as tmp_file:
tmp_file.write(text + '\n')
def clean_text(text):
text = text.strip('\n').strip('\r').strip(' ')
if len(text) == 0:
return None
if '-' not in text[:4]:
text = '- ' + text
return text
def is_date(date_text):
try:
datetime.datetime.strptime(strip_to_date(date_text), '%Y/%m/%d')
except ValueError:
return False
return True
def strip_to_date(date_text):
return date_text.strip('\n').strip('\r').replace('-', '/')
def main():
parser = argparse.ArgumentParser(description="""Migrate ohlife export data file to dayone app""")
parser.add_argument('-d', default=True,
dest="dry_run",
action="store_false",
help="""Actually write to dayone""")
parser.add_argument('-f', default='data/sample.txt',
dest="filename",
type=argparse.FileType('r'),
help="""filename including path to load from, by default it will look for data/sample.txt""")
args = parser.parse_args()
migrate(args.filename, args.dry_run)
if __name__ == '__main__':
main()