1
0
mirror of https://github.com/instaloader/instaloader.git synced 2024-11-04 09:22:29 +01:00

Encode username in default session file name

Fixes #7.
This commit is contained in:
Alexander Graf 2016-07-26 17:36:21 +02:00
parent a99d1875e5
commit c69214536a

View File

@ -1,11 +1,9 @@
#!/usr/bin/env python3 #!/usr/bin/env python3
import re, json, datetime, shutil, os, time, random, sys, pickle, getpass import re, json, datetime, shutil, os, time, random, sys, pickle, getpass, tempfile
from argparse import ArgumentParser from argparse import ArgumentParser
import requests, requests.utils import requests, requests.utils
DEFAULTSESSIONFILE = "/tmp/.instaloadersession"
class InstaloaderException(Exception): class InstaloaderException(Exception):
"""Base exception for this script""" """Base exception for this script"""
pass pass
@ -134,17 +132,26 @@ def download_profilepic(name, url, quiet=False):
else: else:
raise ConnectionException("file \'" + url + "\' could not be downloaded") raise ConnectionException("file \'" + url + "\' could not be downloaded")
def save_session(session, filename, quiet=False): def get_default_session_filename(username):
dirname = tempfile.gettempdir() + "/" + ".instaloader-" + getpass.getuser()
filename = dirname + "/" + "session-" + username
return filename
def save_session(session, username, filename, quiet=False):
if filename is None: if filename is None:
filename = DEFAULTSESSIONFILE filename = get_default_session_filename(username)
dirname = os.path.dirname(filename)
if not os.path.exists(dirname):
os.makedirs(dirname)
os.chmod(dirname, 0o700)
with open(filename, 'wb') as sessionfile: with open(filename, 'wb') as sessionfile:
os.chmod(filename, 0o600) os.chmod(filename, 0o600)
pickle.dump(requests.utils.dict_from_cookiejar(session.cookies), sessionfile) pickle.dump(requests.utils.dict_from_cookiejar(session.cookies), sessionfile)
log("Saved session to %s." % filename, quiet=quiet) log("Saved session to %s." % filename, quiet=quiet)
def load_session(filename, quiet=False): def load_session(username, filename, quiet=False):
if filename is None: if filename is None:
filename = DEFAULTSESSIONFILE filename = get_default_session_filename(username)
try: try:
with open(filename, 'rb') as sessionfile: with open(filename, 'rb') as sessionfile:
session = requests.Session() session = requests.Session()
@ -285,7 +292,7 @@ def download_profiles(targets, username=None, password=None, sessionfile=None,
# pylint:disable=too-many-arguments # pylint:disable=too-many-arguments
# Login, if desired # Login, if desired
if username is not None: if username is not None:
session = load_session(sessionfile, quiet=quiet) session = load_session(username, sessionfile, quiet=quiet)
if not test_login(username, session): if not test_login(username, session):
session = get_logged_in_session(username, password, quiet) session = get_logged_in_session(username, password, quiet)
log("Logged in as %s." % username, quiet=quiet) log("Logged in as %s." % username, quiet=quiet)
@ -308,7 +315,7 @@ def download_profiles(targets, username=None, password=None, sessionfile=None,
", ".join(failedtargets), file=sys.stderr) ", ".join(failedtargets), file=sys.stderr)
# Save session if it is useful # Save session if it is useful
if username is not None: if username is not None:
save_session(session, sessionfile, quiet=quiet) save_session(session, username, sessionfile, quiet=quiet)
def main(): def main():
parser = ArgumentParser(description='Simple downloader to fetch all Instagram pics and '\ parser = ArgumentParser(description='Simple downloader to fetch all Instagram pics and '\
@ -319,7 +326,8 @@ def main():
parser.add_argument('-p', '--password', parser.add_argument('-p', '--password',
help='Provide password for your Instagram account') help='Provide password for your Instagram account')
parser.add_argument('-f', '--sessionfile', parser.add_argument('-f', '--sessionfile',
help='File to store session key, defaults to '+DEFAULTSESSIONFILE) help='File to store session key, defaults to '+ \
get_default_session_filename("<login_name>"))
parser.add_argument('-P', '--profile-pic-only', action='store_true', parser.add_argument('-P', '--profile-pic-only', action='store_true',
help='Only download profile picture') help='Only download profile picture')
parser.add_argument('-V', '--skip-videos', action='store_true', parser.add_argument('-V', '--skip-videos', action='store_true',