Skip to content

Commit

Permalink
Add proper fix for #154
Browse files Browse the repository at this point in the history
  • Loading branch information
Martin Larralde committed Apr 3, 2018
1 parent 12e3de1 commit 65d61b2
Showing 1 changed file with 35 additions and 30 deletions.
65 changes: 35 additions & 30 deletions instalooter/looters.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@

import abc
import atexit
import copy
import functools
import random
import threading
Expand All @@ -18,7 +19,7 @@
import six
from requests import Session
from six.moves.queue import Queue
from six.moves.http_cookiejar import CookieJar, LWPCookieJar
from six.moves.http_cookiejar import FileCookieJar, LWPCookieJar

from . import __author__, __name__ as __appname__, __version__
from ._impl import length_hint
Expand Down Expand Up @@ -90,10 +91,10 @@ def _init_session(cls, session=None):
session.cookies = LWPCookieJar(
cls._cachefs.getsyspath(cls._COOKIE_FILE))
try:
typing.cast(CookieJar, session.cookies).load()
typing.cast(FileCookieJar, session.cookies).load()
except IOError:
pass
typing.cast(CookieJar, session).cookies.clear_expired_cookies()
typing.cast(FileCookieJar, session.cookies).clear_expired_cookies()
return session

@classmethod
Expand All @@ -112,36 +113,42 @@ 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/"
data = {'username': username, 'password': password}

session.headers.update({
'Host': 'www.instagram.com',
'Origin': 'https://www.instagram.com',
'Referer': 'https://www.instagram.com',
'User-Agent': cls._user_agents.firefox,
'X-Instagram-AJAX': '1',
'X-Requested-With': 'XMLHttpRequest'
})

with session.get(homepage) as res:
session.headers.update({'X-CSRFToken': res.cookies['csrftoken']})
time.sleep(5 * random.random()) # nosec

with session.post(login_url, data=data, allow_redirects=True) as login:
session.headers.update({'X-CSRFToken': login.cookies['csrftoken']})
try:
session.headers.update({
'Host': 'www.instagram.com',
'Origin': 'https://www.instagram.com',
'Referer': 'https://www.instagram.com',
'User-Agent': cls._user_agents.firefox,
'X-Instagram-AJAX': '1',
'X-Requested-With': 'XMLHttpRequest'
})

with session.get(homepage) as res:
token = res.cookies['csrftoken']
session.headers.update({'X-CSRFToken': token})
time.sleep(5 * random.random()) # nosec
if not login.status_code == 200:
raise SystemError("Login error: check your connection")

with session.get(homepage) as res:
if res.text.find(username) == -1:
raise ValueError('Login error: check your login data')
try:
session.cookies.save() # type: ignore
except IOError:
pass
with session.post(login_url, data, allow_redirects=True) as login:
token = login.cookies['csrftoken']
session.headers.update({'X-CSRFToken': token})
time.sleep(5 * random.random()) # nosec
if not login.status_code == 200:
raise SystemError("Login error: check your connection")

with session.get(homepage) as res:
if res.text.find(username) == -1:
raise ValueError('Login error: check your login data')
try:
typing.cast(FileCookieJar, session.cookies).save()
except IOError:
pass
finally:
session.headers = headers

@classmethod
def _logout(cls, session=None):
Expand Down Expand Up @@ -197,7 +204,7 @@ def _sessionid(cls, session=None):
"""
_session = cls._init_session(session)
_cookies = typing.cast(CookieJar, _session.cookies)
_cookies = typing.cast(FileCookieJar, _session.cookies)
return next((ck.value for ck in _cookies
if ck.domain == "www.instagram.com"
and ck.name == "sessionid"
Expand Down Expand Up @@ -465,8 +472,6 @@ def login(self, username, password):
"""
self._login(username, password, session=self.session)
# workaround of #154
self.session = self._init_session()

def logout(self):
# type: () -> None
Expand Down

0 comments on commit 65d61b2

Please sign in to comment.