Skip to content

Related pins #224

Description

@ifilipis

I'm not gonna create a PR for this. There was one quite essential API missing, which is when you click on a pin, and it shows relevant pins

Just added these two changes

RELATED_MODULES_RESOURCE = "https://www.pinterest.com/resource/RelatedModulesResource/get/"

    def related_pins(
        self,
        pin_id,
        page_size=50,
        search_query="",
        context_pin_ids=None,
        reset_bookmark=False,
    ):
        """
        Loads pins related to a target pin.
        This method is batched. In order to obtain all related pins
        you need to call it until an empty list is returned.
        :param pin_id: target pin id
        :param page_size: batch size
        :param search_query: optional query used to refine related pins
        :param context_pin_ids: optional list of already loaded context pin ids
        :param reset_bookmark: resets pagination for this pin
        :return: list of related pin objects
        """
        if context_pin_ids is None:
            context_pin_ids = []

        if reset_bookmark:
            self.bookmark_manager.reset_bookmark(
                primary="related_pins", secondary=pin_id
            )

        next_bookmark = self.bookmark_manager.get_bookmark(
            primary="related_pins", secondary=pin_id
        )

        if next_bookmark == "-end-":
            return []

        options = {
            "additional_fields": ["pin.gen_ai_topics"],
            "pin_id": pin_id,
            "context_pin_ids": context_pin_ids,
            "page_size": page_size,
            "search_query": search_query,
            "source": "deep_linking",
            "top_level_source": "deep_linking",
            "top_level_source_depth": 1,
            "is_pdp": False,
            "bookmarks": [next_bookmark],
        }
        source_url = "/pin/{}/".format(pin_id)
        url = self.req_builder.buildGet(
            url=RELATED_MODULES_RESOURCE, options=options, source_url=source_url
        )
        resp = self.get(url=url).json()

        bookmark = "-end-"
        if "resource" in resp:
            bookmark = resp["resource"]["options"]["bookmarks"][0]
        elif "bookmark" in resp.get("resource_response", {}):
            bookmark = resp["resource_response"]["bookmark"]

        self.bookmark_manager.add_bookmark(
            primary="related_pins", secondary=pin_id, bookmark=bookmark
        )

        results = resp["resource_response"]["data"]
        if isinstance(results, dict) and "results" in results:
            results = results["results"]

        return [result for result in results if result.get("type") == "pin"]

Metadata

Metadata

Assignees

No one assigned

    Labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions