o
    ñ…ØhP  ã                
   @   s|  d dl Z d dlmZmZmZmZ d dlmZ d dlmZ d dl	m
Z
 e e¡ZeeƒZdedefdd	„Zejd
dgd�defdd„ƒZejddgd�dd„ ƒZejddgd�dd„ ƒZe d¡dd„ ƒZe d¡dd„ ƒZedkr¼ze ¡  W n ey” Z ze eeƒ¡ edƒ W Y dZ[ndZ[ww e d¡ e d¡ e d ¡ e d!¡ e d"¡ e ¡ Zej d#i e¤Ž dS dS )$é    N)ÚFlaskÚjsonifyÚrequestÚmake_response)Úconfig)Úexport_notion_page_to_markdown)Úconvert_markdown_to_pdfÚtitleÚreturnc                 C   s~   | }t  dd|¡}t  dd|¡}t  dd|¡}t  dd|¡}| d¡}t|ƒdkr/|dd… }|r;|dks;| dd¡s=d	}|S )
a   
    Sanitize a string to make it safe for use as a filename.
    Only allows ASCII characters to avoid HTTP header encoding issues.
    
    Args:
        title: The original title string
        
    Returns:
        str: A sanitized filename-safe string with only ASCII characters
    z[<>:"/\\|?*]Ú_z[^a-zA-Z0-9\s\-_.]z\s+z_{2,}éd   NÚ ÚUntitled)ÚreÚsubÚstripÚlenÚreplace)r	   Ú	sanitized© r   ú /var/www/html/notion2html/app.pyÚsanitize_filename   s   
r   z/page/<page_id>ÚGET)ÚmethodsÚpage_idc              
   C   sl  zät  d| › �¡ tj dd¡ ¡ dk}|s$d|  ¡ v s"d|  ¡ v r$d}t  d|› �¡ |  d	d¡}d
|v r=| d
¡d }d|v rH| d¡d }t  d|› �¡ |rXt|ƒdk rat	ddiƒdfW S t
|ƒ\}}t  d|› d| › d�¡ |rÅz%t||ƒ\}}t  d|› �¡ t|ƒ}d|jd< d|› d�|jd< |W W S  tyÄ } zt  dt|ƒ› �¡ t	ddt|ƒ› �iƒdfW  Y d}~W S d}~ww t|ƒ}	|	› d�}
t  d |
› �¡ d!d|
› d�d"œ}|d#|fW S  t�y	 } zt  d$t|ƒ› �¡ t	dd%iƒdfW  Y d}~S d}~w t�y5 } z t  d&| › d't|ƒ› �¡ t	dd(t|ƒ› �iƒdfW  Y d}~S d}~ww ))aY  
    Export a Notion page to Markdown format or PDF format.
    
    Args:
        page_id: The ID of the Notion page to export
        
    Query Parameters:
        pdf: If set to 'true', returns PDF instead of Markdown
        
    Returns:
        The page content in Markdown or PDF format as a file download with page name as filename
    z!Received request to export page: Úpdfr   Útruez	&pdf=truez	?pdf=trueTzPDF output requested: Ú-ú&r   Ú?zCleaned page_id: é    Úerrorz>Invalid page ID format. Page ID should be a 32-character UUID.i�  zSuccessfully exported page 'z' (z) to MarkdownzSuccessfully converted to PDF: zapplication/pdfúContent-Typezattachment; filename="ú"úContent-DispositionzPDF conversion failed: zFailed to convert to PDF: éô  Nz.mdzSuggesting filename: ztext/markdown; charset=utf-8)r"   r$   éÈ   zConfiguration error: zBServer configuration error. Please check that NOTION_TOKEN is set.zError exporting page z: zFailed to export page: )ÚloggerÚinfor   ÚargsÚgetÚlowerr   Úsplitr   r   r   r   r   ÚheadersÚ	Exceptionr!   Ústrr   Ú
ValueError)r   Úpdf_requestedÚclean_page_idÚ
page_titleÚmarkdown_contentÚpdf_contentÚpdf_filenameÚresponseÚ	pdf_errorÚsafe_filenameÚfilenamer-   Úer   r   r   Úexport_page2   sz   ÿþ
ÿþ€þ

þÿþ€ÿþ€þr<   ú/healthc                   C   ó   t dddœƒdfS )zHealth check endpoint.ÚhealthyúNotion to Markdown Exporter)ÚstatusÚservicer&   ©r   r   r   r   r   Úhealth_checkŽ   ó   þýrD   ú/c                   C   s   t ddddddiddœƒd	fS )
z&Home endpoint with usage instructions.r@   zNSend GET request to /page/<page_id> to export a Notion page to Markdown or PDFz&/page/897e5a76ae524b489fdfe71f5945d1afz//page/897e5a76ae524b489fdfe71f5945d1af?pdf=truer   z=Optional. Set to "true" to get PDF output instead of Markdownr=   )rB   ÚusageÚexampleÚpdf_exampleÚ
parametersrD   r&   rC   r   r   r   r   Úhome–   s   ÿø	÷rK   é”  c                 C   r>   )zHandle 404 errors.zEndpoint not foundzGSend GET request to /page/<page_id> to export a Notion page to Markdown)r!   rG   rL   rC   ©r!   r   r   r   Ú	not_found¤   rE   rN   r%   c                 C   s$   t  dt| ƒ› �¡ tddiƒdfS )zHandle 500 errors.zInternal server error: r!   zInternal server errorr%   )r'   r!   r/   r   rM   r   r   r   Úinternal_error¬   s   ÿþrO   Ú__main__é   z.Starting Notion to Markdown Exporter server...zAvailable endpoints:z6  GET /page/<page_id> - Export Notion page to Markdownz  GET /health - Health checkz  GET / - Service informationr   )!r   Úflaskr   r   r   r   r   Únotion_to_mdr   Ú	md_to_pdfr   Ú
get_loggerÚ__name__r'   Úappr/   r   Úrouter<   rD   rK   ÚerrorhandlerrN   rO   Úvalidate_required_configr0   r;   r!   Úexitr(   Úget_flask_configÚflask_configÚrunr   r   r   r   Ú<module>   sD    
%[



€þ




ð