Skip to content

Instantly share code, notes, and snippets.

@dbieber

dbieber/roam.py Secret

Created February 9, 2021 03:51
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 dbieber/7722280c7fa10d1fdfa507612427001d to your computer and use it in GitHub Desktop.
Save dbieber/7722280c7fa10d1fdfa507612427001d to your computer and use it in GitHub Desktop.
import datetime
import functools
import json
import subprocess
import re
import fire
ROAMRESEARCH_DBPATH = 'your_path.json'
def update():
roam_dir = '/path/to/roam-backup'
subprocess.call(['ssh-add', '/path/to/.ssh/id_rsa'])
subprocess.call(['git', 'pull', 'origin', 'main'], cwd=roam_dir)
clear_cache()
def clear_cache():
get_pages.cache_clear()
@functools.lru_cache()
def get_pages():
return json.load(open(ROAMRESEARCH_DBPATH, 'r'))
def get_children():
children = []
for page in get_pages():
children.extend(get_children_of(page))
return children
def get_child(uid):
for child in get_children():
if child.get('uid') == uid:
return child
def get_children_of(page_or_child):
children = []
if isinstance(page_or_child, list):
immediate_children = page_or_child
else:
immediate_children = page_or_child.get('children', [])
for child in immediate_children:
if 'uid' in page_or_child: # child of child
child['parent_uid'] = page_or_child['uid']
elif 'title' in page_or_child: # child of page
child['parent_title'] = page_or_child.get('title')
else:
pass # child of list
children.append(child)
children.extend(get_children_of(child))
return children
def get_date(child):
for c in child.get('children', []):
if c['string'].startswith('Date:'):
return c['string'][5:].lstrip(': ').strip('[]') # e.g. 'November 5th, 2020'
return child['create-time'] # e.g. 1606249842438
def get_property_of(page_or_child, name):
for child in page_or_child.get('children', []):
if child['string'].rstrip(': ').strip() == name.rstrip(': ').strip():
return child.get('children', [])
if child['string'].startswith(name):
return child['string'][len(name):].lstrip(': ')
def get_last_edit_time(item):
if isinstance(item, list):
edit_time = 0
else:
edit_time = max(
item.get('create-time', 0),
item.get('edit-time', 0),
)
for child in get_children_of(item):
child_edit_time = max(
child.get('create-time', 0),
child.get('edit-time', 0),
)
edit_time = max(edit_time, child_edit_time)
return edit_time
def as_text(item, clean=False):
"""Handles strings, lists of children, children, and pages."""
if isinstance(item, str):
if clean:
return clean_brackets(item)
return item
if isinstance(item, list):
return '\n\n'.join(as_text(child, clean=clean) for child in item)
text = item['string']
if clean:
text = clean_brackets(text)
texts = [text]
for child in get_children_of(item):
text = child['string']
if clean:
text = clean_brackets(text)
texts.append(text)
return '\n\n'.join(texts)
def clean_brackets(text):
return re.sub(r'\[\[(.*?)\]\]', r'\1', text)
def timestamp_as_date(timestamp):
return datetime.datetime.fromtimestamp(timestamp / 1000)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment