
Â�Â­Â�Â­Â�Â­Â�Â­Â�Â­Â�Â­Â�Â­Â�Â­Â�Â­Â�Â­Â�Â­Â�Â­Â�Â­Â�Â­Â�Â­Â�Â­Â�Â­Â�Â­
<!DOCTYPE html>
<html>
3

  \€"  ã               @   s\   d Z ddlZddlZddlZdgZejddƒZG dd„ dƒZG dd„ dƒZ	G d	d
„ d
ƒZ
dS )a%   robotparser.py

    Copyright (C) 2000  Bastian Kleineidam

    You can choose between two licenses when using this package:
    1) GNU GPLv2
    2) PSF license for Python 2.2

    The robots.txt Exclusion Protocol is implemented as specified in
    http://www.robotstxt.org/norobots-rfc.txt
é    NÚRobotFileParserÚRequestRatezrequests secondsc               @   sj   e Zd Z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d„ ZdS )r   zs This class provides a set of methods to read, parse and answer
    questions about a single robots.txt file.

    Ú c             C   s,   g | _ d | _d| _d| _| j|ƒ d| _d S )NFr   )ÚentriesÚdefault_entryÚdisallow_allÚ	allow_allÚset_urlÚlast_checked)ÚselfÚurl© r   ú*/usr/lib64/python3.6/urllib/robotparser.pyÚ__init__   s    
zRobotFileParser.__init__c             C   s   | j S )z·Returns the time the robots.txt file was last fetched.

        This is useful for long-running web spiders that need to
        check for new robots.txt files periodically.

        )r
   )r   r   r   r   Úmtime$   s    zRobotFileParser.mtimec             C   s   ddl }|j ƒ | _dS )zYSets the time the robots.txt file was last fetched to the
        current time.

        r   N)Útimer
   )r   r   r   r   r   Úmodified-   s    zRobotFileParser.modifiedc             C   s&   || _ tjj|ƒdd… \| _| _dS )z,Sets the URL referring to a robots.txt file.é   é   N)r   ÚurllibÚparseÚurlparseÚhostÚpath)r   r   r   r   r   r	   5   s    zRobotFileParser.set_urlc             C   s†   yt jj| jƒ}W nR t jjk
rd } z2|jdkr:d| _n|jdkrT|jdk rTd| _W Y dd}~X nX |j	ƒ }| j
|jdƒjƒ ƒ dS )	z4Reads the robots.txt URL and feeds it to the parser.é‘  é“  Ti�  iô  Nzutf-8)r   r   )r   ZrequestZurlopenr   ÚerrorZ	HTTPErrorÚcoder   r   Úreadr   ÚdecodeÚ
splitlines)r   ÚfÚerrÚrawr   r   r   r   :   s    
zRobotFileParser.readc             C   s,   d|j kr| jd kr(|| _n| jj|ƒ d S )NÚ*)Ú
useragentsr   r   Úappend)r   Úentryr   r   r   Ú
_add_entryG   s    

zRobotFileParser._add_entryc             C   s6  d}t ƒ }| jƒ  �x|D �]þ}|sT|dkr8t ƒ }d}n|dkrT| j|ƒ t ƒ }d}|jdƒ}|dkrr|d|… }|jƒ }|s€q|jddƒ}t|ƒdkr|d jƒ jƒ |d< tj	j
|d jƒ ƒ|d< |d dk�r |dkrê| j|ƒ t ƒ }|jj|d ƒ d}q|d dk�r4|dk�r|jjt|d d	ƒƒ d}q|d d
k�rh|dk�r|jjt|d dƒƒ d}q|d dk�r¦|dk�r|d jƒ jƒ �r t|d ƒ|_d}q|d dkr|dkr|d jdƒ}t|ƒdk�r|d jƒ jƒ �r|d jƒ jƒ �rtt|d ƒt|d ƒƒ|_d}qW |dk�r2| j|ƒ dS )z”Parse the input lines from a robots.txt file.

        We allow that a user-agent: line is not preceded by
        one or more blank lines.
        r   r   é   ú#Nú:z
