-
Notifications
You must be signed in to change notification settings - Fork 2
/
extract_archive.py
executable file
·33 lines (24 loc) · 1002 Bytes
/
extract_archive.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
#!/usr/bin/env python
import json
import pathlib
import sys
from zipfile import ZipFile
archive_name = sys.argv[1]
base_path = pathlib.Path(sys.argv[2])
def extract_object(path, container, archive, level=0):
if container['object'] == 'container':
path.mkdir()
if level:
title = f"{'#' * level} {container['title']}\n\n"
else:
title = f"% {container['title']}\n\n"
extract_file(path / f"0-{container['slug']}.md", title.encode() + archive.read(container['introduction']))
#extract_file(path / 'x-conclusion.md', archive.read(container['conclusion']))
for i, child in enumerate(container['children'], 1):
p = path / f"{i}-{child['slug']}"
extract_object(p, child, archive, level+1)
def extract_file(path, content):
path.write_bytes(content)
with ZipFile(archive_name, 'r') as archive:
manifest = json.loads(archive.read('manifest.json'))
extract_object(base_path, manifest, archive)