|
37 | 37 | PIN_COMMENT_RESOURCE = 'https://www.pinterest.com/resource/PinCommentResource/create/' |
38 | 38 | BOARD_INVITE_RESOURCE = 'https://www.pinterest.com/_ngjs/resource/BoardInviteResource/create/' |
39 | 39 | BOARD_DELETE_INVITE_RESOURCE = 'https://www.pinterest.com/_ngjs/resource/BoardCollaboratorResource/delete/' |
| 40 | +VISUAL_LIVE_SEARCH_RESOURCE = 'https://www.pinterest.com/resource/VisualLiveSearchResource/get' |
40 | 41 | SEARCH_RESOURCE = 'https://www.pinterest.com/resource/SearchResource/get' |
41 | 42 | BOARD_RECOMMEND_RESOURCE = 'https://www.pinterest.com/_ngjs/resource/BoardContentRecommendationResource/get' |
42 | 43 | PINNABLE_IMAGES_RESOURCE = 'https://www.pinterest.com/_ngjs/resource/FindPinImagesResource/get' |
@@ -625,6 +626,59 @@ def delete_invite(self, board_id, invited_user_id, also_block=False): |
625 | 626 | data = self.req_builder.buildPost(options=options) |
626 | 627 | return self.post(url=BOARD_DELETE_INVITE_RESOURCE, data=data) |
627 | 628 |
|
| 629 | + def visual_search(self, pin_data, x = None, y = None, w = None, h = None, padding=10): |
| 630 | + """ |
| 631 | + Gives access to pinterest search api |
| 632 | + This method is batched, meaning is needs to be called until empty list is returned. |
| 633 | + :param pin_data: pin data |
| 634 | + :param x: x position of the cropped part of the image used for searching |
| 635 | + :param y: y position of the cropped part of the image used for searching |
| 636 | + :param w: width of the cropped part of the image used for searching |
| 637 | + :param h: height of the cropped part of the image used for searching |
| 638 | + :param padding: Default padding for for cropped image. |
| 639 | +
|
| 640 | + :return: python dict describing the pinterest response |
| 641 | + """ |
| 642 | + |
| 643 | + orig = pin_data['images']['orig'] |
| 644 | + width = orig['width'] |
| 645 | + height = orig['height'] |
| 646 | + image_signature = pin_data['image_signature'] |
| 647 | + pin_id = pin_data['id'] |
| 648 | + |
| 649 | + x = padding if x is None else x |
| 650 | + y = padding if y is None else y |
| 651 | + w = width - padding * 2 if w is None else w |
| 652 | + h = height - padding * 2 if h is None else h |
| 653 | + |
| 654 | + source_url = '/pin/{}/visual-search/?x={}&y={}&w={}&h={}'.format(pin_id, x, y, w, h) |
| 655 | + |
| 656 | + next_bookmark = self.bookmark_manager.get_bookmark(primary='visual_search', secondary=source_url) |
| 657 | + if next_bookmark == '-end-': |
| 658 | + return [] |
| 659 | + |
| 660 | + options = { |
| 661 | + "isPrefetch": False, |
| 662 | + "pin_id":pin_id, |
| 663 | + "image_signature":image_signature, |
| 664 | + "crop":{ |
| 665 | + "x": x / width, |
| 666 | + "y": y / height, |
| 667 | + "w": w / width, |
| 668 | + "h": h / height |
| 669 | + }, |
| 670 | + "bookmarks": [next_bookmark], |
| 671 | + "no_fetch_context_on_resource": False |
| 672 | + } |
| 673 | + url = self.req_builder.buildGet(url=VISUAL_LIVE_SEARCH_RESOURCE, options=options, source_url=source_url) |
| 674 | + resp = self.get(url=url).json() |
| 675 | + |
| 676 | + bookmark = resp['resource']['options']['bookmarks'][0] |
| 677 | + |
| 678 | + self.bookmark_manager.add_bookmark(primary='visual_search', secondary=source_url, bookmark=bookmark) |
| 679 | + |
| 680 | + return resp['resource_response']['data']['results'] |
| 681 | + |
628 | 682 | def search(self, scope, query, page_size=250): |
629 | 683 | """ |
630 | 684 | Gives access to pinterest search api |
|
0 commit comments