Ejemplo n.º 1
0
    def _check_response_for_errors(self, resp: requests.Response) -> None:
        try:
            parsed = resp.json()
        except json.JSONDecodeError:
            if not resp.ok:
                if resp.status_code == 404 and '/friendships/' in resp.url:
                    raise InvalidUserId(
                        f"url: {resp.url} is not recognized by Instagram")

                logger.exception(
                    f"response received: \n{resp.text}\nurl: {resp.url}\nstatus code: {resp.status_code}"
                )
                raise BadResponse("Received a non-200 response from Instagram")
            return
        if resp.ok:
            return

        if parsed.get('description') == 'invalid password':
            raise IncorrectLoginDetails(
                "Instagram does not recognize the provided login details")
        if parsed.get('message') in ("checkpoint_required",
                                     "challenge_required"):
            if not hasattr(self, '_handle_challenge'):
                raise BadResponse(
                    "Challenge required. ChallengeMixin is not mixed in.")
            eh = self._handle_challenge(resp)
            if eh:
                return
        if parsed.get('message') == 'feedback_required':
            # TODO: implement a handler for this error
            raise BadResponse(
                "Something unexpected happened. Please check the IG app.")
        if parsed.get('message') == 'rate_limit_error':
            raise TimeoutError("Calm down. Please try again in a few minutes.")
        raise BadResponse("Received a non-200 response from Instagram")
Ejemplo n.º 2
0
    def _check_response_for_errors(self, resp: requests.Response) -> None:
        if resp.ok:
            return

        try:
            parsed = resp.json()
        except json.JSONDecodeError:
            if resp.status_code == 404 and '/friendships/' in resp.url:
                raise InvalidUserId(f"account id: {resp.url.split('/')[-2]} is not recognized by Instagram or you do not have a relation with this account.")

            logger.exception(f"response received: \n{resp.text}\nurl: {resp.url}\nstatus code: {resp.status_code}")
            raise BadResponse("Received a non-200 response from Instagram")

        if parsed.get('error_type') == 'bad_password':
            raise IncorrectLoginDetails("Instagram does not recognize the provided login details")
        if parsed.get('message') in ("checkpoint_required", "challenge_required"):
            if not hasattr(self, '_handle_challenge'):
                raise BadResponse("Challenge required. ChallengeMixin is not mixed in.")
            eh = self._handle_challenge(resp)
            if eh:
                return
        if parsed.get('message') == 'feedback_required':
            if os.environ.get("ENABLE_INSTAUTO_USAGE_METRICS", True):
                # This logs which actions cause limitations on Instagram accounts.
                # I use this data to focus my development on area's where it's most needed.
                requests.post('https://instauto.rooy.dev/feedback_required', data={
                    'feedback_url': parsed.get('feedback_url'),
                    'category': parsed.get('category')
                })
            raise BadResponse("Something unexpected happened. Please check the IG app.")
        if parsed.get('message') == 'rate_limit_error':
            raise TimeoutError("Calm down. Please try again in a few minutes.")
        if parsed.get('message') == 'Not authorized to view user':
            raise AuthorizationError("This is a private user, which you do not follow.")
        raise BadResponse("Received a non-200 response from Instagram")
Ejemplo n.º 3
0
    def _check_response_for_errors(self, resp):
        try:
            parsed = resp.json()
        except json.JSONDecodeError:
            if not resp.ok:
                if resp.status_code == 404 and '/friendships/' in resp.url:
                    raise InvalidUserId(f"url: {resp.url} is not recognized by Instagram")

                logger.exception(f"response received: \n{resp.text}\nurl: {resp.url}\nstatus code: {resp.status_code}")
                raise Exception("Received a non-200 response from Instagram")
            return
        if not resp.ok and parsed.get('description') == 'invalid password':
            raise IncorrectLoginDetails("Instagram does not recognize the provided login details")
Ejemplo n.º 4
0
    def _check_response_for_errors(self, resp: requests.Response) -> None:
        if resp.ok:
            return

        try:
            # pyre-ignore[9]: assume the response is a dictionary
            parsed: Dict[Any, Any] = self._json_loads(resp.text)
        except orjson.JSONDecodeError:
            if resp.status_code == 404 and '/friendships/' in resp.url:
                raise InvalidUserId(
                    f"account id: {resp.url.split('/')[-2]} is not recognized "
                    f"by Instagram or you do not have a relation with this account."
                )

            logger.exception(
                f"response received: \n{resp.text}\nurl: {resp.url}\nstatus code: {resp.status_code}"
            )
            raise BadResponse("Received a non-200 response from Instagram")

        message = parsed.get('message')
        error_type = parsed.get('error_type')
        two_factor_required = parsed.get('two_factor_required', False)

        if two_factor_required:
            return self._handle_2fa(parsed)
        if error_type == 'bad_password':
            raise IncorrectLoginDetails(
                "Instagram does not recognize the provided login details")
        if message in ("checkpoint_required", "challenge_required"):
            if self._handle_checkpoint(resp):
                return
        if error_type == 'feedback_required':
            self._handle_feedback_required(parsed)
        if error_type == 'rate_limit_error':
            raise Exception("Calm down. Please try again in a few minutes.")
        if error_type == 'Not authorized to view user':
            raise AuthorizationError(
                "This is a private user, which you do not follow.")
        raise BadResponse(f"Received a non-200 response from Instagram: \
            {message}")
Ejemplo n.º 5
0
    def _check_response_for_errors(self, resp: requests.Response) -> None:
        if resp.ok:
            return

        try:
            parsed = resp.json()
        except json.JSONDecodeError:
            if resp.status_code == 404 and '/friendships/' in resp.url:
                raise InvalidUserId(
                    f"account id: {resp.url.split('/')[-2]} is not recognized by Instagram or you do not have a relation with this account."
                )

            logger.exception(
                f"response received: \n{resp.text}\nurl: {resp.url}\nstatus code: {resp.status_code}"
            )
            raise BadResponse("Received a non-200 response from Instagram")

        if parsed.get('error_type') == 'bad_password':
            raise IncorrectLoginDetails(
                "Instagram does not recognize the provided login details")
        if parsed.get('message') in ("checkpoint_required",
                                     "challenge_required"):
            if not hasattr(self, '_handle_challenge'):
                raise BadResponse(
                    "Challenge required. ChallengeMixin is not mixed in.")
            eh = self._handle_challenge(resp)
            if eh:
                return
        if parsed.get('message') == 'feedback_required':
            # TODO: implement a handler for this error
            raise BadResponse(
                "Something unexpected happened. Please check the IG app.")
        if parsed.get('message') == 'rate_limit_error':
            raise TimeoutError("Calm down. Please try again in a few minutes.")
        if parsed.get('message') == 'Not authorized to view user':
            raise AuthorizationError(
                "This is a private user, which you do not follow.")
        raise BadResponse("Received a non-200 response from Instagram")