From ebff2205b173a5400e5ef17c3e3bcbdfffeb766d Mon Sep 17 00:00:00 2001 From: Alexander Graf Date: Fri, 29 Jul 2016 17:59:07 +0200 Subject: [PATCH] @ to download all followees of profile This closes #10. --- README.md | 5 +++++ instaloader.py | 26 +++++++++++++++++++++----- 2 files changed, 26 insertions(+), 5 deletions(-) diff --git a/README.md b/README.md index 31f3a65..aba7ff6 100644 --- a/README.md +++ b/README.md @@ -29,6 +29,11 @@ When invoked like this, it also **stores the session cookies** in a file in `/tm reused later when `--login` is given. So you can download private profiles **non-interactively** when you already have a valid session cookies file. +If you want to **download all followees of a given profile**, call +``` +./instaloader.py --login=your_username @profile +``` + The `--quiet` option makes it also **suitable as a cron job**. To get a list of other helpful flags, run `./instaloader.py --help`. diff --git a/instaloader.py b/instaloader.py index 045d049..1711859 100755 --- a/instaloader.py +++ b/instaloader.py @@ -367,11 +367,11 @@ def get_logged_in_session(username, password=None, quiet=False): print(err, file=sys.stderr) password = None -def download_profiles(targets, username=None, password=None, sessionfile=None, +def download_profiles(profilelist, username=None, password=None, sessionfile=None, profile_pic_only=False, download_videos=True, fast_update=False, sleep_min_max=[0.25,2], quiet=False): """Download set of profiles and handle sessions""" - # pylint:disable=too-many-arguments + # pylint:disable=too-many-arguments,too-many-branches,too-many-locals # Login, if desired if username is not None: session = load_session(username, sessionfile, quiet=quiet) @@ -380,9 +380,23 @@ def download_profiles(targets, username=None, password=None, sessionfile=None, log("Logged in as %s." % username, quiet=quiet) else: session = get_anonymous_session() - # Iterate through targets list and download them + # Try block for KeyboardInterrupt (save session on ^C) failedtargets = [] + targets = set() try: + # Generate set of targets + for pentry in profilelist: + if pentry[0] == '@': + log("Retrieving followees of %s..." % pentry[1:], quiet=quiet) + followees = get_followees(pentry[1:], session) + targets.update([followee['username'] for followee in followees]) + else: + targets.add(pentry) + if len(targets) == 0: + log("No profiles to download given.", quiet=quiet) + elif len(targets) > 1: + log("Downloading %i profiles..." % len(targets), quiet=quiet) + # Iterate through targets list and download them for target in targets: try: download(target, session, profile_pic_only, download_videos, @@ -402,7 +416,9 @@ def download_profiles(targets, username=None, password=None, sessionfile=None, def main(): parser = ArgumentParser(description='Simple downloader to fetch all Instagram pics and '\ 'captions from a given profile') - parser.add_argument('targets', nargs='+', help='Names of profiles to download') + parser.add_argument('profile', nargs='*', + help='Name of profile to download, or @ to download all followees of '\ + '') parser.add_argument('--version', action='version', version='1.0') parser.add_argument('-l', '--login', metavar='login_name', @@ -425,7 +441,7 @@ def main(): 'if login credentials are needed but not given.') args = parser.parse_args() try: - download_profiles(args.targets, args.login, args.password, args.sessionfile, + download_profiles(args.profile, args.login, args.password, args.sessionfile, args.profile_pic_only, not args.skip_videos, args.fast_update, [0,0] if args.no_sleep else [0.25,2], args.quiet) except InstaloaderException as err: