Skip to content

Commit

Permalink
Adds visual search (#91)
Browse files Browse the repository at this point in the history
* added visual search

* Update README.md

Co-authored-by: spookslayer <74124595+spookslayer@users.noreply.github.com>
  • Loading branch information
RocksGarnett and spookslayer committed Nov 27, 2020
1 parent 358f90c commit 9917426
Show file tree
Hide file tree
Showing 2 changed files with 59 additions and 0 deletions.
5 changes: 5 additions & 0 deletions README.md
Expand Up @@ -180,7 +180,12 @@ If username is not provided current user will be used

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

### Visual Search

```
pin_data = pinterest.load_pin(pin_id='pin_id')
search_batch = pinterest.visual_search(pin_data, x=10, y=50, w=100, h=100)
```
## User interactions
### Invite to board

Expand Down
54 changes: 54 additions & 0 deletions py3pin/Pinterest.py
Expand Up @@ -37,6 +37,7 @@
PIN_COMMENT_RESOURCE = 'https://www.pinterest.com/resource/PinCommentResource/create/'
BOARD_INVITE_RESOURCE = 'https://www.pinterest.com/_ngjs/resource/BoardInviteResource/create/'
BOARD_DELETE_INVITE_RESOURCE = 'https://www.pinterest.com/_ngjs/resource/BoardCollaboratorResource/delete/'
VISUAL_LIVE_SEARCH_RESOURCE = 'https://www.pinterest.com/resource/VisualLiveSearchResource/get'
SEARCH_RESOURCE = 'https://www.pinterest.com/resource/SearchResource/get'
BOARD_RECOMMEND_RESOURCE = 'https://www.pinterest.com/_ngjs/resource/BoardContentRecommendationResource/get'
PINNABLE_IMAGES_RESOURCE = 'https://www.pinterest.com/_ngjs/resource/FindPinImagesResource/get'
Expand Down Expand Up @@ -625,6 +626,59 @@ def delete_invite(self, board_id, invited_user_id, also_block=False):
data = self.req_builder.buildPost(options=options)
return self.post(url=BOARD_DELETE_INVITE_RESOURCE, data=data)

def visual_search(self, pin_data, x = None, y = None, w = None, h = None, padding=10):
"""
Gives access to pinterest search api
This method is batched, meaning is needs to be called until empty list is returned.
:param pin_data: pin data
:param x: x position of the cropped part of the image used for searching
:param y: y position of the cropped part of the image used for searching
:param w: width of the cropped part of the image used for searching
:param h: height of the cropped part of the image used for searching
:param padding: Default padding for for cropped image.
:return: python dict describing the pinterest response
"""

orig = pin_data['images']['orig']
width = orig['width']
height = orig['height']
image_signature = pin_data['image_signature']
pin_id = pin_data['id']

x = padding if x is None else x
y = padding if y is None else y
w = width - padding * 2 if w is None else w
h = height - padding * 2 if h is None else h

source_url = '/pin/{}/visual-search/?x={}&y={}&w={}&h={}'.format(pin_id, x, y, w, h)

next_bookmark = self.bookmark_manager.get_bookmark(primary='visual_search', secondary=source_url)
if next_bookmark == '-end-':
return []

options = {
"isPrefetch": False,
"pin_id":pin_id,
"image_signature":image_signature,
"crop":{
"x": x / width,
"y": y / height,
"w": w / width,
"h": h / height
},
"bookmarks": [next_bookmark],
"no_fetch_context_on_resource": False
}
url = self.req_builder.buildGet(url=VISUAL_LIVE_SEARCH_RESOURCE, options=options, source_url=source_url)
resp = self.get(url=url).json()

bookmark = resp['resource']['options']['bookmarks'][0]

self.bookmark_manager.add_bookmark(primary='visual_search', secondary=source_url, bookmark=bookmark)

return resp['resource_response']['data']['results']

def search(self, scope, query, page_size=250):
"""
Gives access to pinterest search api
Expand Down

0 comments on commit 9917426

Please sign in to comment.