
    k"g=                         d Z ddlZddlZddlmZ ddlmZ ddlmZ ddl	m
Z
 d Zd Zd	 Z G d
 d      Z G d d      Z G d de      Z G d de      Z G d de      Z G d de      ZeZy)a  
Syndication feed generation library -- used for generating RSS, etc.

Sample usage:

>>> from django.utils import feedgenerator
>>> feed = feedgenerator.Rss201rev2Feed(
...     title="Poynter E-Media Tidbits",
...     link="http://www.poynter.org/column.asp?id=31",
...     description="A group blog by the sharpest minds in online journalism.",
...     language="en",
... )
>>> feed.add_item(
...     title="Hello",
...     link="http://www.holovaty.com/test/",
...     description="Testing."
... )
>>> with open('test.rss', 'w') as fp:
...     feed.write(fp, 'utf-8')

For definitions of the different versions of RSS, see:
https://web.archive.org/web/20110718035220/http://diveintomark.org/archives/2004/02/04/incompatible-rss
    N)StringIO)urlparse)
iri_to_uri)SimplerXMLGeneratorc                     t        | t        j                        s2t        j                  j                  | t        j                               } t        j
                  j                  |       S N)
isinstancedatetimecombinetimeemailutilsformat_datetimedates    S/var/www/html/djangosite/lib/python3.12/site-packages/django/utils/feedgenerator.pyrfc2822_dater   "   sE    dH--.  ((x}}?;;&&t,,    c                     t        | t        j                        s2t        j                  j                  | t        j                               } | j	                         | j                         dz   S dz   S )NZ )r	   r
   r   r   	isoformat	utcoffsetr   s    r   rfc3339_dater   (   sV    dH--.  ((x}}?>>dnn&6&>sGGBGGr   c                     t        |       }d}|d|j                  d      z  }d|j                  |d|j                  d|j                  S )z
    Create a TagURI.

    See
    https://web.archive.org/web/20110514113830/http://diveintomark.org/archives/2004/05/28/howto-atom-id
    r   z,%sz%Y-%m-%dztag::/)r   strftimehostnamepathfragment)urlr   bitsds       r   get_tag_urir%   .   sG     C=D
ADMM*--#}}aDMMJJr   c                   |    e Zd ZdZ	 	 	 	 	 	 	 	 	 	 ddZ	 	 	 	 	 	 	 	 	 	 	 	 ddZd Zd Zd Zd Z	d	 Z
d
 Zd Zd Zy)SyndicationFeedzGBase class for all syndication feeds. Subclasses should provide write()Nc                 0   d }|	xr |	D cg c]  }t        |       c}}	 ||      t        |       ||       ||       ||       ||      t        |       ||      |	xs dt        |
       ||      |xs | ||      d|| _        g | _        y c c}w )Nc                      | t        |       S | S r   strss    r   to_strz(SyndicationFeed.__init__.<locals>.to_strP       ]3q611r    )titlelinkdescriptionlanguageauthor_emailauthor_nameauthor_linksubtitle
categoriesfeed_urlfeed_copyrightidttl)r+   r   feeditems)selfr1   r2   r3   r4   r5   r6   r7   r8   r9   r:   r;   	feed_guidr=   kwargsr.   cs                    r   __init__zSyndicationFeed.__init__?   s    "	2  @Z$@SV$@
E]t$!+.x("<0!+.%k2x($*"8,$^4#t#;
 
	  
# %As   Bc                 6   d }|xr |D cg c]
  } ||       c}}| j                   j                   ||      t        |       ||       ||       ||      t        |      || ||       ||	      |
|xs d|xs d ||       ||      d|       yc c}w )z
        Add an item to the feed. All args are expected to be strings except
        pubdate and updateddate, which are datetime.datetime objects, and
        enclosures, which is an iterable of instances of the Enclosure class.
        c                      | t        |       S | S r   r*   r,   s    r   r.   z(SyndicationFeed.add_item.<locals>.to_str   r/   r   r0   )r1   r2   r3   r5   r6   r7   pubdateupdateddatecomments	unique_idunique_id_is_permalink
enclosuresr9   item_copyrightr=   N)r?   appendr   )r@   r1   r2   r3   r5   r6   r7   rG   rI   rJ   rK   r9   rM   r=   rH   rL   rB   r.   rC   s                      r   add_itemzSyndicationFeed.add_itemf   s    2	2  C
$C1VAY$C


"4(%k2 &| 4%k2)+6"*"8,#I.*@(.B(.B"("8c{  !	
 %Ds   Bc                 ,    t        | j                        S r   )lenr?   r@   s    r   	num_itemszSyndicationFeed.num_items   s    4::r   c                     i S )zx
        Return extra attributes to place on the root (i.e. feed/channel) element.
        Called from write().
        r0   rR   s    r   root_attributeszSyndicationFeed.root_attributes   s	    
 	r   c                      y)zd
        Add elements in the root (i.e. feed/channel) element. Called
        from write().
        Nr0   r@   handlers     r   add_root_elementsz!SyndicationFeed.add_root_elements   s    
 	r   c                     i S )zZ
        Return extra attributes to place on each item (i.e. item/entry) element.
        r0   )r@   items     r   item_attributeszSyndicationFeed.item_attributes   s	     	r   c                      y)zF
        Add elements on each item (i.e. item/entry) element.
        Nr0   r@   rX   r[   s      r   add_item_elementsz!SyndicationFeed.add_item_elements   s     	r   c                     t        d      )z
        Output the feed in the given encoding to outfile, which is a file-like
        object. Subclasses should override this.
        z;subclasses of SyndicationFeed must provide a write() method)NotImplementedError)r@   outfileencodings      r   writezSyndicationFeed.write   s    
 "I
 	
