yummly.py 2.2 KB

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