user-agentZdisallowFZallowTzcrawl-delayzrequest-rateú/)ÚEntryr   r(   ÚfindÚstripÚsplitÚlenÚlowerr   r   Úunquoter%   r&   Ú	rulelinesÚRuleLineÚisdigitÚintÚdelayr   Úreq_rate)r   ÚlinesÚstater'   ÚlineÚiZnumbersr   r   r   r   P   sd    






 
zRobotFileParser.parsec             C   s    | j r
dS | jrdS | jsdS tjjtjj|ƒƒ}tjjdd|j|j	|j
|jfƒ}tjj|ƒ}|sfd}x"| jD ]}|j|ƒrn|j|ƒS qnW | jrœ| jj|ƒS dS )z=using the parsed robots.txt decide if useragent can fetch urlFTr   r,   )r   r   r
   r   r   r   r3   Ú
urlunparser   ZparamsZqueryZfragmentÚquoter   Ú
applies_toÚ	allowancer   )r   Ú	useragentr   Z
parsed_urlr'   r   r   r   Ú	can_fetch“   s$    
zRobotFileParser.can_fetchc             C   s4   | j ƒ sd S x| jD ]}|j|ƒr|jS qW | jjS )N)r   r   r@   r8   r   )r   rB   r'   r   r   r   Úcrawl_delay°   s    

zRobotFileParser.crawl_delayc             C   s4   | j ƒ sd S x| jD ]}|j|ƒr|jS qW | jjS )N)r   r   r@   r9   r   )r   rB   r'   r   r   r   Úrequest_rate¸   s    

zRobotFileParser.request_ratec             C   s0   | j }| jd k	r|| jg }djtt|ƒƒd S )NÚ
)r   r   ÚjoinÚmapÚstr)r   r   r   r   r   Ú__str__À   s    
zRobotFileParser.__str__N)r   )Ú__name__Ú
__module__Ú__qualname__Ú__doc__r   r   r   r	   r   r(   r   rC   rD   rE   rJ   r   r   r   r   r      s   
		Cc               @   s(   e Zd ZdZdd„ Zdd„ Zdd„ ZdS )	r5   zoA rule line is a single "Allow:" (allowance==True) or "Disallow:"
       (allowance==False) followed by a path.c             C   s>   |dkr| rd}t jjt jj|ƒƒ}t jj|ƒ| _|| _d S )Nr   T)r   r   r>   r   r?   r   rA   )r   r   rA   r   r   r   r   Ê   s
    zRuleLine.__init__c             C   s   | j dkp|j| j ƒS )Nr$   )r   Ú
startswith)r   Úfilenamer   r   r   r@   Ò   s    zRuleLine.applies_toc             C   s   | j r
dndd | j S )NZAllowZDisallowz: )rA   r   )r   r   r   r   rJ   Õ   s    zRuleLine.__str__N)rK   rL   rM   rN   r   r@   rJ   r   r   r   r   r5   Ç   s   r5   c               @   s0   e Zd ZdZdd„ Zdd„ Zdd„ Zdd	„ Zd
S )r-   z?An entry has one or more user-agents and zero or more rulelinesc             C   s   g | _ g | _d | _d | _d S )N)r%   r4   r8   r9   )r   r   r   r   r   Û   s    zEntry.__init__c             C   s�   g }x| j D ]}|jd|› �ƒ qW | jd k	r@|jd| j› �ƒ | jd k	rj| j}|jd|j› d|j› �ƒ |jtt| j	ƒƒ |jdƒ dj
|ƒS )NzUser-agent: zCrawl-delay: zRequest-rate: r,   r   rF   )r%   r&   r8   r9   ZrequestsZsecondsÚextendrH   rI   r4   rG   )r   ZretÚagentZrater   r   r   rJ   á   s    


zEntry.__str__c             C   sF   |j dƒd jƒ }x.| jD ]$}|dkr*dS |jƒ }||krdS qW dS )z2check if this entry applies to the specified agentr,   r   r$   TF)r0   r2   r%   )r   rB   rR   r   r   r   r@   î   s    zEntry.applies_toc             C   s$   x| j D ]}|j|ƒr|jS qW dS )zZPreconditions:
        - our agent applies to this entry
        - filename is URL decodedT)r4   r@   rA   )r   rP   r<   r   r   r   rA   û   s    

zEntry.allowanceN)rK   rL   rM   rN   r   rJ   r@   rA   r   r   r   r   r-   Ù   s
   r-   )rN   ÚcollectionsZurllib.parser   Zurllib.requestÚ__all__Ú
namedtupler   r   r5   r-   r   r   r   r   Ú<module>   s    2