def reset_password(self, username: str) -> Dict: """ Reset your password Returns ------- Dict Jsonified response from Instagram """ response = requests.post( "https://www.instagram.com/accounts/account_recovery_send_ajax/", data={ "email_or_username": username, "recaptcha_challenge_field": "" }, headers={ "x-requested-with": "XMLHttpRequest", "x-csrftoken": gen_csrftoken(), "Connection": "Keep-Alive", "Accept": "*/*", "Accept-Encoding": "gzip,deflate", "Accept-Language": "en-US", "User-Agent": "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_13_6) AppleWebKit/605.1.15 (KHTML, like Gecko) Version/11.1.2 Safari/605.1.15" }, proxies=self.public.proxies ) try: return response.json() except JSONDecodeError as e: if "/login/" in response.url: raise ClientLoginRequired(e, response=response) raise ClientError(e, response=response)
def _send_public_request(self, url, data=None, params=None, headers=None, return_json=False): self.public_requests_count += 1 if headers: self.public.headers.update(headers) if self.request_timeout: time.sleep(self.request_timeout) try: if data is not None: # POST response = self.public.data(url, data=data, params=params) else: # GET response = self.public.get(url, params=params) expected_length = int(response.headers.get("Content-Length") or 0) actual_length = response.raw.tell() if actual_length < expected_length: raise ClientIncompleteReadError( "Incomplete read ({} bytes read, {} more expected)".format( actual_length, expected_length), response=response, ) self.request_logger.debug("public_request %s: %s", response.status_code, response.url) self.request_logger.info( "[%s] [%s] %s %s", self.public.proxies.get("https"), response.status_code, "POST" if data else "GET", response.url, ) self.last_public_response = response response.raise_for_status() if return_json: self.last_public_json = response.json() return self.last_public_json return response.text except JSONDecodeError as e: if "/login/" in response.url: raise ClientLoginRequired(e, response=response) self.request_logger.error( "Status %s: JSONDecodeError in public_request (url=%s) >>> %s", response.status_code, response.url, response.text, ) raise ClientJSONDecodeError( "JSONDecodeError {0!s} while opening {1!s}".format(e, url), response=response, ) except requests.HTTPError as e: if e.response.status_code == 403: raise ClientForbiddenError(e, response=e.response) if e.response.status_code == 400: raise ClientBadRequestError(e, response=e.response) if e.response.status_code == 429: raise ClientThrottledError(e, response=e.response) if e.response.status_code == 404: raise ClientNotFoundError(e, response=e.response) raise ClientError(e, response=e.response) except requests.ConnectionError as e: raise ClientConnectionError("{} {}".format(e.__class__.__name__, str(e)))