Last active
March 27, 2023 07:12
-
-
Save richbs/595e112f0fd1037a9eb31ee4fef4d8bd to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from datetime import datetime, date | |
import json | |
import os | |
import sys | |
import glob | |
import re | |
import csv | |
def make_page(outfile, props, mdoutlines, body_text, created_time, source): | |
print('giugjg' +outfile) | |
cdate = date.fromtimestamp(created_time) | |
cdt = datetime.fromtimestamp(created_time) | |
props.append("source: " + source) | |
props.append("createdtime: " + | |
cdt.isoformat(sep=' ', timespec='seconds')) | |
props.append("createddate: " + str(cdate)) | |
txtlinesin = body_text.split('\n') | |
txtlines = [] | |
newblock = False | |
for i,l in enumerate(txtlinesin): | |
if l.find('::') > -1: | |
continue | |
txtlines.append(l) | |
journal_file = "".join(['Journal', os.sep, str(cdate), '.md']) | |
print(journal_file) | |
journal_out = open(journal_file, 'a', encoding='utf-8') | |
if outfile: | |
link = os.path.basename(outfile).replace('.md', '') | |
mdout = open(outfile, 'w', encoding='utf-8') | |
mdout.write("---\n") | |
mdout.write("\n".join(props)) | |
mdout.write("\n---\n") | |
mdout.write("\n".join(mdoutlines)) | |
new_body = "\n".join(txtlines) | |
# l = l.replace('- - ', '- ') | |
# l = l.replace('- - - ', '- ') | |
mdout.write(new_body) | |
mdout.close() | |
os.utime(outfile, times=(ctr, ctr)) | |
journal_out.write('* Created this note in #' +source + ' ' +source +': [[' + link + ']]\n') | |
else: | |
journal_out.write("__From #" + source + " " +"[["+ source +"]]__\n") | |
journal_out.write("---\n") | |
journal_out.write("\n".join(props)) | |
journal_out.write("\n---\n") | |
journal_out.write("\n".join(mdoutlines) + "\n") | |
new_body = "\n".join(txtlines) | |
journal_out.write(body_text + '\n\n') | |
journal_out.close() | |
source = sys.argv[3] | |
glob_arg = "".join([sys.argv[1], os.sep, '*.', sys.argv[2]]) | |
for fn in glob.iglob(glob_arg): | |
print(fn) | |
if fn.endswith('md') : | |
props = [] | |
ctr = os.path.getctime(fn) | |
#ctr = os.stat(fn).st_birthtime | |
mf = open(fn, 'r', encoding='utf-8') | |
body_text = mf.read() | |
mf.close() | |
bn = os.path.basename(fn) | |
cmatch = re.search(r'Created: ([\d :-]+)', body_text) | |
if cmatch: | |
cstr = cmatch.group(1).strip() | |
cdate = datetime.fromisoformat(cstr) | |
ctr = cdate.timestamp() | |
mmatch = re.search(r'Modified: ([\d :-]+)', body_text) | |
if mmatch: | |
mstr = mmatch.group(1).strip() | |
mdt = datetime.fromisoformat(mstr) | |
mdate = date.fromtimestamp(mdt.timestamp()) | |
props.append("modifiedtime: " + | |
mdt.isoformat(sep=' ', timespec='seconds')) | |
props.append("modifieddate: " + str(mdate)) | |
# cstr = cstr.replace(' +', '+').replace(' ', 'T') | |
# datetime.fromisoformat('2022-08-03T23:48:52+0100') | |
# Created: 2021-09-27 18:19:00 +0100 | |
# Modified: 2021-12-03 17: 15: 59 + 0000 | |
title = bn.replace('.md', '') | |
if not body_text.startswith(title) and source != 'onenote': | |
body_text = title + "\n\n" + body_text | |
mdoutlines = [] | |
make_page(fn, props, mdoutlines, body_text, ctr, source) | |
elif fn.endswith('json'): | |
dn = os.path.dirname(fn) | |
bn = os.path.basename(fn) | |
infile = "".join([dn, os.sep, bn]) | |
jf = open(infile, encoding="utf-8") | |
jj = json.load(jf) | |
ct = jj['createdTimestampUsec']; | |
ctr = round(ct/1000000) | |
props = [] | |
#props.append("- title:: " + jj['title']) | |
mdoutlines = [] | |
title = jj['title'].strip() | |
if jj['title']: | |
mdoutlines.append('## ' + jj['title'] + "\n") | |
if 'annotations' in jj: | |
for an in jj['annotations']: | |
for k, i in an.items(): | |
mdoutlines.append('- ' + ": ".join([k,i])) | |
if not title: | |
title = an['title'] | |
if 'attachments' in jj: | |
for att in jj['attachments']: | |
mdoutlines.append('![](../../assets/' + att['filePath'] + ')\n') | |
body_text = '' | |
line1 = '' | |
if 'textContent' in jj: | |
body_text = jj['textContent'] | |
for l in body_text.split('\n'): | |
if len(l.strip()) > 0: | |
line1 = l | |
outfn = bn.replace('.json', '.extmd') | |
if outfn.startswith('202'): | |
cdt = datetime.fromtimestamp(ctr) | |
outfn = cdt.isoformat(sep=' ', timespec='seconds') | |
if not title: | |
title = line1.strip() | |
if title: | |
outfn = title[:15] + '.extmd' | |
outfn = re.sub('[^A-z0-9]+', ' ', outfn) | |
outfn = re.sub('[^A-z0-9]+', ' ', outfn) | |
outfn = re.sub('\W*extmd', '.md', outfn) | |
outfile = "".join([dn, os.sep, outfn]) | |
make_page(outfile, props, mdoutlines, body_text, ctr, source) | |
elif fn.endswith('.csv'): | |
print('goat') | |
with open(fn, newline='', encoding='utf-8') as csvfile: | |
spamreader = csv.DictReader(csvfile) | |
for row in spamreader: | |
body_text = row['Note Body'] | |
(d, m, y) = row['Note Date'].split('/') | |
ctr = datetime(int(y), int(m) , int(d)).timestamp() | |
make_page('Sticky_' + row['Note Date'].replace('/','_') + '.md', [], [], body_text, ctr, source) | |
1620986135975000 | |
1678890747 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment