From 27b7c98be2beaf8af899d53e7ab30c373872a598 Mon Sep 17 00:00:00 2001 From: Alexander Graf <17130992+aandergr@users.noreply.github.com> Date: Wed, 3 Jun 2020 17:14:08 +0200 Subject: [PATCH] Update doc section Troubleshooting -> Login Error New Firefox cookie import script, based on a comment in #615: https://github.com/instaloader/instaloader/issues/615#issuecomment-630127064 --- .../615_import_firefox_session.py | 53 +++++++++++++++++++ .../codesnippets/92_import_firefox_session.py | 23 -------- docs/conf.py | 25 ++++++--- docs/troubleshooting.rst | 36 ++++++++----- 4 files changed, 93 insertions(+), 44 deletions(-) create mode 100644 docs/codesnippets/615_import_firefox_session.py delete mode 100644 docs/codesnippets/92_import_firefox_session.py diff --git a/docs/codesnippets/615_import_firefox_session.py b/docs/codesnippets/615_import_firefox_session.py new file mode 100644 index 0000000..84630bc --- /dev/null +++ b/docs/codesnippets/615_import_firefox_session.py @@ -0,0 +1,53 @@ +from argparse import ArgumentParser +from glob import glob +from os.path import expanduser +from platform import system +from sqlite3 import OperationalError, connect + +try: + from instaloader import ConnectionException, Instaloader +except ModuleNotFoundError: + raise SystemExit("Instaloader not found.\n pip install [--user] instaloader") + + +def get_cookiefile() -> str: + default_cookiefile = { + "Windows": "~/AppData/Roaming/Mozilla/Firefox/Profiles/*/cookies.sqlite", + "Darwin": "~/Library/Application Support/Firefox/Profiles/*.default/cookies.sqlite", + }.get(system(), "~/.mozilla/firefox/*.default/cookies.sqlite") + cookiefiles = glob(expanduser(default_cookiefile)) + if not cookiefiles: + raise SystemExit("No Firefox cookies.sqlite file found. Use -c COOKIEFILE.") + return cookiefiles[0] + + +def import_session(cookiefile, sessionfile): + print(f"Using cookies from {cookiefile}.") + conn = connect(cookiefile) + try: + cookie_data = conn.execute( + "SELECT name, value FROM moz_cookies WHERE baseDomain='instagram.com'" + ) + except OperationalError: + cookie_data = conn.execute( + "SELECT name, value FROM moz_cookies WHERE host LIKE '%instagram.com'" + ) + instaloader = Instaloader(max_connection_attempts=1) + instaloader.context._session.cookies.update(cookie_data) + username = instaloader.test_login() + if not username: + raise SystemExit("Not logged in. Are you logged in successfully in Firefox?") + print(f"Imported session cookie for {username}.") + instaloader.context.username = username + instaloader.save_session_to_file(sessionfile) + + +if __name__ == "__main__": + p = ArgumentParser() + p.add_argument("-c", "--cookiefile") + p.add_argument("-f", "--sessionfile") + args = p.parse_args() + try: + import_session(args.cookiefile or get_cookiefile(), args.sessionfile) + except (ConnectionException, OperationalError) as e: + raise SystemExit(f"Cookie import failed: {e}") diff --git a/docs/codesnippets/92_import_firefox_session.py b/docs/codesnippets/92_import_firefox_session.py deleted file mode 100644 index 0e05d2d..0000000 --- a/docs/codesnippets/92_import_firefox_session.py +++ /dev/null @@ -1,23 +0,0 @@ -from glob import glob -from os.path import expanduser -from sqlite3 import connect - -from instaloader import ConnectionException, Instaloader - -# FIREFOXCOOKIEFILE = "/home/alex/.mozilla/firefox/l96w6b90.default/cookies.sqlite" -FIREFOXCOOKIEFILE = glob(expanduser("~/.mozilla/firefox/*.default/cookies.sqlite"))[0] - -instaloader = Instaloader(max_connection_attempts=1) -instaloader.context._session.cookies.update(connect(FIREFOXCOOKIEFILE) - .execute("SELECT name, value FROM moz_cookies " - "WHERE baseDomain='instagram.com'")) - -try: - username = instaloader.test_login() - if not username: - raise ConnectionException() -except ConnectionException: - raise SystemExit("Cookie import failed. Are you logged in successfully in Firefox?") - -instaloader.context.username = username -instaloader.save_session_to_file() diff --git a/docs/conf.py b/docs/conf.py index 054e980..0af84cf 100644 --- a/docs/conf.py +++ b/docs/conf.py @@ -49,7 +49,22 @@ autodoc_member_order = 'bysource' intersphinx_mapping = {'python': ('https://docs.python.org/3', None), 'requests': ('https://requests.kennethreitz.org/en/master/', None)} -extlinks = {'issue': ('https://github.com/instaloader/instaloader/issues/%s', 'issue #')} +current_release = subprocess.check_output(["git", "describe", "--abbrev=0"]).decode("ascii")[1:-1] +current_release_date = subprocess.check_output( + ["git", "log", "-1", "--tags", "--format=%ad", "--date=format:%e %b %Y"]).decode("ascii")[:-1] + +html_context = {'current_release': current_release, 'current_release_date': current_release_date} + +extlinks = { + 'issue': ( + 'https://github.com/instaloader/instaloader/issues/%s', + 'Issue #' + ), + 'example': ( + 'https://raw.githubusercontent.com/instaloader/instaloader/v' + current_release + '/docs/codesnippets/%s', + 'Example ' + ), +} # Add any paths that contain templates here, relative to this directory. templates_path = ['_templates'] @@ -69,7 +84,7 @@ master_doc = 'index' # General information about the project. project = 'Instaloader' -copyright = '2017, Alexander Graf and Andre Koch-Kramer' +copyright = '2017-2020, Alexander Graf and Andre Koch-Kramer' author = 'Alexander Graf and Andre Koch-Kramer' # The version info for the project you're documenting, acts as replacement for @@ -353,12 +368,6 @@ texinfo_documents = [ # texinfo_no_detailmenu = False -current_release = subprocess.check_output(["git", "describe", "--abbrev=0"]).decode("ascii")[1:-1] -current_release_date = subprocess.check_output(["git", "log", "-1", "--tags", "--format=%ad", "--date=format:%e %b %Y"]).decode("ascii")[:-1] - -html_context = {'current_release': current_release, 'current_release_date': current_release_date} - - def setup(app): typing.TYPE_CHECKING = True app.add_stylesheet("instaloader.css") diff --git a/docs/troubleshooting.rst b/docs/troubleshooting.rst index 3069c8e..6bc6cbd 100644 --- a/docs/troubleshooting.rst +++ b/docs/troubleshooting.rst @@ -32,7 +32,7 @@ rate limit has almost been reached, according to Instaloader's own rate accounting mechanism. We regularly adjust this mechanism to match Instagram's current rate limiting. -Login error +Login Error ----------- Instaloader's login *should* work fine, both with and without @@ -40,21 +40,31 @@ Two-Factor-Authentication. It also supports handling the *checkpoint challenge*, issued when Instagram suspects authentication activity on your account, by pointing the user to an URL to be opened in a browser. -Nevertheless, in :issue:`92` users report problems with logging in. To still use -Instaloader's logged-in functionality, you may use the following script to -workaround login problems by importing the session cookies from Firefox and -bypassing Instaloader's login. +Nevertheless, in :issue:`92` and :issue:`615` users reported problems with +logging in. We recommend to always keep the sessionfile which Instaloader +creates when using :option:`--login`. If a sessionfile is present, +:option:`--login` does not make make use of the failure-prone login procedure. +Also, sessionfiles usually do not expire and can be copied between different +computers without any problems. -.. literalinclude:: codesnippets/92_import_firefox_session.py +If you do not have a sessionfile present, you may use the following script +(:example:`615_import_firefox_session.py`) to workaround login problems by +importing the session cookies from Firefox and bypassing Instaloader's login and +so still use Instaloader's logged-in functionality. -To use this, +.. literalinclude:: codesnippets/615_import_firefox_session.py -#. login to Instagram in Firefox, +To use this script, -#. execute the snippet, +#. Download the script: :example:`615_import_firefox_session.py`, -#. then, ``instaloader -l USERNAME`` should work fine. +#. Login to Instagram in Firefox, -If you do not use your default firefox profile, or your operating system has the -paths differently set up, you may have to alter the ``FIREFOXCOOKIEFILE`` -variable first. +#. Execute the snippet, e.g. with ``python 615_import_firefox_session.py``, + +#. Then, ``instaloader -l USERNAME`` should work fine. + +This script also supports specifying a cookiefile path, which may be useful if +you use multiple Firefox profiles or if your operating system has the directory +structure differently set up. Also, you can specify an alternative sessionfile +path.