r   c                 Z    t               }| j                  ||       |j                         S )zD
        Return the feed in the given encoding as a string.
        )r   rd   getvalue)r@   rc   r-   s      r   writeStringzSyndicationFeed.writeString   s%     J

1hzz|r   c                     d}d}| j                   D ]'  }|D ]   }|j                  |      }|s|||kD  s|}" ) |xs8 t        j                  j                  t        j                  j
                        S )z
        Return the latest item's pubdate or updateddate. If no items
        have either of these attributes this return the current UTC date/time.
        N)rH   rG   )tz)r?   getr
   nowtimezoneutc)r@   latest_date	date_keysr[   date_key	item_dates         r   latest_post_datez SyndicationFeed.latest_post_date   s    
 .	JJ 	0D% 0 HHX.	"*i+.E&/	0	0 Mh//33x7H7H7L7L3MMr   )
NNNNNNNNNN)NNNNNNNr0   NNNN)__name__
__module____qualname____doc__rD   rO   rS   rU   rY   r\   r_   rd   rg   rr   r0   r   r   r'   r'   <   s    M %X #!0
d
Nr   r'   c                       e Zd ZdZd Zy)	EnclosurezAn RSS enclosurec                 B    ||c| _         | _        t        |      | _        y)z#All args are expected to be stringsN)length	mime_typer   r"   )r@   r"   rz   r{   s       r   rD   zEnclosure.__init__   s    &,i#T^c?r   N)rs   rt   ru   rv   rD   r0   r   r   rx   rx      s
    #r   rx   c                   .    e Zd ZdZd Zd Zd Zd Zd Zy)RssFeedz"application/rss+xml; charset=utf-8c                 H   t        ||d      }|j                          |j                  d| j                                |j                  d| j	                                | j                  |       | j                  |       | j                  |       |j                  d       y )NTshort_empty_elementsrsschannel)	r   startDocumentstartElementrss_attributesrU   rY   write_itemsendChannelElement
endElementr@   rb   rc   rX   s       r   rd   zRssFeed.write   s    %gxdSUD$7$7$9:Y(<(<(>?w'!w'5!r   c                      | j                   ddS )Nhttp://www.w3.org/2005/Atom)versionz
xmlns:atom)_versionrR   s    r   r   zRssFeed.rss_attributes   s    }}7
 	
