o
    þÞh©F  ã                   @   sÞ   d Z ddl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„ Zd	d
„ Zdd„ Zdd„ ZG dd„ dƒZG dd„ dƒZG dd„ dƒZG dd„ deƒZG dd„ deƒZG dd„ deƒZG dd„ deƒZeZdS )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                 C   s*   t | tjƒstj | t ¡ ¡} tj | ¡S ©N)Ú
isinstanceÚdatetimeÚcombineÚtimeÚemailÚutilsÚformat_datetime©Údate© r   úZ/var/www/html/premium_crap/venv/lib/python3.10/site-packages/django/utils/feedgenerator.pyÚrfc2822_date#   s   r   c                 C   s<   t | tjƒstj | t ¡ ¡} |  ¡ |  ¡ d u rd S d S )NÚZÚ )r   r   r	   r
   Ú	isoformatÚ	utcoffsetr   r   r   r   Úrfc3339_date)   s   r   c                 C   s8   t | ƒ}d}|durd| d¡ }d|j||j|jf S )zŒ
    Create a TagURI.

    See
    https://web.archive.org/web/20110514113830/http://diveintomark.org/archives/2004/05/28/howto-atom-id
    r   Nz,%sz%Y-%m-%dztag:%s%s:%s/%s)r   ÚstrftimeÚhostnameÚpathÚfragment)Úurlr   ÚbitsÚdr   r   r   Úget_tag_uri/   s
   r   c                 C   s    t  ¡ }| tdƒ¡ | | ¡S )z
    Return the given stylesheet's mimetype tuple, using a slightly custom
    version of Python's mimetypes.guess_type().
    ztext/xsl	xsl
text/xsl	xslt)Ú	mimetypesÚ	MimeTypesÚreadfpr   Ú
guess_type)r   Ú
mimetypedbr   r   r   Ú_guess_stylesheet_mimetype=   s   
r%   c                   @   sB   e Zd ZdZddd„Zedd„ ƒZedd	„ ƒZd
d„ Zdd„ Z	dS )Ú
StylesheetzAn RSS stylesheetr   Úscreenc                 C   s   || _ || _|| _d S r   )Ú_urlÚ	_mimetypeÚmedia)Úselfr   Úmimetyper*   r   r   r   Ú__init__O   s   
zStylesheet.__init__c                 C   ó
   t | jƒS r   )r   r(   ©r+   r   r   r   r   V   s   
zStylesheet.urlc                 C   s   | j dkrt| jƒd S | j S )Nr   r   )r)   r%   r   r/   r   r   r   r,   Z   s   
zStylesheet.mimetypec                 C   sV   d| j › d�g}| jd ur| d| j› d�¡ | jd ur&| d| j› d�¡ d |¡S )Nzhref="ú"ztype="zmedia="ú )r   r,   Úappendr*   Újoin)r+   Údatar   r   r   Ú__str__`   s   


zStylesheet.__str__c                 C   s   t | j| j| jfƒS r   )Úreprr   r,   r*   r/   r   r   r   Ú__repr__h   ó   zStylesheet.__repr__N)r   r'   )
Ú__name__Ú
__module__Ú__qualname__Ú__doc__r-   Úpropertyr   r,   r5   r7   r   r   r   r   r&   L   s    


r&   c                   @   sš   e Zd ZdZ											ddd„Z												ddd„Zdd	„ Zd
d„ Zdd„ Zdd„ Z	dd„ Z
dd„ Zdd„ Zdd„ Zdd„ ZdS )ÚSyndicationFeedzGBase class for all syndication feeds. Subclasses should provide write()Nc                    sÀ   dd„ }dd„ ‰ |	odd„ |	D ƒ}	|d ur-t |ttfƒr$td|j› �ƒ‚‡ fdd„|D ƒ}||ƒt|ƒ||ƒ||ƒ||ƒ||ƒt|ƒ||ƒ|	pHd	t|
ƒ||ƒ|pQ|||ƒ|d
œ|¥| _g | _d S )Nc                 S   ó   | d urt | ƒS | S r   ©Ústr©Úsr   r   r   Úto_str�   r8   z(SyndicationFeed.__init__.<locals>.to_strc                 S   s   t | tƒr| S t| ƒS r   )r   r&   rB   r   r   r   Úto_stylesheet„   s   z/SyndicationFeed.__init__.<locals>.to_stylesheetc                 S   s   g | ]}t |ƒ‘qS r   r@   ©Ú.0Úcr   r   r   Ú
<listcomp>‡   ó    z,SyndicationFeed.__init__.<locals>.<listcomp>z"stylesheets should be a list, not c                    ó   g | ]}ˆ |ƒ‘qS r   r   )rG   rC   ©rE   r   r   rI   Ž   rJ   r   )ÚtitleÚlinkÚdescriptionÚlanguageÚauthor_emailÚauthor_nameÚauthor_linkÚsubtitleÚ
categoriesÚfeed_urlÚfeed_copyrightÚidÚttlÚstylesheets)r   r&   rA   Ú	TypeErrorÚ	__class__r   ÚfeedÚitems)r+   rM   rN   rO   rP   rQ   rR   rS   rT   rU   rV   rW   Ú	feed_guidrY   rZ   ÚkwargsrD   r   rL   r   r-   o   s6   
ÿòñ
zSyndicationFeed.__init__r   c                    s‚   dd„ ‰ |o‡ fdd„|D ƒ}| j  ˆ |ƒt|ƒˆ |ƒˆ |ƒˆ |ƒt|ƒ||ˆ |ƒˆ |	ƒ|
|p0d|p3dˆ |ƒˆ |ƒdœ|¥¡ dS )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                 S   r?   r   r@   rB   r   r   r   rD   ¼   r8   z(SyndicationFeed.add_item.<locals>.to_strc                    rK   r   r   rF   ©rD   r   r   rI   ¿   rJ   z,SyndicationFeed.add_item.<locals>.<listcomp>r   )rM   rN   rO   rQ   rR   rS   ÚpubdateÚupdateddateÚcommentsÚ	unique_idÚunique_id_is_permalinkÚ
enclosuresrU   Úitem_copyrightrY   N)r^   r2   r   )r+   rM   rN   rO   rQ   rR   rS   rb   rd   re   rf   rU   rh   rY   rc   rg   r`   r   ra   r   Úadd_item£   s,   ñðÿzSyndicationFeed.add_itemc                 C   r.   r   )Úlenr^   r/   r   r   r   Ú	num_itemsÕ   s   
zSyndicationFeed.num_itemsc                 C   ó   i S )zx
        Return extra attributes to place on the root (i.e. feed/channel) element.
        Called from write().
        r   r/   r   r   r   Úroot_attributesØ   ó   zSyndicationFeed.root_attributesc                 C   ó   dS )zd
        Add elements in the root (i.e. feed/channel) element. Called
        from write().
        Nr   ©r+   Úhandlerr   r   r   Úadd_root_elementsß   rn   z!SyndicationFeed.add_root_elementsc                 C   ro   )zE
        Add stylesheet(s) to the feed. Called from write().
        Nr   rp   r   r   r   Úadd_stylesheetsæ   ó   zSyndicationFeed.add_stylesheetsc                 C   rl   )zZ
        Return extra attributes to place on each item (i.e. item/entry) element.
        r   )r+   Úitemr   r   r   Úitem_attributesì   rt   zSyndicationFeed.item_attributesc                 C   ro   )zF
        Add elements on each item (i.e. item/entry) element.
        Nr   ©r+   rq   ru   r   r   r   Úadd_item_elementsò   rt   z!SyndicationFeed.add_item_elementsc                 C   s   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Úencodingr   r   r   Úwriteø   s   ÿzSyndicationFeed.writec                 C   s   t ƒ }|  ||¡ | ¡ S )zD
        Return the feed in the given encoding as a string.
        )r   r|   Úgetvalue)r+   r{   rC   r   r   r   ÚwriteString  s   zSyndicationFeed.writeStringc                 C   sV   d}d}| j D ]}|D ]}| |¡}|r|du s||kr|}qq|p*tjjtjjd�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)rc   rb   )Útz)r^   Úgetr   ÚnowÚtimezoneÚutc)r+   Úlatest_dateÚ	date_keysru   Údate_keyÚ	item_dater   r   r   Úlatest_post_date	  s   

€üz SyndicationFeed.latest_post_date)NNNNNNNNNNN)NNNNNNNr   NNNN)r9   r:   r;   r<   r-   ri   rk   rm   rr   rs   rv   rx   r|   r~   rˆ   r   r   r   r   r>   l   sH    
ñ9
ð2	r>   c                   @   ó   e Zd ZdZdd„ ZdS )Ú	EnclosurezAn RSS enclosurec                 C   s   ||| _ | _t|ƒ| _dS )z#All args are expected to be stringsN)ÚlengthÚ	mime_typer   r   )r+   r   r‹   rŒ   r   r   r   r-     s   zEnclosure.__init__N)r9   r:   r;   r<   r-   r   r   r   r   rŠ     ó    rŠ   c                   @   s@   e Zd ZdZdd„ Zdd„ Zdd„ Zdd	„ Zd
d„ Zdd„ Z	dS )ÚRssFeedz"application/rss+xml; charset=utf-8c                 C   sl   t ||dd�}| ¡  |  |¡ | d|  ¡ ¡ | d|  ¡ ¡ |  |¡ |  |¡ |  |¡ | 	d¡ d S )NT©Úshort_empty_elementsÚrssÚchannel)
r   ÚstartDocumentrs   ÚstartElementÚrss_attributesrm   rr   Úwrite_itemsÚendChannelElementÚ
endElement©r+   rz   r{   rq   r   r   r   r|   '  s   



zRssFeed.writec                 C   s   | j ddœS )Núhttp://www.w3.org/2005/Atom)Úversionz
xmlns:atom)Ú_versionr/   r   r   r   r•   4  s   þzRssFeed.rss_attributesc                 C   ó8   | j D ]}| d|  |¡¡ |  ||¡ | d¡ qd S )Nru   ©r^   r”   rv   rx   r˜   rw   r   r   r   r–   :  ó
   
ýzRssFeed.write_itemsc                 C   s$   | j d pg D ]}| d|¡ qd S )NrZ   zxml-stylesheet)r]   ÚprocessingInstruction)r+   rq   Ú
stylesheetr   r   r   rs   @  s   ÿzRssFeed.add_stylesheetsc                 C   sö   |  d| jd ¡ |  d| jd ¡ |  d| jd ¡ | jd d ur/|  dd d| jd dœ¡ | jd d ur?|  d| jd ¡ | jd	 D ]}|  d
|¡ qD| jd d ur]|  d| jd ¡ |  dt|  ¡ ƒ¡ | jd d ury|  d| jd ¡ d S d S )NrM   rN   rO   rV   z	atom:linkr+   ©ÚrelÚhrefrP   rU   ÚcategoryrW   Ú	copyrightÚlastBuildDaterY   )ÚaddQuickElementr]   r   rˆ   ©r+   rq   Úcatr   r   r   rr   D  s"   ÿÿzRssFeed.add_root_elementsc                 C   s   |  d¡ d S )Nr’   )r˜   rp   r   r   r   r—   V  s   zRssFeed.endChannelElementN)
r9   r:   r;   Úcontent_typer|   r•   r–   rs   rr   r—   r   r   r   r   rŽ   $  s    rŽ   c                   @   r‰   )ÚRssUserland091Feedz0.91c                 C   sD   |  d|d ¡ |  d|d ¡ |d d ur |  d|d ¡ d S d S )NrM   rN   rO   )r¨   rw   r   r   r   rx   ]  s
   ÿz$RssUserland091Feed.add_item_elementsN©r9   r:   r;   rœ   rx   r   r   r   r   r¬   Z  r�   r¬   c                   @   r‰   )ÚRss201rev2Feedz2.0c                 C   sª  |  d|d ¡ |  d|d ¡ |d d ur|  d|d ¡ |d r5|d r5|  dd|d |d f ¡ n|d rB|  d|d ¡ n|d rQ|  d|d d	d
i¡ |d d ura|  dt|d ƒ¡ |d d uro|  d|d ¡ |d d ur’i }t| d¡tƒr‰t|d ƒ ¡ |d< |  d|d |¡ |d d ur |  d|d ¡ |d rÆt|d ƒ}t|ƒdkr´t	dƒ‚|d }|  dd|j
|j|jdœ¡ |d D ]}|  d|¡ qÊd S )NrM   rN   rO   rR   rQ   Úauthorz%s (%s)z
dc:creatorzxmlns:dcz http://purl.org/dc/elements/1.1/rb   ÚpubDaterd   re   rf   ÚisPermaLinkÚguidrY   rg   é   zrRSS feed items may only have one enclosure, see http://www.rssboard.org/rss-profile#element-channel-item-enclosurer   Ú	enclosurer   )r   r‹   ÚtyperU   r¥   )r¨   r   r   r€   ÚboolrA   ÚlowerÚlistrj   Ú
ValueErrorr   r‹   rŒ   )r+   rq   ru   Ú
guid_attrsrg   r´   rª   r   r   r   rx   h  sZ   ÿýÿýýÿz Rss201rev2Feed.add_item_elementsNr­   r   r   r   r   r®   d  s    r®   c                   @   s<   e Zd ZdZdZdd„ Zdd„ Zdd„ Zd	d
„ Zdd„ Z	dS )Ú	Atom1Feedz#application/atom+xml; charset=utf-8rš   c                 C   sH   t ||dd�}| ¡  | d|  ¡ ¡ |  |¡ |  |¡ | d¡ d S )NTr�   r]   )r   r“   r”   rm   rr   r–   r˜   r™   r   r   r   r|   ¥  s   

zAtom1Feed.writec                 C   s*   | j d d ur| j| j d dœS d| jiS )NrP   )Úxmlnszxml:langr¼   )r]   Únsr/   r   r   r   rm   ­  s   
zAtom1Feed.root_attributesc                 C   sZ  |  d| jd ¡ |  ddd| jd dœ¡ | jd d ur*|  ddd| jd dœ¡ |  d| jd ¡ |  d	t|  ¡ ƒ¡ | jd
 d urx| di ¡ |  d| jd
 ¡ | jd d urc|  d| jd ¡ | jd d urs|  d| jd ¡ | d¡ | jd d urˆ|  d| jd ¡ | jd D ]}|  ddd|i¡ q�| jd d ur«|  d| jd ¡ d S d S )NrM   rN   r   Ú	alternater¢   rV   r+   rX   ÚupdatedrR   r¯   ÚnamerQ   r   rS   ÚurirT   rU   r¥   ÚtermrW   Úrights)r¨   r]   r   rˆ   r”   r˜   r©   r   r   r   rr   ³  s2   ÿÿ
ÿzAtom1Feed.add_root_elementsc                 C   r�   )NÚentryrž   rw   r   r   r   r–   Í  rŸ   zAtom1Feed.write_itemsc              
   C   s˜  |  d|d ¡ |  dd|d ddœ¡ |d d ur$|  dt|d ƒ¡ |d d ur4|  d	t|d ƒ¡ |d
 d uri| di ¡ |  d|d
 ¡ |d d urV|  d|d ¡ |d d urd|  d|d ¡ | d¡ |d d urt|d }n	t|d |d ƒ}|  d|¡ |d d ur”|  d|d ddi¡ |d D ]}|  ddd|j|j|jdœ¡ q˜|d D ]}|  ddd|i¡ q®|d d urÊ|  d|d ¡ d S d S )NrM   rN   r   r¾   )r¤   r£   rb   Ú	publishedrc   r¿   rR   r¯   rÀ   rQ   r   rS   rÁ   re   rX   rO   Úsummaryrµ   Úhtmlrg   r´   )r£   r¤   r‹   rµ   rU   r¥   rÂ   rh   rÃ   )r¨   r   r”   r˜   r   r   r‹   rŒ   )r+   rq   ru   re   r´   rª   r   r   r   rx   Ó  sF   

üýÿzAtom1Feed.add_item_elementsN)
r9   r:   r;   r«   r½   r|   rm   rr   r–   rx   r   r   r   r   r»      s    r»   )r<   r   r   r    Úior   Úurllib.parser   Údjango.utils.encodingr   Údjango.utils.xmlutilsr   r   r   r   r%   r&   r>   rŠ   rŽ   r¬   r®   r»   ÚDefaultFeedr   r   r   r   Ú<module>   s*      0	6
<j