yummly.py 2.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879
  1. # SPDX-License-Identifier: AGPL-3.0-or-later
  2. # lint: pylint
  3. """Yummly
  4. """
  5. from urllib.parse import urlencode
  6. from flask_babel import gettext
  7. from searx.utils import markdown_to_text
  8. about = {
  9. "website": 'https://yummly.com',
  10. "wikidata_id": 'Q8061140',
  11. # not used since it requires an api key
  12. "official_api_documentation": 'https://developer.yummly.com/documentation.html',
  13. "use_official_api": False,
  14. "require_api_key": False,
  15. "results": 'JSON',
  16. }
  17. paging = True
  18. categories = []
  19. api_url = "https://mapi.yummly.com"
  20. number_of_results = 10
  21. show_pro_recipes = False
  22. base_url = "https://www.yummly.com"
  23. def request(query, params):
  24. args = {
  25. 'q': query,
  26. 'start': (params['pageno'] - 1) * number_of_results,
  27. 'maxResult': number_of_results,
  28. }
  29. params['url'] = f"{api_url}/mapi/v23/content/search?{urlencode(args)}&allowedContent=single_recipe"
  30. return params
  31. def response(resp):
  32. results = []
  33. json = resp.json()
  34. for result in json['feed']:
  35. # don't show pro recipes since they can't be accessed without an account
  36. if not show_pro_recipes and result['proRecipe']:
  37. continue
  38. content = result['seo']['web']['meta-tags']['description']
  39. description = result['content']['description']
  40. if description is not None:
  41. content = markdown_to_text(description['text'])
  42. img_src = None
  43. if result['display']['images']:
  44. img_src = result['display']['images'][0]
  45. elif result['content']['details']['images']:
  46. img_src = result['content']['details']['images'][0]['resizableImageUrl']
  47. url = result['display']['source']['sourceRecipeUrl']
  48. if 'www.yummly.com/private' in url:
  49. url = base_url + '/' + result['tracking-id']
  50. results.append(
  51. {
  52. 'url': url,
  53. 'title': result['display']['displayName'],
  54. 'content': content,
  55. 'img_src': img_src,
  56. 'metadata': f"{gettext('Language')}: {result['locale'].split('-')[0]}",
  57. }
  58. )
  59. for suggestion in json['relatedPhrases']['keywords']:
  60. results.append({'suggestion': suggestion})
  61. return results