pinterest.py 1.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667
  1. # SPDX-License-Identifier: AGPL-3.0-or-later
  2. # lint: pylint
  3. """Pinterest (images)
  4. """
  5. from json import dumps
  6. about = {
  7. "website": 'https://www.pinterest.com/',
  8. "wikidata_id": 'Q255381',
  9. "official_api_documentation": 'https://developers.pinterest.com/docs/api/v5/',
  10. "use_official_api": False,
  11. "require_api_key": False,
  12. "results": 'JSON',
  13. }
  14. categories = ['images']
  15. paging = True
  16. base_url = 'https://www.pinterest.com'
  17. def request(query, params):
  18. args = {
  19. 'options': {
  20. 'query': query,
  21. 'bookmarks': [params['engine_data'].get('bookmark', '')],
  22. },
  23. 'context': {},
  24. }
  25. params['url'] = f"{base_url}/resource/BaseSearchResource/get/?data={dumps(args)}"
  26. return params
  27. def response(resp):
  28. results = []
  29. json_resp = resp.json()
  30. results.append(
  31. {
  32. 'engine_data': json_resp['resource_response']['bookmark'],
  33. # it's called bookmark by pinterest, but it's rather a nextpage
  34. # parameter to get the next results
  35. 'key': 'bookmark',
  36. }
  37. )
  38. for result in json_resp['resource_response']['data']['results']:
  39. if result['type'] == 'story':
  40. continue
  41. results.append(
  42. {
  43. 'template': 'images.html',
  44. 'url': result['link'] or f"{base_url}/pin/{result['id']}/",
  45. 'title': result.get('title') or result.get('grid_title'),
  46. 'content': (result.get('rich_summary') or {}).get('display_description') or "",
  47. 'img_src': result['images']['orig']['url'],
  48. 'thumbnail_src': result['images']['236x']['url'],
  49. 'source': (result.get('rich_summary') or {}).get('site_name'),
  50. }
  51. )
  52. return results