-
-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
f2a043b
commit 17e635b
Showing
6 changed files
with
104 additions
and
7 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,2 +1,4 @@ | ||
.vscode/ | ||
**/__pycache__ | ||
cookies.txt | ||
instagram.sess |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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(): | ||
default_cookiefile = { | ||
"Windows": "~/AppData/Roaming/Mozilla/Firefox/Profiles/*/cookies.sqlite", | ||
"Darwin": "~/Library/Application Support/Firefox/Profiles/*/cookies.sqlite", | ||
}.get(system(), "~/.mozilla/firefox/*/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("Using cookies from {}.".format(cookiefile)) | ||
conn = connect(f"file:{cookiefile}?immutable=1", uri=True) | ||
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("Imported session cookie for {}.".format(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("Cookie import failed: {}".format(e)) |
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters