Skip to content

Instantly share code, notes, and snippets.

@YuzuRyo61
Created May 13, 2023 04:31
Show Gist options
  • Save YuzuRyo61/624d888cf90d08ac2982cdad19fda307 to your computer and use it in GitHub Desktop.
Save YuzuRyo61/624d888cf90d08ac2982cdad19fda307 to your computer and use it in GitHub Desktop.
Scrapbox exported data convert to text files (Abridged edition)
#!/usr/bin/env python3
import argparse
import logging
import os
import sys
import json
import re
logging.basicConfig(
level=logging.INFO,
stream=sys.stderr,
)
parser = argparse.ArgumentParser(
prog="Scrapbox to Text",
description="Exported Scrapbox data convert to text files.",
)
parser.add_argument("filename", type=argparse.FileType("r"))
parser.add_argument("-o", "--output", help="Output folder")
# parser.add_argument("-v", "--verbose", action="store_true")
if __name__ == '__main__':
args = parser.parse_args()
try:
data = json.load(args.filename)
except json.JSONDecodeError:
logging.critical("JSON decode error.", exc_info=sys.exc_info())
sys.exit(1)
if args.output is None:
base_folder = os.path.splitext(os.path.basename(args.filename.name))[0]
else:
base_folder = args.output
try:
os.makedirs(base_folder, exist_ok=False)
os.chdir(base_folder)
except FileExistsError:
logging.critical("Base folder is already exists.")
sys.exit(1)
for dp in data["pages"]:
export_filename = re.sub(r'[\\/:*?"<>|]+', '-', f"{dp['title']}.txt")
with open(export_filename, mode="w", encoding="utf-8") as fs:
fs.write("\n".join(dp["lines"][1:]))
logging.info(f"Exported: {export_filename}")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment