U
    e=                     @   s   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 Zd	d
 Z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   >/tmp/pip-unpacked-wheel-lctamlir/django/utils/feedgenerator.pyrfc2822_date!   s    r   c                 C   s:   t | tjstj| t } |  |  d kr4dnd S )NZ )r   r   r	   r
   	isoformat	utcoffsetr   r   r   r   rfc3339_date'   s    r   c                 C   s8   t | }d}|dk	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                   @   sd   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S )SyndicationFeedzGBase class for all syndication feeds. Subclasses should provide write()Nc                 K   s~   dd }|	odd |	D }	||t |||||||||t ||||	pPdt |
|||pb|||d|| _g | _d S )Nc                 S   s   | d k	rt | S | S r   strsr   r   r   to_strO   s    z(SyndicationFeed.__init__.<locals>.to_strc                 S   s   g | ]}t |qS r   r!   .0cr   r   r   
<listcomp>R   s     z,SyndicationFeed.__init__.<locals>.<listcomp>r   )titlelinkdescriptionlanguageauthor_emailauthor_nameauthor_linksubtitle
categoriesfeed_urlfeed_copyrightidttl)r   feeditems)selfr*   r+   r,   r-   r.   r/   r0   r1   r2   r3   r4   Z	feed_guidr6   kwargsr%   r   r   r   __init__>   s&    zSyndicationFeed.__init__r   c                    s   dd  |o fdd|D }| j  |t| | | |t||| | |	|
|p`d|pfd | |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   s   | d k	rt | S | S r   r!   r#   r   r   r   r%   ~   s    z(SyndicationFeed.add_item.<locals>.to_strc                    s   g | ]} |qS r   r   r&   r%   r   r   r)      s     z,SyndicationFeed.add_item.<locals>.<listcomp>r   )r*   r+   r,   r.   r/   r0   pubdateupdateddatecomments	unique_idunique_id_is_permalink
enclosuresr2   item_copyrightr6   N)r8   appendr   )r9   r*   r+   r,   r.   r/   r0   r=   r?   r@   rA   r2   rC   r6   r>   rB   r:   r   r<   r   add_iteme   s,    zSyndicationFeed.add_itemc                 C   s
   t | jS r   )lenr8   r9   r   r   r   	num_items   s    zSyndicationFeed.num_itemsc                 C   s   i S )zx
        Return extra attributes to place on the root (i.e. feed/channel) element.
        Called from write().
        r   rG   r   r   r   root_attributes   s    zSyndicationFeed.root_attributesc                 C   s   dS )zd
        Add elements in the root (i.e. feed/channel) element. Called
        from write().
        Nr   r9   handlerr   r   r   add_root_elements   s    z!SyndicationFeed.add_root_elementsc                 C   s   i S )zZ
        Return extra attributes to place on each item (i.e. item/entry) element.
        r   )r9   itemr   r   r   item_attributes   s    zSyndicationFeed.item_attributesc                 C   s   dS )zF
        Add elements on each item (i.e. item/entry) element.
        Nr   r9   rK   rM   r   r   r   add_item_elements   s    z!SyndicationFeed.add_item_elementsc                 C   s   t ddS )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() methodN)NotImplementedError)r9   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   rT   getvalue)r9   rS   r$   r   r   r   writeString   s    zSyndicationFeed.writeStringc                 C   sV   d}d}| j D ]0}|D ]&}||}|r|dks8||kr|}qq|pTt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)r>   r=   )tz)r8   getr   nowtimezoneutc)r9   Zlatest_dateZ	date_keysrM   Zdate_keyZ	item_dater   r   r   latest_post_date   s    

z SyndicationFeed.latest_post_date)
NNNNNNNNNN)NNNNNNNr   NNNN)__name__
__module____qualname____doc__r;   rE   rH   rI   rL   rN   rP   rT   rV   r\   r   r   r   r   r    ;   sB             
,            
2	r    c                   @   s   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   )r9   r   rb   rc   r   r   r   r;      s    zEnclosure.__init__N)r]   r^   r_   r`   r;   r   r   r   r   ra      s   ra   c                   @   s8   e Zd Z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   sb   t ||dd}|  |d|   |d|   | | | | | | |d d S )NTshort_empty_elementsZrsschannel)	r   startDocumentstartElementrss_attributesrI   rL   write_itemsendChannelElement
endElementr9   rR   rS   rK   r   r   r   rT      s    


zRssFeed.writec                 C   s   | j ddS )Nhttp://www.w3.org/2005/Atom)versionz
xmlns:atom)_versionrG   r   r   r   rj      s    zRssFeed.rss_attributesc                 C   s8   | j D ],}|d| | | || |d qd S )NrM   r8   ri   rN   rP   rm   rO   r   r   r   rk      s    
zRssFeed.write_itemsc                 C   s   | d| jd  | d| jd  | d| jd  | jd d k	r^| dd d| jd d | jd d k	r~| d| jd  | jd	 D ]}| d
| q| jd d k	r| d| jd  | dt|   | jd d k	r| d| jd  d S )Nr*   r+   r,   r3   z	atom:linkr9   relhrefr-   r2   categoryr4   	copyrightZlastBuildDater6   )addQuickElementr7   r   r\   r9   rK   catr   r   r   rL      s$      zRssFeed.add_root_elementsc                 C   s   | d d S )Nrg   )rm   rJ   r   r   r   rl     s    zRssFeed.endChannelElementN)	r]   r^   r_   content_typerT   rj   rk   rL   rl   r   r   r   r   rd      s   
rd   c                   @   s   e Zd ZdZdd ZdS )RssUserland091Feedz0.91c                 C   s@   | d|d  | d|d  |d d k	r<| d|d  d S )Nr*   r+   r,   )rx   rO   r   r   r   rP     s    z$RssUserland091Feed.add_item_elementsNr]   r^   r_   rq   rP   r   r   r   r   r|     s   r|   c                   @   s   e Zd ZdZdd ZdS )Rss201rev2Feedz2.0c                 C   s  | d|d  | d|d  |d d k	r<| d|d  |d rj|d rj| dd|d |d f  n8|d r| d|d  n|d r| d|d d	d
i |d d k	r| dt|d  |d d k	r| d|d  |d d k	r(i }t|dtrt|d  |d< | d|d | |d d k	rF| d|d  |d rt|d }t|dkrrt	d|d }| dd|j
|j|jd |d D ]}| d| qd S )Nr*   r+   r,   r/   r.   authorz%s (%s)z
dc:creatorzxmlns:dcz http://purl.org/dc/elements/1.1/r=   ZpubDater?   r@   rA   ZisPermaLinkZguidr6   rB      zrRSS feed items may only have one enclosure, see http://www.rssboard.org/rss-profile#element-channel-item-enclosurer   	enclosurer   )r   rb   typer2   rv   )rx   r   r   rX   boolr"   lowerlistrF   
ValueErrorr   rb   rc   )r9   rK   rM   Z
guid_attrsrB   r   rz   r   r   r   rP     sZ     
z Rss201rev2Feed.add_item_elementsNr}   r   r   r   r   r~     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-8ro   c                 C   sH   t ||dd}|  |d|   | | | | |d d S )NTre   r7   )r   rh   ri   rI   rL   rk   rm   rn   r   r   r   rT   Z  s    

zAtom1Feed.writec                 C   s.   | j d d k	r | j| j d dS d| jiS d S )Nr-   )xmlnszxml:langr   )r7   nsrG   r   r   r   rI   b  s    zAtom1Feed.root_attributesc                 C   s\  | d| jd  | ddd| jd d | jd d k	rT| ddd| jd d | d| jd  | d	t|   | jd
 d k	r|di  | d| jd
  | jd d k	r| d| jd  | jd d k	r| d| jd  |d | jd d k	r| d| jd  | jd D ]}| ddd|i q| jd d k	rX| d| jd  d S )Nr*   r+   r   	alternaters   r3   r9   r5   updatedr/   r   namer.   r   r0   urir1   r2   rv   termr4   rights)rx   r7   r   r\   ri   rm   ry   r   r   r   rL   h  s8        
zAtom1Feed.add_root_elementsc                 C   s8   | j D ],}|d| | | || |d qd S )Nentryrr   rO   r   r   r   rk     s    
zAtom1Feed.write_itemsc              
   C   s  | d|d  | dd|d dd |d d k	rH| dt|d  |d d k	rh| d	t|d  |d
 d k	r|di  | d|d
  |d d k	r| d|d  |d d k	r| d|d  |d |d d k	r|d }nt|d |d }| d| |d d k	r*| d|d ddi |d D ]$}| ddd|j|j|jd q2|d D ]}| ddd|i q`|d d k	r| d|d  d S )Nr*   r+   r   r   )ru   rt   r=   Z	publishedr>   r   r/   r   r   r.   r   r0   r   r@   r5   r,   summaryr   htmlrB   r   )rt   ru   rb   r   r2   rv   r   rC   r   )rx   r   ri   rm   r   r   rb   rc   )r9   rK   rM   r@   r   rz   r   r   r   rP     sD    

zAtom1Feed.add_item_elementsN)
r]   r^   r_   r{   r   rT   rI   rL   rk   rP   r   r   r   r   r   U  s   r   )r`   r   r   ior   urllib.parser   Zdjango.utils.encodingr   Zdjango.utils.xmlutilsr   r   r   r   r    ra   rd   r|   r~   r   ZDefaultFeedr   r   r   r   <module>   s"    	/
<j