tineye.py 2.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283
  1. # SPDX-License-Identifier: AGPL-3.0-or-later
  2. # lint: pylint
  3. """Tineye - Reverse search images
  4. """
  5. from json import loads
  6. from urllib.parse import urlencode
  7. from datetime import datetime
  8. about = {
  9. "website": 'https://tineye.com',
  10. "wikidata_id": 'Q2382535',
  11. "use_official_api": False,
  12. "require_api_key": False,
  13. "results": 'JSON',
  14. }
  15. categories = ['images']
  16. paging = True
  17. safesearch = False
  18. base_url = 'https://tineye.com'
  19. search_string = '/result_json/?page={page}&{query}'
  20. def request(query, params):
  21. # see https://github.com/TinEye/pytineye/blob/main/pytineye/api.py
  22. params['url'] = base_url + search_string.format(query=urlencode({'url': query}), page=params['pageno'])
  23. params['headers'].update(
  24. {
  25. 'Connection': 'keep-alive',
  26. 'Accept-Encoding': 'gzip, defalte, br',
  27. 'Host': 'tineye.com',
  28. 'DNT': '1',
  29. 'TE': 'trailers',
  30. }
  31. )
  32. return params
  33. def response(resp):
  34. results = []
  35. # Define wanted results
  36. json_data = loads(resp.text)
  37. number_of_results = json_data['num_matches']
  38. for i in json_data['matches']:
  39. image_format = i['format']
  40. width = i['width']
  41. height = i['height']
  42. thumbnail_src = i['image_url']
  43. backlink = i['domains'][0]['backlinks'][0]
  44. url = backlink['backlink']
  45. source = backlink['url']
  46. title = backlink['image_name']
  47. img_src = backlink['url']
  48. # Get and convert published date
  49. api_date = backlink['crawl_date'][:-3]
  50. publishedDate = datetime.fromisoformat(api_date)
  51. # Append results
  52. results.append(
  53. {
  54. 'template': 'images.html',
  55. 'url': url,
  56. 'thumbnail_src': thumbnail_src,
  57. 'source': source,
  58. 'title': title,
  59. 'img_src': img_src,
  60. 'format': image_format,
  61. 'widht': width,
  62. 'height': height,
  63. 'publishedDate': publishedDate,
  64. }
  65. )
  66. # Append number of results
  67. results.append({'number_of_results': number_of_results})
  68. return results