mirror of
https://github.com/instaloader/instaloader.git
synced 2026-10-16 21:52:17 -07:00
Compare commits
4
Commits
v4.10
...
2fa-experiments
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
4022e7fae7 | ||
|
|
ac28a33b4d | ||
|
|
8bc35cf3a1 | ||
|
|
e48251e75e |
@@ -253,6 +253,8 @@ class InstaloaderContext:
|
||||
'ig_vw': '1920', 'ig_cb': '1', 'csrftoken': '',
|
||||
's_network': '', 'ds_user_id': ''})
|
||||
session.headers.update(self._default_http_header())
|
||||
# Getting mid cookie value
|
||||
session.get("https://i.instagram.com/api/v1/web/login_page/")
|
||||
# Override default timeout behavior.
|
||||
# Need to silence mypy bug for this. See: https://github.com/python/mypy/issues/2427
|
||||
session.request = partial(session.request, timeout=self.request_timeout) # type: ignore
|
||||
@@ -307,7 +309,8 @@ class InstaloaderContext:
|
||||
# username is invalid.
|
||||
raise InvalidArgumentException('Login error: User {} does not exist.'.format(user))
|
||||
# '{"authenticated": true, "user": true, "userId": ..., "oneTapPrompt": false, "status": "ok"}'
|
||||
session.headers.update({'X-CSRFToken': login.cookies['csrftoken']})
|
||||
if 'csrftoken' in login.cookies:
|
||||
session.headers.update({'X-CSRFToken': login.cookies['csrftoken']})
|
||||
self._session = session
|
||||
self.username = user
|
||||
self.user_id = resp_json['userId']
|
||||
@@ -324,8 +327,15 @@ class InstaloaderContext:
|
||||
raise InvalidArgumentException("No two-factor authentication pending.")
|
||||
(session, user, two_factor_id) = self.two_factor_auth_pending
|
||||
|
||||
login = session.post('https://www.instagram.com/accounts/login/ajax/two_factor/',
|
||||
data={'username': user, 'verificationCode': two_factor_code, 'identifier': two_factor_id},
|
||||
login = session.post('https://www.instagram.com/api/v1/web/accounts/login/ajax/two_factor/',
|
||||
data={
|
||||
'identifier': two_factor_id,
|
||||
'queryParams': '{"next":"/"}',
|
||||
'trust_signal': 'true',
|
||||
'username': user,
|
||||
'verification_method': '1',
|
||||
'verificationCode': two_factor_code,
|
||||
},
|
||||
allow_redirects=True)
|
||||
resp_json = login.json()
|
||||
if resp_json['status'] != 'ok':
|
||||
@@ -333,7 +343,8 @@ class InstaloaderContext:
|
||||
raise BadCredentialsException("2FA error: {}".format(resp_json['message']))
|
||||
else:
|
||||
raise BadCredentialsException("2FA error: \"{}\" status.".format(resp_json['status']))
|
||||
session.headers.update({'X-CSRFToken': login.cookies['csrftoken']})
|
||||
if 'csrftoken' in login.cookies:
|
||||
session.headers.update({'X-CSRFToken': login.cookies['csrftoken']})
|
||||
self._session = session
|
||||
self.username = user
|
||||
self.two_factor_auth_pending = None
|
||||
@@ -405,20 +416,13 @@ class InstaloaderContext:
|
||||
raise ConnectionException("HTTP error code {}.".format(resp.status_code))
|
||||
is_html_query = not is_graphql_query and not "__a" in params and host == "www.instagram.com"
|
||||
if is_html_query:
|
||||
match = re.search(r'window\._sharedData = (.*);</script>', resp.text)
|
||||
# Extract JSON from HTML response
|
||||
match = re.search('(?<={"raw":").*?(?<!\\\\)(?=")', resp.text)
|
||||
if match is None:
|
||||
raise QueryReturnedNotFoundException("Could not find \"window._sharedData\" in html response.")
|
||||
resp_json = json.loads(match.group(1))
|
||||
entry_data = resp_json.get('entry_data')
|
||||
post_or_profile_page = list(entry_data.values())[0] if entry_data is not None else None
|
||||
if post_or_profile_page is None:
|
||||
raise ConnectionException("\"window._sharedData\" does not contain required keys.")
|
||||
# If GraphQL data is missing in `window._sharedData`, search for it in `__additionalDataLoaded`.
|
||||
if 'graphql' not in post_or_profile_page[0]:
|
||||
match = re.search(r'window\.__additionalDataLoaded\(.*?({.*"graphql":.*})\);</script>',
|
||||
resp.text)
|
||||
if match is not None:
|
||||
post_or_profile_page[0]['graphql'] = json.loads(match.group(1))['graphql']
|
||||
raise QueryReturnedNotFoundException("Could not find JSON data in html response.")
|
||||
# Unescape escaped JSON string
|
||||
unescaped_string = match.group(0).encode("utf-8").decode("unicode_escape")
|
||||
resp_json = json.loads(unescaped_string)
|
||||
return resp_json
|
||||
else:
|
||||
resp_json = resp.json()
|
||||
|
||||
@@ -27,7 +27,8 @@ class LatestStamps:
|
||||
self.data.read(latest_stamps_file)
|
||||
|
||||
def _save(self):
|
||||
makedirs(dirname(self.file), exist_ok=True)
|
||||
if dn := dirname(self.file):
|
||||
makedirs(dn, exist_ok=True)
|
||||
with open(self.file, 'w') as f:
|
||||
self.data.write(f)
|
||||
|
||||
|
||||
Reference in New Issue
Block a user