Git Product home page Git Product logo

Comments (2)

Showmaster3000 avatar Showmaster3000 commented on June 12, 2024

The challenge thing has problems since longer time which haven't been solved. Same thing as in #1787 or #2027.
You can grab the session from your browser as descibed in #1787 or use the parameter --load-cookies (requires preview version 4.11) and (re-)use saved sessionfile with instaloader.

from instaloader.

windviki avatar windviki commented on June 12, 2024

Based on the #1787 script, I write a new script to import hand-made cookies and it works.
First, use Chrome or Firefox to login and use developer tools to find out your cookies (or copy as HAR to find out cookies).
Then, write them into a txt file "cookies.txt" with JSON format. e,g.,

[
            {
              "name": "dpr",
              "value": "1.31243",
              "path": "/",
              "domain": ".instagram.com",
              "expires": "2024-03-02T03:41:01.000Z",
              "httpOnly": false,
              "secure": true,
              "sameSite": "None"
            },
            {
              "name": "mid",
              "value": "xxxxxxxxxx3UmPXDZxxxxxxH3K",
              "path": "/",
              "domain": ".instagram.com",
              "expires": "2025-03-30T03:40:37.752Z",
              "httpOnly": false,
              "secure": true
            }
]

The use this script to import it automatically. You can modify the cookie parsing part to support other formats you like:

from argparse import ArgumentParser
from glob import glob
from os.path import expanduser
from platform import system
import json

try:
    from instaloader import ConnectionException, Instaloader, get_default_session_filename
except ModuleNotFoundError:
    raise SystemExit("Instaloader not found.\n  pip install [--user] instaloader")


def import_session():
    cookie_data = {items["name"]:items["value"] for items in json.load(open("cookies.txt", "r"))}
    print("Using cookies {}.".format(cookie_data))
    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("Imported session cookie for {}.".format(username))
    instaloader.context.username = username
    sessionfile = get_default_session_filename(username)
    instaloader.save_session_to_file(sessionfile)
    print("Saved session to {}.".format(sessionfile))


if __name__ == "__main__":
    try:
        import_session()
    except (ConnectionException, OperationalError) as e:
        raise SystemExit("Cookie import failed: {}".format(e))

from instaloader.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.