Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

load pin error #208

Open
faelern opened this issue Aug 19, 2024 · 1 comment
Open

load pin error #208

faelern opened this issue Aug 19, 2024 · 1 comment

Comments

@faelern
Copy link

faelern commented Aug 19, 2024

Function load_pin returns following error: in py3pin\Pinterest.py", line 718, in load_pin
pinJsonData = json.loads(s.contents[0])["response"]["data"]["v3GetPinQuery"]["data"]
KeyError: 'v3GetPinQuery'

@runcode1
Copy link

Function load_pin returns following error: in py3pin\Pinterest.py", line 718, in load_pin pinJsonData = json.loads(s.contents[0])["response"]["data"]["v3GetPinQuery"]["data"] KeyError: 'v3GetPinQuery'

Adjust the load_pin method

def load_pin(self, pin_id):
    """
    Loads full information about a pin
    :param pin_id: pin id to load
    :return: python dict describing the pinterest response
    :raises: Exception if pin data cannot be found or parsed
    """
    try:
        resp = self.get(url=self.LOAD_PIN_URL_FORMAT.format(pin_id))
        resp.raise_for_status()  # Raises an HTTPError for bad responses
    except Exception as e:
        raise Exception(f"Failed to fetch pin data: {str(e)}")

    try:
        soup = BeautifulSoup(resp.text, "html.parser")
        script = soup.find("script", id="__PWS_INITIAL_PROPS__")

        if not script:
            raise Exception("__PWS_INITIAL_PROPS__ script not found. Pinterest may have changed their structure.")

        data = json.loads(script.string)
        pin_data = data["initialReduxState"]["pins"].get(pin_id)

        if not pin_data:
            raise Exception(f"Pin data not found for pin_id: {pin_id}")

        return pin_data

    except json.JSONDecodeError:
        raise Exception("Failed to parse JSON data from __PWS_INITIAL_PROPS__")
    except KeyError as e:
        raise Exception(f"Expected key not found in Pinterest response: {str(e)}")
    except Exception as e:
        raise Exception(f"An unexpected error occurred: {str(e)}")

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants