|
| 1 | +import glob |
| 2 | +import json |
| 3 | +import os |
| 4 | + |
| 5 | +import click |
| 6 | +import nbformat |
| 7 | +import requests |
| 8 | +from nbconvert import MarkdownExporter |
| 9 | + |
| 10 | +README_AUTH = os.getenv('README_AUTH') |
| 11 | +README_ENDPOINT = "https://dash.readme.com/api/v1/docs" |
| 12 | +README_DOC_ENDPOINT = "https://dash.readme.com/api/v1/docs/" |
| 13 | +CATEGORY_ID = '61fb645198ad91004246bd5f' |
| 14 | +CATEGORY_SLUG = 'tutorials' |
| 15 | + |
| 16 | + |
| 17 | +def upload_doc(path, section): |
| 18 | + title = path.split('/')[-1].replace(".ipynb", '').capitalize().replace('_', ' ') |
| 19 | + |
| 20 | + with open(path) as fb: |
| 21 | + nb = nbformat.reads(json.dumps(json.load(fb)), as_version=4) |
| 22 | + |
| 23 | + nb.cells = [nb.cells[1]] + nb.cells[3:] |
| 24 | + nb.cells[0]['source'] += '\n' |
| 25 | + exporter = MarkdownExporter() |
| 26 | + |
| 27 | + body, resources = exporter.from_notebook_node(nb) |
| 28 | + |
| 29 | + payload = { |
| 30 | + "hidden": True, |
| 31 | + "title": title.replace(' ', '-') + f'-{section["slug"]}', |
| 32 | + "category": CATEGORY_ID, |
| 33 | + "parentDoc": section['id'], |
| 34 | + "body": body |
| 35 | + } |
| 36 | + |
| 37 | + headers = { |
| 38 | + "Accept": "application/json", |
| 39 | + "Content-Type": "application/json", |
| 40 | + "Authorization": README_AUTH |
| 41 | + } |
| 42 | + |
| 43 | + response = requests.post(README_ENDPOINT, json=payload, headers=headers) |
| 44 | + response.raise_for_status() |
| 45 | + data = response.json() |
| 46 | + change_name(data['slug'], title, headers) |
| 47 | + |
| 48 | + |
| 49 | +def make_sections(sections): |
| 50 | + for section in sections: |
| 51 | + print(section) |
| 52 | + payload = { |
| 53 | + "hidden": True, |
| 54 | + "order": section['order'], |
| 55 | + "title": section['title'].replace(' ', '-') + '-nb-section', |
| 56 | + "category": CATEGORY_ID |
| 57 | + } |
| 58 | + headers = { |
| 59 | + "Accept": "application/json", |
| 60 | + "Content-Type": "application/json", |
| 61 | + "Authorization": README_AUTH |
| 62 | + } |
| 63 | + |
| 64 | + response = requests.post(README_ENDPOINT, json=payload, headers=headers) |
| 65 | + data = response.json() |
| 66 | + |
| 67 | + section['id'] = data['id'] |
| 68 | + section['slug'] = data['slug'] |
| 69 | + |
| 70 | + change_name(data["slug"], section['title'], headers) |
| 71 | + |
| 72 | + return sections |
| 73 | + |
| 74 | + |
| 75 | +def change_name(slug, title, headers): |
| 76 | + resp = requests.put( |
| 77 | + f'{README_DOC_ENDPOINT}/{slug}', |
| 78 | + json={ |
| 79 | + "title": title, |
| 80 | + "category": CATEGORY_ID |
| 81 | + }, |
| 82 | + headers=headers |
| 83 | + ) |
| 84 | + resp.raise_for_status() |
| 85 | + |
| 86 | + |
| 87 | +def erase_category_docs(cat_slug): |
| 88 | + headers = { |
| 89 | + "Accept": "application/json", |
| 90 | + "Authorization": README_AUTH |
| 91 | + } |
| 92 | + |
| 93 | + response = requests.request("GET", f'https://dash.readme.com/api/v1/categories/{cat_slug}/docs', headers=headers) |
| 94 | + docs = response.json() |
| 95 | + for doc in docs: |
| 96 | + for child in doc["children"]: |
| 97 | + resp = requests.delete(f'{README_DOC_ENDPOINT}/{child["slug"]}', headers=headers) |
| 98 | + resp = requests.delete(f'{README_DOC_ENDPOINT}/{doc["slug"]}', headers=headers) |
| 99 | + |
| 100 | + |
| 101 | +@click.command() |
| 102 | +@click.option('--config-path') |
| 103 | +# @click.option('--output-path') |
| 104 | +def main(config_path): |
| 105 | + # print(input_path) |
| 106 | + erase_category_docs(CATEGORY_SLUG) |
| 107 | + with open(config_path) as fb: |
| 108 | + config = json.load(fb) |
| 109 | + config = make_sections(config) |
| 110 | + |
| 111 | + for section in config: |
| 112 | + print(section, '\n------') |
| 113 | + for path in glob.glob(f'{section["dir"]}/**.ipynb'): |
| 114 | + print('*', path) |
| 115 | + upload_doc(path, section) |
| 116 | + print('-------') |
| 117 | + |
| 118 | + |
| 119 | +if __name__ == '__main__': |
| 120 | + main() |
0 commit comments