1
1
import argparse
2
2
import os
3
3
import sys
4
- from pathlib import Path
5
4
import asyncio
5
+ from pathlib import Path
6
6
from aiohttp import ClientSession
7
7
from notion_client import AsyncClient
8
8
from rich .console import Console
9
9
from rich .table import Table
10
10
from rich .text import Text
11
+ from rich import box
11
12
from .utils import setup_logger , read_config , logger , config
12
13
from .translate .notion_import import NotionImporter
13
14
from .translate .batch_import import BatchImport
15
+ from .translate .import_stats import StatLevel
14
16
console = Console ()
15
17
16
18
@@ -27,22 +29,62 @@ def parse_args():
27
29
return parser .parse_args ()
28
30
29
31
30
- def print_fail_details (failed_files ):
31
- if len (failed_files ) == 0 :
32
+ def print_single_stats (stat ):
33
+ if stat .get_level () == StatLevel .EXCEPTION .value :
34
+ text = Text (f"Failed to import { stat .filename } " , style = "default" )
35
+ text .append (f"\n Exception: { stat .exception } " , style = "red" )
36
+ console .print (text )
32
37
return
33
- table = Table (title = f"\n Failed Detail\n Log path: { config .get ('log_path' )} " )
34
- table .add_column ("File Name" , justify = "left" , style = "cyan" , no_wrap = True )
35
- table .add_column ("Fail Reason" , justify = "left" , style = "red" , no_wrap = True )
36
-
37
- for row in failed_files :
38
- table .add_row (str (row [0 ].name ), str (row [1 ]))
38
+
39
+ title = f"{ stat .filename } " if stat .filename else "Import Result (Loss filename)"
40
+ style = "default"
41
+ if stat .get_level () == StatLevel .LOSS .value :
42
+ title += " (Loss some content)"
43
+ style = "yellow"
44
+ elif stat .get_level () == StatLevel .SUCC .value :
45
+ title += "(Import successfully)"
46
+ style = "green"
47
+
48
+ table = Table (title = title , title_style = style , expand = True , box = box .HEAVY_HEAD , show_lines = True )
49
+ table .add_column ("Item" , justify = "right" , style = "default" )
50
+ table .add_column ("Html" , style = "default" )
51
+ table .add_column ("Notion" , justify = "left" , style = "default" )
52
+ table .add_row ("Text Len" , str (stat .text_count ), str (stat .notion_text_count ))
53
+ table .add_row ("Image Count" , str (stat .image_count ), str (stat .notion_image_count ))
54
+ if stat .skip_tag :
55
+ table .add_row ("Skip Tag Count" , "" , 'Detail: [yellow]' + ";" .join ([repr (s )
56
+ for s in stat .skip_tag ])[:2000 ] + "[/yellow]" )
57
+
39
58
console .print (table )
40
59
41
- text = Text ("\n If you need help, please submit an " )
42
- link = Text ("issue" , style = "cyan underline link https://github.com/selfboot/html2notion/issues" )
43
- text .append (link )
44
- text .append (" on gitHub.\n " )
45
- console .print (text )
60
+
61
+ def print_batch_stats (batch_import ):
62
+ all_files = batch_import .all_files
63
+ batch_stats = batch_import .batch_stats
64
+ success_stats = [stat for stat in batch_stats if not stat .get_level () == StatLevel .SUCC .value ]
65
+ if len (success_stats ) == len (all_files ):
66
+ console .print (f"All files migrated successfully and there is no data loss." , style = "green" )
67
+
68
+ failed_stats = [stat for stat in batch_stats if stat .get_level () == StatLevel .EXCEPTION .value ]
69
+ if failed_stats :
70
+ table = Table (title = f"\n Import Fail Exception Detail\n Log path: { config .get ('log_path' )} " , expand = True , box = box .HEAVY_HEAD , show_lines = True )
71
+ table .add_column ("File Name" , justify = "left" , style = "default" )
72
+ table .add_column ("Fail Reason" , justify = "left" , style = "default" )
73
+
74
+ for stat in failed_stats :
75
+ table .add_row (str (stat .filename ), str (stat ))
76
+ console .print (table )
77
+
78
+ less_stats = [stat for stat in batch_stats if stat .get_level () == StatLevel .LOSS .value ]
79
+ if less_stats :
80
+ table = Table (title = f"\n Import Data Loss Detail (You can use --file to import single file for more info)\n " , expand = True , box = box .HEAVY_HEAD , show_lines = True )
81
+ table .add_column ("File Name" , justify = "left" , style = "default" )
82
+ table .add_column ("Loss Detail" , justify = "left" , style = "default" )
83
+
84
+ for stat in less_stats :
85
+ table .add_row (str (stat .filename ), str (stat ))
86
+ console .print (table )
87
+
46
88
47
89
48
90
def prepare_env (args : argparse .Namespace ):
@@ -70,13 +112,9 @@ async def import_single_file(file):
70
112
async with ClientSession () as session :
71
113
async with AsyncClient (auth = notion_api_key ) as notion_client :
72
114
notion_importer = NotionImporter (session , notion_client )
73
- try :
74
- result = await notion_importer .process_file (file )
75
- logger .info (f"Finish file { file } " )
76
- return ("succ" , result )
77
- except Exception as e :
78
- logger .error (f"Error processing { file } : { str (e )} " )
79
- return ("fail" , str (e ))
115
+ await notion_importer .process_file (file )
116
+ return notion_importer .import_stats
117
+
80
118
81
119
def main ():
82
120
args = parse_args ()
@@ -86,32 +124,22 @@ def main():
86
124
dir_path = Path (args .dir ) if args .dir else None
87
125
max_concurrency = args .batch
88
126
if file_path and file_path .is_file ():
89
- result = asyncio .run (import_single_file (file_path ))
90
- text = Text ("Single file " , style = "default" )
91
- text .append (f"{ file_path } " , style = "cyan" )
92
- if result [0 ] == "fail" :
93
- text .append ("save to notion failed." , style = "default" )
94
- text .append (f"\n { result [1 ]} " , style = "red" )
95
- else :
96
- text .append ("save to notion success." , style = "default" )
97
- console .print (text )
127
+ stats = asyncio .run (import_single_file (file_path ))
128
+ print_single_stats (stats )
98
129
elif dir_path and dir_path .is_dir ():
99
130
logger .info (f"Begin save all html files in the dir: { dir_path } ." )
100
131
batch_import = BatchImport (dir_path , max_concurrency )
101
132
result = asyncio .run (batch_import .process_directory ())
102
133
logger .info (f"Finish save all html files in the dir: { dir_path } .\n { result } " )
103
-
104
- if len (batch_import .success_files ) == len (batch_import .all_files ):
105
- console .print (f"All files processed success." , style = "green" )
106
-
107
- print_fail_details (batch_import .failed_files )
134
+ print_batch_stats (batch_import )
108
135
else :
109
136
text = Text ("The parameters provided are incorrect, please check." , style = "red" )
110
- text .append ("\n If you need help, please submit an " , style = "default" )
111
- link = Text ("issue" , style = "cyan underline link https://github.com/selfboot/html2notion/issues" )
112
- text .append (link )
113
- text .append (" on gitHub." , style = "default" )
114
- console .print (text )
137
+
138
+ text = Text ("\n If you need help, please submit an " , style = "default" )
139
+ link = Text ("issue" , style = "cyan underline link https://github.com/selfboot/html2notion/issues" )
140
+ text .append (link )
141
+ text .append (" on gitHub." , style = "default" )
142
+ console .print (text )
115
143
return
116
144
117
145
0 commit comments