1+ # ABOUTME: Debug tool to inspect the raw structure of a post
2+ # ABOUTME: Returns detailed information about post fields and content structure
3+
4+ import json
5+ from typing import Dict , Any
6+
7+ async def debug_post_structure (post_handler , post_id : str ) -> Dict [str , Any ]:
8+ """Debug tool to inspect post structure
9+
10+ Args:
11+ post_handler: The PostHandler instance
12+ post_id: The ID of the post to inspect
13+
14+ Returns:
15+ Detailed structure information
16+ """
17+ try :
18+ # Get the raw post data
19+ post = post_handler .client .get_draft (post_id )
20+
21+ # Build debug info
22+ debug_info = {
23+ "post_id" : post_id ,
24+ "post_type" : type (post ).__name__ ,
25+ "post_keys" : list (post .keys ()) if isinstance (post , dict ) else "Not a dict" ,
26+ "has_body" : "body" in post if isinstance (post , dict ) else False ,
27+ "has_draft_body" : "draft_body" in post if isinstance (post , dict ) else False ,
28+ "title_fields" : {},
29+ "body_analysis" : {},
30+ "sample_content" : {}
31+ }
32+
33+ if isinstance (post , dict ):
34+ # Check title fields
35+ for field in ["title" , "draft_title" , "subtitle" , "draft_subtitle" ]:
36+ if field in post :
37+ debug_info ["title_fields" ][field ] = post [field ][:50 ] + "..." if len (str (post [field ])) > 50 else post [field ]
38+
39+ # Analyze body structure
40+ for body_field in ["body" , "draft_body" ]:
41+ if body_field in post :
42+ body = post [body_field ]
43+ debug_info ["body_analysis" ][body_field ] = {
44+ "type" : type (body ).__name__ ,
45+ "is_dict" : isinstance (body , dict ),
46+ "keys" : list (body .keys ())[:10 ] if isinstance (body , dict ) else None ,
47+ "length" : len (body ) if isinstance (body , (str , list , dict )) else None
48+ }
49+
50+ # If it's a dict with blocks
51+ if isinstance (body , dict ) and "blocks" in body :
52+ blocks = body ["blocks" ]
53+ debug_info ["body_analysis" ][body_field ]["blocks_info" ] = {
54+ "type" : type (blocks ).__name__ ,
55+ "count" : len (blocks ) if isinstance (blocks , list ) else 0 ,
56+ "first_block" : blocks [0 ] if isinstance (blocks , list ) and blocks else None
57+ }
58+
59+ # Sample content
60+ if isinstance (body , str ):
61+ debug_info ["sample_content" ][body_field ] = body [:200 ] + "..." if len (body ) > 200 else body
62+ elif isinstance (body , dict ):
63+ debug_info ["sample_content" ][body_field ] = json .dumps (body , indent = 2 )[:500 ] + "..."
64+
65+ return debug_info
66+
67+ except Exception as e :
68+ return {
69+ "error" : str (e ),
70+ "error_type" : type (e ).__name__
71+ }
0 commit comments