From 4d8ff55c0a858516c8cc2d5f30009ca0541115da Mon Sep 17 00:00:00 2001 From: echo724 Date: Tue, 22 Mar 2022 23:15:55 +0900 Subject: [PATCH] refactor: Refactoring ExportBlockCommand's exporting process with using Exporter class --- notion2md/console/commands/export_block.py | 113 ++++++++++----------- 1 file changed, 55 insertions(+), 58 deletions(-) diff --git a/notion2md/console/commands/export_block.py b/notion2md/console/commands/export_block.py index 739380a..90db772 100644 --- a/notion2md/console/commands/export_block.py +++ b/notion2md/console/commands/export_block.py @@ -1,19 +1,27 @@ import os -import shutil import sys import time from cleo.commands.command import Command from cleo.helpers import option -from notion2md.config import Config from notion2md.console.formatter import error from notion2md.console.formatter import status from notion2md.console.formatter import success from notion2md.console.ui.indicator import progress -from notion2md.convertor.block import BlockConvertor -from notion2md.notion_api import NotionClient -from notion2md.util import zip_dir +from notion2md.exporter.block import Exporter + + +class CLIExporter(Exporter): + def export(self, blocks): + with open( + os.path.join( + self._config.tmp_path, self._config.file_name + ".md" + ), + "w", + encoding="utf-8", + ) as output: + output.write(self.block_convertor.convert(blocks)) ARGS_NEW_KEY_MAP = { @@ -79,63 +87,52 @@ def error(self, msg): self.line_error(error(msg)) sys.exit(1) - def handle(self): - try: - notion_client = NotionClient() - except Exception as e: - self.error(e) + def _parse_args(self): args = {} for k, v in self.io.input.options.items(): if k in ARGS_NEW_KEY_MAP: args[ARGS_NEW_KEY_MAP[k]] = v else: pass - config = Config(**args) - if not config.target_id: - self.error("Notion2Md requires either id or url.") - exporter = BlockConvertor(config, notion_client, self.io) - # Directory Checking and Creating - if not os.path.exists(config.tmp_path): - os.makedirs(config.tmp_path) - if not os.path.exists(config.output_path): - os.makedirs(config.output_path) - start_time = time.time() - # Get actual blocks - self.line("") - with progress( - self.io, - status("Retrieving", "Notion blocks..."), - success("Retrieved", "Notion blocks..."), - ): - blocks = notion_client.get_children(config.target_id) - # Write(Export) Markdown file - self.success( - "Converting", f"{str(len(blocks))} blocks..." - ) - with open( - os.path.join(config.tmp_path, config.file_name + ".md"), - "w", - encoding="utf-8", - ) as output: - output.write(exporter.convert(blocks)) - self.success( - "Converted", - f"{str(len(blocks))} blocks to Markdown{f' ({time.time() - start_time:0.1f}s)' if not self.io.is_debug() else ''}", - ) - - # Compress Output files into a zip file - if not config.unzipped: - zip_dir( - os.path.join(config.output_path, config.file_name) + ".zip", - config.tmp_path, + return args + + def handle(self): + try: + args = self._parse_args() + exporter = CLIExporter(**args) + exporter.io = self.io + exporter.create_directories() + # Get actual blocks + self.line("") + with progress( + self.io, + status("Retrieving", "Notion blocks..."), + success("Retrieved", "Notion blocks..."), + ): + blocks = exporter.get_blocks() + # Write(Export) Markdown file + start_time = time.time() + self.success( + "Converting", f"{str(len(blocks))} blocks..." + ) + exporter.export(blocks) + self.success( + "Converted", + f"{str(len(blocks))} blocks to Markdown{f' ({time.time() - start_time:0.1f}s)' if not self.io.is_debug() else ''}", ) - shutil.rmtree(config.tmp_path) - extension = ".zip" - else: - extension = ".md" - # Result and Time Check - self.success( - "Exported", - f'"{config.file_name}{extension}" in "./{config.path_name}/"', - ) - self.line("") + + # Compress Output files into a zip file + if not exporter.config.unzipped: + exporter.make_zip() + extension = ".zip" + else: + extension = ".md" + # Result and Time Check + self.success( + "Exported", + f'"{exporter.config.file_name}{extension}" in "./{exporter.config.path_name}/"', + ) + self.line("") + + except Exception as e: + self.error(e)