tineye.py 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  1. # SPDX-License-Identifier: AGPL-3.0-or-later
  2. # lint: pylint
  3. """This engine implements *Tineye - reverse image search*
  4. Using TinEye, you can search by image or perform what we call a reverse image
  5. search. You can do that by uploading an image or searching by URL. You can also
  6. simply drag and drop your images to start your search. TinEye constantly crawls
  7. the web and adds images to its index. Today, the TinEye index is over 50.2
  8. billion images `[tineye.com] <https://tineye.com/how>`_.
  9. .. hint::
  10. This SearXNG engine only supports *'searching by URL'* and it does not use
  11. the official API `[api.tineye.com] <https://api.tineye.com/python/docs/>`_.
  12. """
  13. from urllib.parse import urlencode
  14. from datetime import datetime
  15. about = {
  16. "website": 'https://tineye.com',
  17. "wikidata_id": 'Q2382535',
  18. "official_api_documentation": 'https://api.tineye.com/python/docs/',
  19. "use_official_api": False,
  20. "require_api_key": False,
  21. "results": 'JSON',
  22. }
  23. engine_type = 'online_url_search'
  24. categories = ['general']
  25. paging = True
  26. safesearch = False
  27. base_url = 'https://tineye.com'
  28. search_string = '/result_json/?page={page}&{query}'
  29. def request(query, params):
  30. if params['search_urls']['data:image']:
  31. query = params['search_urls']['data:image']
  32. elif params['search_urls']['http']:
  33. query = params['search_urls']['http']
  34. query = urlencode({'url': query})
  35. # see https://github.com/TinEye/pytineye/blob/main/pytineye/api.py
  36. params['url'] = base_url + search_string.format(query=query, page=params['pageno'])
  37. params['headers'].update(
  38. {
  39. 'Connection': 'keep-alive',
  40. 'Accept-Encoding': 'gzip, defalte, br',
  41. 'Host': 'tineye.com',
  42. 'DNT': '1',
  43. 'TE': 'trailers',
  44. }
  45. )
  46. return params
  47. def response(resp):
  48. results = []
  49. # Define wanted results
  50. json_data = resp.json()
  51. number_of_results = json_data['num_matches']
  52. for i in json_data['matches']:
  53. image_format = i['format']
  54. width = i['width']
  55. height = i['height']
  56. thumbnail_src = i['image_url']
  57. backlink = i['domains'][0]['backlinks'][0]
  58. url = backlink['backlink']
  59. source = backlink['url']
  60. title = backlink['image_name']
  61. img_src = backlink['url']
  62. # Get and convert published date
  63. api_date = backlink['crawl_date'][:-3]
  64. publishedDate = datetime.fromisoformat(api_date)
  65. # Append results
  66. results.append(
  67. {
  68. 'template': 'images.html',
  69. 'url': url,
  70. 'thumbnail_src': thumbnail_src,
  71. 'source': source,
  72. 'title': title,
  73. 'img_src': img_src,
  74. 'format': image_format,
  75. 'widht': width,
  76. 'height': height,
  77. 'publishedDate': publishedDate,
  78. }
  79. )
  80. # Append number of results
  81. results.append({'number_of_results': number_of_results})
  82. return results