o
    AfJ                     @   s   d dl Z d dlZd dlZd dlZedZde_dgg ddgdgg dd	Zd
gdgdZ	e
ee	B ZG dd deZdd ZdddZdd ZdS )    Nzlocation.importer   z3https://ftp.afrinic.net/pub/pub/dbase/afrinic.db.gz)z6https://ftp.apnic.net/apnic/whois/apnic.db.inet6num.gzz5https://ftp.apnic.net/apnic/whois/apnic.db.inetnum.gzz5https://ftp.apnic.net/apnic/whois/apnic.db.aut-num.gzz:https://ftp.apnic.net/apnic/whois/apnic.db.organisation.gzz'https://ftp.nic.ad.jp/jpirr/jpirr.db.gzz0https://ftp.lacnic.net/lacnic/dbase/lacnic.db.gz)z9https://ftp.ripe.net/ripe/dbase/split/ripe.db.inet6num.gzz8https://ftp.ripe.net/ripe/dbase/split/ripe.db.inetnum.gzz8https://ftp.ripe.net/ripe/dbase/split/ripe.db.aut-num.gzz=https://ftp.ripe.net/ripe/dbase/split/ripe.db.organisation.gz)ZAFRINICZAPNICZJPNICLACNICZRIPEzBhttps://ftp.arin.net/pub/stats/arin/delegated-arin-extended-latestzHhttps://ftp.lacnic.net/pub/stats/lacnic/delegated-lacnic-extended-latest)ZARINr   c                   @   s:   e Zd Zdd Zdd ZdddZddd	Zdd
dZdS )
Downloaderc                 C   s
   d | _ d S N)proxy)self r   6/usr/lib/python3.10/site-packages/location/importer.py__init__f   s   
zDownloader.__init__c                 C   s   t d|  || _dS )z=
			Sets a HTTP proxy that is used to perform all requests
		zUsing proxy %sN)loginfor   )r   urlr   r   r	   	set_proxyi   s   
zDownloader.set_proxyNc                 C   s   t jdd}tjj||d}| jr|| jd td|j	  tj
|}td |jD ]}td||j| f  q.|}	 |d	}|sIn|| qAW d
   n1 sYw   Y  |d d}	|jd}
|
dv rrd}	n	|ddkr{d}	|d |	rtd tj|dd}|S )z
			This method will fetch the content at the given URL
			and will return a file-object to a temporary file.

			If the content was compressed, it will be decompressed on the fly.
		i  @)Zmax_sizedataZhttpzRetrieving %s...zResponse Headers:z	%s: %sTi   Nr   FzContent-Type)zapplication/x-gzipzapplication/gzip   s   zGzip compression detectedrb)Zfileobjmode)tempfileZSpooledTemporaryFileurllibZrequestZRequestr   r   r   r   Zfull_urlZurlopendebugZheadersreadwriteseekgetgzipZGzipFile)r   r   r   tZreqresheaderfZbufZgzip_compressedZcontent_typer   r   r	   retrievep   s<   




	

zDownloader.retrievec                 C      | j ||d}t|S )zi
			This method will fetch the data from the URL and return an
			iterator for each block in the data.
		r   )r    iterate_over_blocksr   r   r   r   r   r   r	   request_blocks      zDownloader.request_blocksc                 C   r!   )zh
			This method will fetch the data from the URL and return an
			iterator for each line in the data.
		r   )r    iterate_over_linesr#   r   r   r	   request_lines   r%   zDownloader.request_linesr   )__name__
__module____qualname__r
   r   r    r$   r'   r   r   r   r	   r   e   s    

@r   c                 c   s`    t | D ](}d }i }t|D ]\}}|dd\}}|dkr!|}| ||< q||fV  qd S )N:r   r   )r"   	enumeratesplitstrip)r   blocktyper   ilinekeyvaluer   r   r	   read_blocks   s   r5   zutf-8latin1c              
   c   s    g }| D ]C}| ds| drq|D ]}z||}W n	 ty&   Y qw  |d\}}}| }|r9|s9q|rA|| q|rF|V  g }q|rP|V  d S d S )N   #   %#)
startswithdecodeUnicodeDecodeError	partitionrstripappend)r   Zcharsetsr/   r2   charsethashZcommentr   r   r	   r"      s2   

r"   c                 c   s"    | D ]}|  }| V  qd S r   )r<   r?   )r   r2   r   r   r	   r&     s
   r&   )r6   )r   Zloggingr   Zurllib.requestr   Z	getLoggerr   Z	propagateZWHOIS_SOURCESZEXTENDED_SOURCESsetZSOURCESobjectr   r5   r"   r&   r   r   r   r	   <module>   s2   
8b
,