
    h;                         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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 Weblog by the sharpest minds in online media/journalism/publishing.",
...     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)utc)SimplerXMLGeneratorc                     t        | t        j                        s2t        j                  j                  | t        j                               } t        j
                  j                  |       S N)
isinstancedatetimecombinetimeemailutilsformat_datetimedates    ]/var/www/html/ranktracker/api/venv/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_str@       ]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)selfr2   r3   r4   r5   r6   r7   r8   r9   r:   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^   r0   r   r1   )r2   r3   r4   r6   r7   r8   pubdateupdateddatecomments	unique_idunique_id_is_permalink
enclosuresr:   item_copyrightr>   N)r@   appendr   )rA   r2   r3   r4   r6   r7   r8   rH   rJ   rK   rL   r:   rN   r>   rI   rM   rC   r/   rD   s                      r   add_itemzSyndicationFeed.add_itemU   s    	2C
$C1VAY$C


E]t$!+."<0!+.%k2&x(	*&<$*$*$^4#;
  !
 	 %Ds   Bc                 ,    t        | j                        S r	   )lenr@   rA   s    r   	num_itemszSyndicationFeed.num_itemst   s    4::r   c                     i S )zx
        Return extra attributes to place on the root (i.e. feed/channel) element.
        Called from write().
        r1   rS   s    r   root_attributeszSyndicationFeed.root_attributesw   s	    
 	r   c                      y)zd
        Add elements in the root (i.e. feed/channel) element. Called
        from write().
        Nr1   rA   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.
        r1   )rA   items     r   item_attributeszSyndicationFeed.item_attributes   s	     	r   c                      y)zF
        Add elements on each item (i.e. item/entry) element.
        Nr1   rA   rY   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)rA   outfileencodings      r   writezSyndicationFeed.write   s    
 ""_``r   c                 Z    t               }| j                  ||       |j                         S )zD
        Return the feed in the given encoding as a string.
        )r   re   getvalue)rA   rd   r.   s      r   writeStringzSyndicationFeed.writeString   s%     J

1hzz|r   c                     d}d}| j                   D ]'  }|D ]   }|j                  |      }|s|||kD  s|}" ) |xs2 t        j                  j                         j	                  t
              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)rI   rH   )tzinfo)r@   getr   utcnowreplacer   )rA   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 Lh//668@@@LLr   )
NNNNNNNNNN)NNNNNNNr1   NNNN)__name__
__module____qualname____doc__rE   rP   rT   rV   rZ   r]   r`   re   rh   rr   r1   r   r   r(   r(   ;   sQ    MMQOSIM0 ?CLPIKMQ>aM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#   )rA   r#   rz   r{   s       r   rE   zEnclosure.__init__   s    &,i#T^c?r   N)rs   rt   ru   rv   rE   r1   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                 D   t        ||      }|j                          |j                  d| j                                |j                  d| j	                                | j                  |       | j                  |       | j                  |       |j                  d       y )Nrsschannel)	r   startDocumentstartElementrss_attributesrV   rZ   write_itemsendChannelElement
endElementrA   rc   rd   rY   s       r   re   zRssFeed.write   s    %gx8UD$7$7$9:Y(<(<(>?w'!w'5!r   c                      | j                   ddS )Nhttp://www.w3.org/2005/Atom)versionz
xmlns:atom)_versionrS   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 )Nr2   r3   r4   r;   z	atom:linkrA   relhrefr5   r:   categoryr<   	copyrightlastBuildDater>   )addQuickElementr?   r   rr   rA   rY   cats      r   rZ   zRssFeed.add_root_elements   sB   7);<		&(9:tyy/GH99Z ,##KvtyyYcOd7ef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   rX   s     r   r   zRssFeed.endChannelElement   s    9%r   N)	rs   rt   ru   content_typere   r   r   rZ   r   r1   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 )Nr2   r3   r4   )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`   r1   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 )Nr2   r3   r4   r7   r6   authorz ()z
dc:creatorzxmlns:dcz http://purl.org/dc/elements/1.1/rH   pubDaterJ   rK   rL   isPermaLinkguidr>   rM      zrRSS feed items may only have one enclosure, see http://www.rssboard.org/rss-profile#element-channel-item-enclosurer   	enclosurer   )r#   rz   typer:   r   )r   r   r
   rk   boolr,   lowerlistrR   
ValueErrorr#   rz   r{   )rA   rY   r\   
guid_attrsrM   r   r   s          r   r`   z Rss201rev2Feed.add_item_elements   s   g7V5*##M43FG 4#7##H4;OQUVcQd.ef.!##Hd>.BC- ##d=1J@b3c 	?&##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##K }}#**!++6  % 	5C##J4	5r   Nr   r1   r   r   r   r      s    H-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        ||      }|j                          |j                  d| j                                | j	                  |       | j                  |       |j                  d       y )Nr?   )r   r   r   rV   rZ   r   r   r   s       r   re   zAtom1Feed.write+  s]    %gx8VT%9%9%;<w'!6"r   c                 t    | j                   d   | j                  | j                   d   dS d| j                  iS )Nr5   )xmlnszxml:langr   )r?   nsrS   s    r   rV   zAtom1Feed.root_attributes3  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 )Nr2   r3   r   	alternater   r;   rA   r=   updatedr7   r   namer6   r   r8   urir9   r:   r   termr<   rights)r   r?   r   rr   r   r   r   s      r   rZ   zAtom1Feed.add_root_elements9  s   7);<KSYIZ,[\99Z ,##FB		R\H]0^_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_itemsO  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 )Nr2   r3   r   r   )r   r   rH   	publishedrI   r   r7   r   r   r6   r   r8   r   rK   r=   r4   summaryr   htmlrM   r   )r   r   rz   r   r:   r   r   rN   r   )r   r   r   r   r&   r#   rz   r{   )rA   rY   r\   rK   r   r   s         r   r`   zAtom1Feed.add_item_elementsU  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##FB"!#**!++	1 	 % 	CC##JVSMB	C  !-##Hd3C.DE .r   N)
rs   rt   ru   r   r   re   rV   rZ   r   r`   r1   r   r   r   r   &  s'    8L	&B#&K,(.Fr   r   )rv   r   r   ior   urllib.parser   django.utils.encodingr   django.utils.timezoner   django.utils.xmlutilsr   r   r   r&   r(   rx   r}   r   r   r   DefaultFeedr1   r   r   <module>r      s   .    ! , % 5-H
KuM uMp# #*&o *&ZH H15W 15h]F ]FD r   