pinterest.py 1.7 KB

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