r   c                     | j                   D ]F  }|j                  d| j                  |             | j                  ||       |j	                  d       H y )Nr[   r?   r   r\   r_   r   r^   s      r   r   zRssFeed.write_items   sN    JJ 	'D  )=)=d)CD""7D1v&	'r   c                    |j                  d| j                  d          |j                  d| j                  d          |j                  d| j                  d          | j                  d   #|j                  dd d| j                  d   d       | j                  d   |j                  d| j                  d          | j                  d	   D ]  }|j                  d
|        | j                  d   |j                  d| j                  d          |j                  dt        | j                                      | j                  d    |j                  d| j                  d          y y )Nr1   r2   r3   r:   z	atom:linkr@   relhrefr4   r9   categoryr;   	copyrightlastBuildDater=   )addQuickElementr>   r   rr   r@   rX   cats      r   rY   zRssFeed.add_root_elements   sF   7);<		&(9:tyy/GH99Z ,##T6499Z;P#Q 99Z ,##J		*0EF99\* 	5C##J4	599%&2##K;K1LMd>S>S>U1VW99U'##E499U+;< (r   c                 &    |j                  d       y )Nr   )r   rW   s     r   r   zRssFeed.endChannelElement  s    9%r   N)	rs   rt   ru   content_typerd   r   r   rY   r   r0   r   r   r}   r}      s     7L"
'=$&r   r}   c                       e Zd ZdZd Zy)RssUserland091Feedz0.91c                     |j                  d|d          |j                  d|d          |d   |j                  d|d          y y )Nr1   r2   r3   )r   r^   s      r   r_   z$RssUserland091Feed.add_item_elements  sO    g7V5*##M43FG +r   Nrs   rt   ru   r   r_   r0   r   r   r   r     s    HHr   r   c                       e Zd ZdZd Zy)Rss201rev2Feedz2.0c                    |j                  d|d          |j                  d|d          |d   |j                  d|d          |d   r$|d   r|j                  d|d   d|d   d       n8|d   r|j                  d|d          n|d   r|j                  d	|d   d
di       |d   |j                  dt        |d                |d   |j                  d|d          |d   Vi }t        |j                  d      t              rt        |d         j                         |d<   |j                  d|d   |       |d   |j                  d|d          |d   rat        |d         }t        |      dkD  rt        d      |d   }|j                  dd|j                  |j                  |j                  d       |d   D ]  }|j                  d|        y )Nr1   r2   r3   r6   r5   authorz ()z
dc:creatorzxmlns:dcz http://purl.org/dc/elements/1.1/rG   pubDaterI   rJ   rK   isPermaLinkguidr=   rL      zrRSS feed items may only have one enclosure, see http://www.rssboard.org/rss-profile#element-channel-item-enclosurer   	enclosurer   )r"   rz   typer9   r   )r   r   r	   rj   boolr+   lowerlistrQ   
ValueErrorr"   rz   r{   )r@   rX   r[   
guid_attrsrL   r   r   s          r   r_   z Rss201rev2Feed.add_item_elements  s   g7V5*##M43FG 4#7##tN';T-=PQ .!##Hd>.BC- ##]#?@ 	?&##I|DO/LM
'##JZ0@A(J$((#;<dC,/5M0N,O,U,U,W
=)##FD,=zJ;"##E4;7 d<01J:" Y  #1I##$=='..%// % 	5C##J4	5r   Nr   r0   r   r   r   r     s    H55r   r   c                   2    e Zd ZdZdZd Zd Zd Zd Zd Z	y)		Atom1Feedz#application/atom+xml; charset=utf-8r   c                     t        ||d      }|j                          |j                  d| j                                | j	                  |       | j                  |       |j                  d       y )NTr   r>   )r   r   r   rU   rY   r   r   r   s       r   rd   zAtom1Feed.write[  s_    %gxdSVT%9%9%;<w'!6"r   c                 t    | j                   d   | j                  | j                   d   dS d| j                  iS )Nr4   )xmlnszxml:langr   )r>   nsrR   s    r   rU   zAtom1Feed.root_attributesc  s8    99Z ,!WW$))J2GHHTWW%%r   c                    |j                  d| j                  d          |j                  ddd| j                  d   d       | j                  d   #|j                  ddd| j                  d   d       |j                  d| j                  d          |j                  d	t        | j                                      | j                  d
   |j	                  di        |j                  d| j                  d
          | j                  d   |j                  d| j                  d          | j                  d   |j                  d| j                  d          |j                  d       | j                  d   |j                  d| j                  d          | j                  d   D ]  }|j                  ddd|i        | j                  d    |j                  d| j                  d          y y )Nr1   r2   r   	alternater   r:   r@   r<   updatedr6   r   namer5   r   r7   urir8   r9   r   termr;   rights)r   r>   r   rr   r   r   r   s      r   rY   zAtom1Feed.add_root_elementsi  s   7);<BTYYv5FG	
 99Z ,##FDIIj4IJ 	diio6	<8M8M8O+PQ99]#/  2.##FDIIm,DEyy(4''>1JKyy'3''tyy/GHx(99Z ,##J		*0EF99\* 	CC##JVSMB	C99%&2##Hdii8H.IJ 3r   c                     | j                   D ]F  }|j                  d| j                  |             | j                  ||       |j	                  d       H y )Nentryr   r^   s      r   r   zAtom1Feed.write_items  sN    JJ 	(D  $*>*>t*DE""7D1w'	(r   c           
      Z   |j                  d|d          |j                  dd|d   dd       |d   |j                  dt        |d                |d   |j                  d	t        |d                |d
   l|j                  di        |j                  d|d
          |d   |j                  d|d          |d   |j                  d|d          |j                  d       |d   |d   }nt	        |d   |d         }|j                  d|       |d   |j                  d|d   ddi       |d   D ]8  }|j                  ddd|j
                  |j                  |j                  d       : |d   D ]  }|j                  ddd|i        |d   |j                  d|d          y y )Nr1   r2   r   r   )r   r   rG   	publishedrH   r   r6   r   r   r5   r   r7   r   rJ   r<   r3   summaryr   htmlrL   r   )r   r   rz   r   r9   r   r   rM   r   )r   r   r   r   r%   r"   rz   r{   )r@   rX   r[   rJ   r   r   s         r   r_   zAtom1Feed.add_item_elements  s   g7T&\+,VW	?&##Kd9o1NO*##I|D<O/PQ *  2.##FD,?@N#/''n1EFM".''tM/BCx( ([)I#DL$y/BIi0 *##ItM/BVVDTU l+ 
	I##&%MM'..%//		
	 % 	CC##JVSMB	C  !-##Hd3C.DE .r   N)
rs   rt   ru   r   r   rd   rU   rY   r   r_   r0   r   r   r   r   V  s'    8L	&B#&K4(2Fr   r   )rv   r
   r   ior   urllib.parser   django.utils.encodingr   django.utils.xmlutilsr   r   r   r%   r'   rx   r}   r   r   r   DefaultFeedr0   r   r   <module>r      s   0    ! , 5-HKYN YNx# #,&o ,&^H H95W 95xeF eFT r   