
    i                        d Z ddlmZ ddlZddlZddlZddlZddlmZmZ ddl	m
Z
 ddlmZ  e
e          j        dz  ZdZddZddZddZddZddZddZedk    r e e                      dS ) aI  Append a new <item> to feed.xml.

Usage:
    python3 add.py --title "..." --body-file note.html [--slug 2026-05-03-foo]
    echo "<p>html body</p>" | python3 add.py --title "..."

The body is HTML and goes inside <description><![CDATA[ ... ]]></description>.
GUID is stable based on the slug (or auto-generated date+title hash).
    )annotationsN)datetimetimezone)Path)escapezfeed.xmlzhttps://example.invalid/ai-funtitlestrreturnc                    |                                  }t          j        dd|t          j                  }t          j        dd|                              d          }|d d         pdS )Nz[^\w\s-] )flagsz[\s_-]+-<   item)lowerresubUNICODEstrip)r   ss     0/workspace/projects/group-5104415961/feed/add.pyslugifyr      s]    A
{B444A
z3""((--ASbS6V    c                    t          j        t          j                                      d          }| dt          |            S )Nz%Y-%m-%dr   )r   nowr   utcstrftimer   )r   todays     r   	make_slugr       s;    L&&//
;;E&&genn&&&r   dtr   c                    g d}g d}||                                            d| j        dd|| j        dz
            d| j         d|                     d           d
S )	N)JanFebMarAprMayJunJulAugSepOctNovDec)MonTueWedThuFriSatSunz, 02d    z%H:%M:%Sz +0000)weekdaydaymonthyearr   )r    monthsdayss      r   rfc822r>   %   s    7 7 7F<<<DBJJLL! 9 9RVG 9 9&!2D 9 9w9 9Z009 9 9 :r   	body_htmlslugpubc                    d| }dt          |            dt           d| d| dt          |           d|                                 dS )	Nzai-fun-z    <item>
      <title>z</title>
      <link>/z(</link>
      <guid isPermaLink="false">z</guid>
      <pubDate>z(</pubDate>
      <description><![CDATA[
z%
      ]]></description>
    </item>
)r   BASE_URLr>   r   )r   r?   r@   rA   guids        r   
build_itemrF   .   s    TD	u	 		 	"&	 	 .2	 	 !++		 	
 *3):):	 	 	r   feed_xmlitem_xmlc                    t          j        ddt          |           d| d          } t          j        d|           }|st	          d          |                                }| d |         |z   | |d          z   S )Nz$<lastBuildDate>[^<]*</lastBuildDate>z<lastBuildDate>z</lastBuildDate>r7   )countz</lastBuildDate>\s*\nz#feed.xml: <lastBuildDate> not found)r   r   r>   search
SystemExitend)rG   rH   rA   markerposs        r   insert_itemrP   ;   s    v/7&++777	  H Y/::F @>???
**,,CDSD>H$x~55r   intc                    t          j                    } |                     dd           |                     dd           |                     dd           |                                 }|j        r)t          |j                                      d	
          }nt          j        	                                }|
                                st          d          |j        pt          |j                  }t          j        t"          j                  }t&                              d	
          }t)          |j        |||          }t+          |||          }t&                              |d	
           t/          d|            dS )Nz--titleT)requiredz--body-filez)path to HTML body; if omitted reads stdin)helpz--slugz%custom slug (default: <date>-<title>)zutf-8)encodingz
empty bodyzadded: r   )argparseArgumentParseradd_argument
parse_args	body_filer   	read_textsysstdinreadr   rL   r@   r   r   r   r   r   r   	FEED_PATHrF   rP   
write_textprint)pargsbodyr@   rA   rG   rH   new_feeds           r   mainrf   K   sT   !!ANN9tN,,,NN='RNSSSNN8"INJJJ<<>>D~  DN##--w-??y~~::<< '&&&9-	$*--D
,x|
$
$C""G"44H$*dD#66H8Xs33HG444	
D

1r   __main__)r   r	   r
   r	   )r    r   r
   r	   )
r   r	   r?   r	   r@   r	   rA   r   r
   r	   )rG   r	   rH   r	   rA   r   r
   r	   )r
   rQ   )__doc__
__future__r   rV   hashlibr   r\   r   r   pathlibr   xml.sax.saxutilsr   __file__parentr_   rD   r   r   r>   rF   rP   rf   __name__rL    r   r   <module>rq      s@    # " " " " "   				 



 ' ' ' ' ' ' ' '       # # # # # #DNN!J.	+   ' ' ' '
: : : :
 
 
 
6 6 6 6    2 z
*TTVV

 r   