Skip to content

Commit 9917426

Browse files
RocksGarnettspookslayer
andauthored
Adds visual search (bstoilov#91)
* added visual search * Update README.md Co-authored-by: spookslayer <[email protected]>
1 parent 358f90c commit 9917426

File tree

2 files changed

+59
-0
lines changed

2 files changed

+59
-0
lines changed

README.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -180,7 +180,12 @@ If username is not provided current user will be used
180180

181181
Current pinterest scopes are: pins, buyable_pins, my_pins, videos, boards
182182

183+
### Visual Search
183184

185+
```
186+
pin_data = pinterest.load_pin(pin_id='pin_id')
187+
search_batch = pinterest.visual_search(pin_data, x=10, y=50, w=100, h=100)
188+
```
184189
## User interactions
185190
### Invite to board
186191

py3pin/Pinterest.py

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@
3737
PIN_COMMENT_RESOURCE = 'https://www.pinterest.com/resource/PinCommentResource/create/'
3838
BOARD_INVITE_RESOURCE = 'https://www.pinterest.com/_ngjs/resource/BoardInviteResource/create/'
3939
BOARD_DELETE_INVITE_RESOURCE = 'https://www.pinterest.com/_ngjs/resource/BoardCollaboratorResource/delete/'
40+
VISUAL_LIVE_SEARCH_RESOURCE = 'https://www.pinterest.com/resource/VisualLiveSearchResource/get'
4041
SEARCH_RESOURCE = 'https://www.pinterest.com/resource/SearchResource/get'
4142
BOARD_RECOMMEND_RESOURCE = 'https://www.pinterest.com/_ngjs/resource/BoardContentRecommendationResource/get'
4243
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):
625626
data = self.req_builder.buildPost(options=options)
626627
return self.post(url=BOARD_DELETE_INVITE_RESOURCE, data=data)
627628

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+
628682
def search(self, scope, query, page_size=250):
629683
"""
630684
Gives access to pinterest search api

0 commit comments

Comments
 (0)