
    i                         d Z ddlmZ ddlmZ ddlmZ ddlmZ dddd	d	d
dZdZ		 dgZ
dZd	ZdZdZ ed      Z	  ed      Z	  ed      Z	 d Zd ZdefdZy)aW  This engine implements *Tineye - reverse image search*

Using TinEye, you can search by image or perform what we call a reverse image
search.  You can do that by uploading an image or searching by URL. You can also
simply drag and drop your images to start your search.  TinEye constantly crawls
the web and adds images to its index.  Today, the TinEye index is over 50.2
billion images `[tineye.com] <https://tineye.com/how>`_.

.. hint::

   This SearXNG engine only supports *'searching by URL'* and it does not use
   the official API `[api.tineye.com] <https://api.tineye.com/python/docs/>`_.

    )	urlencode)datetime)gettext)EngineResultszhttps://tineye.comQ2382535z#https://api.tineye.com/python/docs/FJSON)websitewikidata_idofficial_api_documentationuse_official_apirequire_api_keyresultsonline_url_searchgeneralTz(/api/v1/result_json/?page={page}&{query}zCould not read that image url. This may be due to an unsupported file format. TinEye only supports images that are JPEG, PNG, GIF, BMP, TIFF or WebP.zyThe image is too simple to find matches. TinEye requires a basic level of visual detail to successfully identify matches.z"The image could not be downloaded.c                    d|d<   |d   d   r	|d   d   } n|d   d   r|d   d   } t         j                  d|        t        d| i      } t        t        j                  | |d   	      z   |d<   |d
   j                  ddddd       |S )zKBuild TinEye HTTP request using ``search_urls`` of a :py:obj:`engine_type`.Fraise_for_httperrorsearch_urlsz
data:imagehttpzquery URL: %surlpageno)querypageheadersz
keep-alivez
tineye.com1trailers)
ConnectionHostDNTTE)loggerdebugr   base_urlsearch_stringformatupdate)r   paramss     %/root/searxng/searx/engines/tineye.pyrequestr(   9   s     %*F !m\*}%l3		v	&}%f-
LL%(uen%E }33%fXFV3WWF5M
9& 		
 M    c                    g }d| v r| d   D ]  }t        |t              s|j                  d      }|rt        j                  |d      }nt        j
                  }|j                  |j                  d      |j                  d      ||j                  d      d        | j                  d      | j                  d	      | j                  d
      | j                  d      | j                  d      | j                  d      | j                  d      | j                  d      | j                  d      | j                  d      |dS )a  Takes parsed JSON from the API server and turns it into a :py:obj:`dict`
    object.

    Attributes `(class Match) <https://github.com/TinEye/pytineye/blob/main/pytineye/api.py>`__

    - `image_url`, link to the result image.
    - `domain`, domain this result was found on.
    - `score`, a number (0 to 100) that indicates how closely the images match.
    - `width`, image width in pixels.
    - `height`, image height in pixels.
    - `size`, image area in pixels.
    - `format`, image format.
    - `filesize`, image size in bytes.
    - `overlay`, overlay URL.
    - `tags`, whether this match belongs to a collection or stock domain.

    - `backlinks`, a list of Backlink objects pointing to the original websites
      and image URLs. List items are instances of :py:obj:`dict`, (`Backlink
      <https://github.com/TinEye/pytineye/blob/main/pytineye/api.py>`__):

      - `url`, the image URL to the image.
      - `backlink`, the original website URL.
      - `crawl_date`, the date the image was crawled.

    	backlinks
crawl_datez%Y-%m-%dr   backlink
image_name)r   r-   r,   r.   	image_urldomainscorewidthheightsizer$   filesizeoverlaytags)r/   r0   r1   r2   r3   r4   image_formatr5   r6   r7   r+   )
isinstancedictgetr   strptimeminappend)
match_jsonr+   backlink_jsonr,   s       r'   parse_tineye_matchrA   T   s'   > Ij '4 	MmT2&**<8J%..z:F
%\\
(,,U3 - 1 1* =","/"3"3L"A		(  ^^K0..*((..*v&"x0NN:.>>),v& r)   returnc                 "   t               }| j                  dv r| j                         }|j                  di       }d| j                   }| j                  dk(  rM|j                  dd      }|dk(  rt        }na|dk(  rt
        }nU|d	k(  rt        }nIt        j                  d
|       n2|j                  d      }t        |t              rdj                  |      }t        j                  |       |S | j                          | j                         }|d   D ]R  }t        |      }|d   s|d   d   }	|j                  d|	d   |d   |	d   |	d   |	d   |d   |d   |d   |	d   d
       T |j                  d      }
|
r|j                  d|
i       |S )z Parse HTTP response from TinEye.)i    suggestionszHTTP Status Code: rD   key zInvalid image URLNO_SIGNATURE_ERRORzDownload Errorz&Unknown suggestion key encountered: %sdescription,matchesr+   r   zimages.htmlr-   r/   r   r.   r8   r2   r3   r,   )
templater   thumbnail_srcsourcetitleimg_srcr$   r2   r3   publishedDatenum_matchesnumber_of_results)r   status_codejsonr;   FORMAT_NOT_SUPPORTEDrH   DOWNLOAD_ERRORr    warningr9   listjoininforaise_for_statusrA   r>   )respr   	json_datarE   messages_keyrI   r?   tineye_matchr-   rS   s              r'   responserb      s   oG :%IIK	mmM26&t'7'7&89s"OOE2.E++...,**(GO%//-8K+t,((;/ 	G 			I	* 

)*5K(,Q/)
+!-k!:"5/!,/#E?&~6%g.&x0!),!7	

0 "m4+->?@Nr)   N)__doc__urllib.parser   r   flask_babelr   searx.result_typesr   aboutengine_type
categoriespaging
safesearchr"   r#   rV   rH   rW   r(   rA   rb    r)   r'   <module>rm      s    #   , $"G	 " 9[
	
:W  7  => 6AHBm Br)   