3
@6^Q                 @   sX   d dl Z d dlZd dlZd dlZd dlZd dlZd dlmZm	Z	m
Z
 G dd de	ZdS )    N)
SpiderFootSpiderFootPluginSpiderFootEventc               @   sJ   e Zd ZdZi Zi ZdZe fddZdd Z	dd Z
d	d
 Zdd ZdS )sfp_openstreetmapzOpenStreetMap:Footprint,Investigate,Passive:Real World::Retrieves latitude/longitude coordinates for physical addresses from OpenStreetMap API.Nc             C   s8   || _ | j | _x"t|j D ]}|| | j|< qW d S )N)sftempStorageresultslistkeysopts)selfsfcuserOptsopt r   </var/www/spiderfoot.crq.systems/modules/sfp_openstreetmap.pysetup   s    
zsfp_openstreetmap.setupc             C   s   dgS )NPHYSICAL_ADDRESSr   )r   r   r   r   watchedEvents'   s    zsfp_openstreetmap.watchedEventsc             C   s   dgS )NPHYSICAL_COORDINATESr   )r   r   r   r   producedEvents+   s    z sfp_openstreetmap.producedEventsc             C   s   |j djddddddd}| jjdtjj| | jd	 d
d}|d d krb| jjd|  d S yt	j
|d }W n4 tk
r } z| jjdt|  d S d }~X nX |S )Nraw_unicode_escapeasciireplace)errorsjson0)qformatZpolygonZaddressdetailsz+https://nominatim.openstreetmap.org/search?_fetchtimeoutr   )timeout	useragentcontentzNo location info found for z Error processing JSON response: )encodedecoder   fetchUrlurllibparse	urlencoder   infor   loads	Exceptiondebugstr)r   qryparamsresdataer   r   r   query0   s    zsfp_openstreetmap.queryc             C   s  |j }|j}|j}| jjd| d |  || jkrL| jjd| d  d S d| j|< |}|j jdrx| jjd d S tj	dtj
d	}tj|d
|}tj	dtj
d	}tj|d
|}| j|}tjd |d kr| jjd|  d S | jjdtt| d |  x|D ]}	y|	jd}
|	jd}W n< tk
rb } z| jjdt|  w
W Y d d }~X nX |
 s
| rxq
t|
d t| }| jjd|  td|| j|}| j| q
W d S )NzReceived event, z, from z	Skipping z as already mapped.Tzpo boxzSkipping PO BOX addressz.^(c/o|care of|attn:|attention:)\s+[0-9a-z\s\.])flags z&^(Level|Floor|Suite|Room)\s+[0-9a-z]+,   zFound no results for zFound z matches for latlonzFailed to get lat/lon: ,zFound coordinates: r   )	eventTypemoduler1   r   r,   r   lower
startswithrecompile
IGNORECASEsubr3   timesleepr)   r-   lengetBaseExceptionr   __name__notifyListeners)r   event	eventNamesrcModuleName	eventDataaddressZrx1Zrx2r1   locationr7   r8   r2   coordsevtr   r   r   handleEventH   sF    



 

zsfp_openstreetmap.handleEvent)rG   
__module____qualname____doc__r   optdescsr   dictr   r   r   r3   rQ   r   r   r   r   r      s   r   )r>   r   rB   urllib.requestr&   urllib.parseurllib.errorsflibr   r   r   r   r   r   r   r   <module>   s
   