Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion instalooter/_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,9 @@ def needs_extended(self, media):

def get_shared_data(html):
match = re.search(r'window._sharedData = ({[^\n]*});', html)
return json.loads(match.group(1))
if (match):
return json.loads(match.group(1))
return None


def get_additional_data(html):
Expand Down
56 changes: 47 additions & 9 deletions instalooter/looters.py
Original file line number Diff line number Diff line change
Expand Up @@ -126,7 +126,7 @@ def _login(cls, username, password, session=None):
session = cls._init_session(session)
headers = copy.deepcopy(session.headers)
homepage = "https://www.instagram.com/"
login_url = "https://www.instagram.com/accounts/login/ajax/"
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

removing the ajax prefix messes up the login token resulting in error of expecting value: line1 column 1 (char 0)

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sorry I am new to this. So what should the login_url be? Will it fix the "......object is not subscriptable" error ?

login_url = "https://www.instagram.com/accounts/login/"
enc_password = "#PWD_INSTAGRAM_BROWSER:0:{}:{}".format(time.time(), password)
data = {'username': username, 'enc_password': enc_password}

Expand All @@ -144,13 +144,45 @@ def _login(cls, username, password, session=None):
'X-Requested-With': 'XMLHttpRequest'
})

with session.get(homepage) as res:
token = get_shared_data(res.text)['config']['csrf_token']
session.headers.update({'X-CSRFToken': token})
for cookie in session.cookies:
if (cookie.name == 'sessionid'):
cookie.value = ''
elif (cookie.name == 'mid'):
cookie.value = ''
elif (cookie.name == 'ig_pr'):
cookie.value = '1'
elif (cookie.name == 'ig_vw'):
cookie.value = '1920'
elif (cookie.name == 'ig_cb'):
cookie.value = '1'
elif (cookie.name == 'csrftoken'):
cookie.value = ''
elif (cookie.name == 's_network'):
cookie.value = ''
elif (cookie.name == 'ds_user_id'):
cookie.value = ''

with session.get(login_url, params={}, allow_redirects=False) as res:
if res.status_code == 400:
raise ValueError('400 Bad Request')
if res.status_code == 404:
raise ValueError('404 Not Found')
if res.status_code == 429:
raise ValueError('429 Too Many Requests')
if res.status_code != 200:
raise ValueError('HTTP error code {}.'.format(res.status_code))

sData = get_shared_data(res.text)
if (sData and ('config' in sData)):
token = sData['config']['csrf_token']
session.headers.update({'X-CSRFToken': token})
else:
raise SystemError('Login error: Token not found')

time.sleep(5 * random.random()) # nosec
with session.post(login_url, data, allow_redirects=True) as login:
token = next(c.value for c in login.cookies if c.name == 'csrftoken')
with session.post('https://www.instagram.com/accounts/login/ajax/', data, allow_redirects=True) as login:
tokens = list(c.value for c in login.cookies if c.name == 'csrftoken')
token = None if not tokens else tokens[0]
session.headers.update({'X-CSRFToken': token})
if not login.ok:
raise SystemError("Login error: check your connection")
Expand Down Expand Up @@ -278,6 +310,7 @@ def __init__(self,
self.dump_json = dump_json or dump_only
self.extended_dump = extended_dump
self.session = self._init_session(session)
self.rhx = ''
atexit.register(self.session.close)

# Set the default webbrowser user agent
Expand All @@ -286,9 +319,14 @@ def __init__(self,

# Get CSRFToken and RHX
with self.session.get('https://www.instagram.com/') as res:
token = get_shared_data(res.text)['config']['csrf_token']
self.session.headers['X-CSRFToken'] = token
self.rhx = get_shared_data(res.text).get('rhx_gis', '')
sData = get_shared_data(res.text)
if sData:
token = None
if 'config' in sData:
token = sData['config']['csrf_token']

self.session.headers['X-CSRFToken'] = token
self.rhx = sData.get('rhx_gis', '')

@abc.abstractmethod
def pages(self):
Expand Down