Skip to content

Commit

Permalink
Added bookmarks support for get_section_pins() and reset_bookmark() o…
Browse files Browse the repository at this point in the history
…ption (#83)

* Reset bookmark option added

If you need to reuse get_section_pins() or get_board_sections() in a while cycle, you can make a bookmark reset to the initial value.

* Added bookmarks support for get_board_sections()

Now you can obtain more than 25 sections using while cycle.

* Added reset_bookmark() support

If you need to reuse get_board_sections() or get_section_pins() after all the stuff was collected - switch reset_bookmark=True.
  • Loading branch information
imgVOID committed Oct 9, 2020
1 parent 871dce3 commit 79df87e
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 4 deletions.
6 changes: 6 additions & 0 deletions py3pin/BookmarkManager.py
Expand Up @@ -20,3 +20,9 @@ def get_bookmark(self, primary, secondary=None):
pass

return None

def reset_bookmark(self, primary, secondary=None):
if primary in self.bookmark_map:
del self.bookmark_map[primary][secondary]
else:
pass
22 changes: 18 additions & 4 deletions py3pin/Pinterest.py
Expand Up @@ -860,7 +860,6 @@ def _get_conversation_batch(self):

url = self.req_builder.buildGet(url=CONVERSATION_RESOURCE, options=options)
response = self.get(url=url).json()

next_bookmark = response['resource']['options']['bookmarks'][0]
self.bookmark_manager.add_bookmark(primary='conversations', bookmark=next_bookmark)

Expand All @@ -880,28 +879,42 @@ def create_board_section(self, board_id='', section_name=''):
data = self.req_builder.buildPost(options=options)
return self.post(url=BOARD_SECTION_RESOURCE, data=data)

def get_board_sections(self, board_id=''):

def get_board_sections(self, board_id='', reset_bookmark=False):
"""
Obtains a list of all sections of a board
"""
next_bookmark = self.bookmark_manager.get_bookmark(primary='board_sections', secondary=board_id)
if next_bookmark == '-end-':
if reset_bookmark:
self.bookmark_manager.reset_bookmark(primary='board_sections', secondary=board_id)
return []

options = {
"isPrefetch": False,
"board_id": board_id,
"redux_normalize_feed": True
"redux_normalize_feed": True,
"bookmarks": [next_bookmark]
}

url = self.req_builder.buildGet(url=GET_BOARD_SECTIONS, options=options)
response = self.get(url=url).json()
bookmark = response['resource']['options']['bookmarks'][0]
self.bookmark_manager.add_bookmark(primary='board_sections', secondary=board_id, bookmark=bookmark)

return response['resource_response']['data']

def get_section_pins(self, section_id='', page_size=250):

def get_section_pins(self, section_id='', page_size=250, reset_bookmark=False):
"""
Returns a list of all pins in a board section.
This method is batched meaning in order to obtain all pins in the section
you need to call is until empty list is returned
"""
next_bookmark = self.bookmark_manager.get_bookmark(primary='section_pins', secondary=section_id)
if next_bookmark == '-end-':
if reset_bookmark:
self.bookmark_manager.reset_bookmark(primary='section_pins', secondary=section_id)
return []

options = {
Expand All @@ -927,6 +940,7 @@ def get_section_pins(self, section_id='', page_size=250):
pins.append(d)
return pins


def delete_board_section(self, section_id=''):
"""
Deletes a board section by id
Expand Down

0 comments on commit 79df87e

Please sign in to comment.