Skip to content

Instantly share code, notes, and snippets.

@nicoandmee
Last active July 17, 2023 19:37
Show Gist options
  • Save nicoandmee/fc70f2558969ed0d67c543a5cada3ce5 to your computer and use it in GitHub Desktop.
Save nicoandmee/fc70f2558969ed0d67c543a5cada3ce5 to your computer and use it in GitHub Desktop.
obsidian -> keycombiner
import csv
import json
import os
import time
from typing import Dict, List, Tuple
import click
import httpx
import pandas as pd
from icecream import ic
def load_cookies():
if not os.path.exists('obsidian_cookie.json'):
# throw
click.echo(click.style('Cookie file not found!', bg='blue', fg='red'))
return None
else:
with open('obsidian_cookie.json', 'r') as f:
return json.load(f)
def json_to_csv(json_data, csv_file) -> None:
df = pd.json_normalize(json_data)
df.to_csv(csv_file, index=False)
def get_hotkeys(keybindings) -> Dict:
with open(keybindings, 'r') as f:
return json.load(f)
def upload(collection, csv_filename, cookies=None) -> None:
"""Upload a CSV, replacing existing content
"""
headers = {
'Host': 'keycombiner.com',
'Connection': 'keep-alive',
'sec-ch-ua': '"Not.A/Brand";v="8", "Chromium";v="114", "Google Chrome";v="114"',
'sec-ch-ua-mobile': '?0',
'sec-ch-ua-platform': '"macOS"',
'Upgrade-Insecure-Requests': '1',
'Origin': 'https://keycombiner.com',
'User-Agent': 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/114.0.0.0 Safari/537.36',
'Accept': 'text/html,application/xhtml+xml,application/xml;q=0.9,image/avif,image/webp,image/apng,*/*;q=0.8,application/signed-exchange;v=b3;q=0.7',
'Accept-Language': 'en-US,en;q=0.9',
}
response = httpx.post(
f'https://keycombiner.com/collecting/combinations/{collection}/import/',
headers=headers,
files={'file': open(csv_filename, 'rb')},
cookies=cookies,
)
print(f'/POST {response.url} -- ${response.status_code}')
@click.command()
@click.option('--collection', default=22069, help='Collection ID to import into')
@click.option('--vault', default='nicomd', help='Name of your obsidian vault')
def run(collection, vault):
# sourcery skip: merge-list-append, move-assign-in-block
kb_path = os.path.join(os.path.expanduser('~'), vault, '.obsidian', 'hotkeys.json')
click.echo(click.style(
f'Looking for your hotkeys in {kb_path}', bg='blue', fg='green'))
# load session from keycombiner.com
cookies = load_cookies()
if cookies is None:
return
# load keybindings from obsidian vault
keybindings = get_hotkeys(kb_path)
ic(keybindings)
# massage them to keycombiners schema
data = []
for key, value in keybindings.items():
ctx = key
# value is an array of dicts with keys: modifiers, key
for entry in value:
print(entry)
combo = '+'.join(entry['modifiers']) + '+' + entry['key']
combo = combo.lower()
data.append({'Description': ctx, 'Keys': combo, 'Keys (macOS)': combo, 'Context': ctx, 'Category': ctx}) # noqa: E501
# export as csv
filename = f'combos-{collection}.csv'
json_to_csv(data, filename)
click.echo(click.style(f'Saved {len(data)} to {filename}', bg='blue', fg='green'))
# now, import the csv into the collection
# TODO: automate this
# upload(collection, filename, cookies=cookies)
click.echo(click.style(f'Success! Exported {len(data)} keybindings. Import them into the target collection.'))
if __name__ == '__main__':
run()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment