CacherApp / cacher-cli

The command line interface to Cacher.
111 stars 15 forks source link

Feature request: Import notes from json #1

Closed pykong closed 6 years ago

pykong commented 6 years ago

I would really like to import the batch of snippets from the json file I have exported from GistBox. If there were any API documentation I could do it myself via a self-written script probably. Yet, as there is now this CLI tool for cacher.io in the making, maybe it could implement such import feature.

jookyboi commented 6 years ago

@pykong That is a great point and a snippets:import feature is slated for the next release.

jookyboi commented 6 years ago

In the meantime, you can do the following:

  1. Write a script to parse the exported JSON file.
  2. Import it into your Cacher personal library using a command like:
    cacher snippets:add [path_to_file] -t=[title] -d=[description] -q
pykong commented 6 years ago

@jookyboi Thanks for your suggestion. I will try that out. Keep on the good work with cacher! IMHO by far the best snippet manager I have found.

pykong commented 6 years ago

Here is such quick and dirty script written in python, in case anyone in use comes across it.

How to use: 1.) Install and Setup Cacher-Cli as explained. 2.) Save below snippet to gist_imp.py 3.) Run like: python3 gist_imp.py path/to/my/gist_box_export_file.json 4.) Enjoy, see the magic happen...

Limitations:

import json
import os
import subprocess
import sys
from tempfile import TemporaryDirectory

with open(sys.argv[1], "r") as f:
    js = json.load(f)
    groups = js["groups"]

    with TemporaryDirectory() as dirpath:        
        for group in groups:
            gists = group["gists"]
            for g in gists:
                description = g["description"]
                files = g["files"]
                for f in files:
                    content = f["content"]
                    filename = f["filename"]
                    title = description[:20]

                    with open(filename, "w") as target:
                        target.write(content)
                    cmd = (f"cacher snippets:add {filename}"
                           f" -d '{description}' -t '{title}'")
                    print(cmd)
                    subprocess.run(cmd, shell=True, cwd=dirpath)