1313from notion_client import Client
1414
1515NOTION_RICH_TEXT_LIMIT = 2000
16+ NOTION_BLOCKS_BATCH_SIZE = 100 # Max blocks per request to avoid 413 errors
1617
1718
1819_Block = dict [str , Any ]
@@ -100,6 +101,45 @@ def _find_existing_page_by_title(
100101 return None
101102
102103
104+ def _upload_blocks_in_batches (
105+ notion_client : Client ,
106+ page_id : str ,
107+ blocks : list [_Block ],
108+ batch_size : int = NOTION_BLOCKS_BATCH_SIZE ,
109+ ) -> None :
110+ """
111+ Upload blocks to a page in batches to avoid 413 errors.
112+ """
113+ if not blocks :
114+ return
115+
116+ total_blocks = len (blocks )
117+ sys .stderr .write (
118+ f"Uploading { total_blocks } blocks in batches of { batch_size } ...\n "
119+ )
120+
121+ for i in range (0 , total_blocks , batch_size ):
122+ batch = blocks [i : i + batch_size ]
123+ batch_num = (i // batch_size ) + 1
124+ total_batches = (total_blocks + batch_size - 1 ) // batch_size
125+
126+ sys .stderr .write (
127+ f"Uploading batch { batch_num } /{ total_batches } "
128+ f"({ len (batch )} blocks)...\n "
129+ )
130+
131+ try :
132+ notion_client .blocks .children .append (
133+ block_id = page_id ,
134+ children = batch ,
135+ )
136+ except Exception as e :
137+ sys .stderr .write (f"Error uploading batch { batch_num } : { e } \n " )
138+ raise
139+
140+ sys .stderr .write (f"Successfully uploaded all { total_blocks } blocks.\n " )
141+
142+
103143def main () -> None :
104144 """
105145 Main entry point for the upload command.
@@ -124,6 +164,14 @@ def main() -> None:
124164 help = "Title of the new page" ,
125165 required = True ,
126166 )
167+ parser .add_argument (
168+ "--batch-size" ,
169+ help = (
170+ f"Number of blocks per batch (default: { NOTION_BLOCKS_BATCH_SIZE } )"
171+ ),
172+ type = int ,
173+ default = NOTION_BLOCKS_BATCH_SIZE ,
174+ )
127175 args = parser .parse_args ()
128176
129177 # Initialize Notion client
@@ -156,22 +204,51 @@ def main() -> None:
156204 if child_id :
157205 notion .blocks .delete (block_id = child_id )
158206
159- notion .blocks .children .append (
160- block_id = existing_page_id ,
161- children = processed_contents ,
207+ _upload_blocks_in_batches (
208+ notion_client = notion ,
209+ page_id = existing_page_id ,
210+ blocks = processed_contents ,
211+ batch_size = args .batch_size ,
162212 )
163213 sys .stdout .write (
164214 f"Updated existing page: { args .title } (ID: { existing_page_id } )"
165215 )
166216 else :
167- new_page : Any = notion .pages .create (
168- parent = {"type" : "page_id" , "page_id" : args .parent_page_id },
169- properties = {
170- "title" : {"title" : [{"text" : {"content" : args .title }}]},
171- },
172- children = processed_contents ,
173- )
174- page_id = new_page .get ("id" , "unknown" )
217+ # For new pages, we still need to handle large content
218+ # Split into initial creation + additional batches if needed
219+ if len (processed_contents ) > args .batch_size :
220+ # Create page with first batch
221+ initial_batch = processed_contents [: args .batch_size ]
222+ remaining_blocks = processed_contents [args .batch_size :]
223+
224+ new_page : Any = notion .pages .create (
225+ parent = {"type" : "page_id" , "page_id" : args .parent_page_id },
226+ properties = {
227+ "title" : {"title" : [{"text" : {"content" : args .title }}]},
228+ },
229+ children = initial_batch ,
230+ )
231+ page_id = new_page .get ("id" , "unknown" )
232+
233+ # Upload remaining blocks in batches
234+ if remaining_blocks :
235+ _upload_blocks_in_batches (
236+ notion_client = notion ,
237+ page_id = page_id ,
238+ blocks = remaining_blocks ,
239+ batch_size = args .batch_size ,
240+ )
241+ else :
242+ # Small enough to create in one go
243+ new_page_small : Any = notion .pages .create (
244+ parent = {"type" : "page_id" , "page_id" : args .parent_page_id },
245+ properties = {
246+ "title" : {"title" : [{"text" : {"content" : args .title }}]},
247+ },
248+ children = processed_contents ,
249+ )
250+ page_id = new_page_small .get ("id" , "unknown" )
251+
175252 sys .stdout .write (f"Created new page: { args .title } (ID: { page_id } )" )
176253
177254
0 commit comments