Skip to content

Instantly share code, notes, and snippets.

@yoonbae81
Created June 9, 2020 03:00
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save yoonbae81/9d3b39fd956b356bb63d8c59f5ddc12b to your computer and use it in GitHub Desktop.
Save yoonbae81/9d3b39fd956b356bb63d8c59f5ddc12b to your computer and use it in GitHub Desktop.
Import Diaro journal entries and render to another format
"""
Output:
### 2020-06-08 12:00
Title and contents are here
### 2020-06-09 12:00
Another title and contents are here
"""
from datetime import datetime
from collections import namedtuple
INPUT = 'entryExport.txt'
OUTPUT = 'output.txt'
Entry = namedtuple('Entry', 'date content')
def load(filename):
with open(filename, encoding='utf-8-sig') as f:
text = f.read()
items = text.split('------------------------------------------------------------------------------------------------')
items = [entry.strip() for entry in items]
return items[::-1]
def parse(item):
try:
date, content = item.split('\n', 1)
except ValueError:
print('ERROR: ' + item)
return None
date = datetime.strptime(date, '%d %B %Y, %A %I:%M %p')
date = date.strftime('%Y-%m-%d %H:%M')
content = content.strip().replace('::: ', '').replace(' :::\n\n', ' ')
# delete weather
content = '\n'.join([line for line in content.split('\n') if not 'Weather: ' in line])
content = content.strip()
return Entry(date, content)
def render(item):
return f'### {item.date}\n{item.content}\n'
if __name__ == '__main__':
with open(OUTPUT, 'wt', encoding='utf-8') as output:
for item in load(INPUT):
entry = parse(item)
print(render(entry), file=output)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment