Move Instaloader.main method to module-level _main

This commit is contained in:
Alexander Graf 2018-04-05 10:02:56 +02:00
parent c350847b50
commit 3511811090

View File

@ -1669,7 +1669,9 @@ class Instaloader:
print(err, file=sys.stderr) print(err, file=sys.stderr)
password = None password = None
def main(self, targetlist: List[str], username: Optional[str] = None, password: Optional[str] = None,
def _main(instaloader: Instaloader, targetlist: List[str],
username: Optional[str] = None, password: Optional[str] = None,
sessionfile: Optional[str] = None, max_count: Optional[int] = None, sessionfile: Optional[str] = None, max_count: Optional[int] = None,
profile_pic: bool = True, profile_pic_only: bool = False, profile_pic: bool = True, profile_pic_only: bool = False,
fast_update: bool = False, fast_update: bool = False,
@ -1679,23 +1681,23 @@ class Instaloader:
# Parse and generate filter function # Parse and generate filter function
if filter_str is not None: if filter_str is not None:
filter_func = filterstr_to_filterfunc(filter_str, username is not None) filter_func = filterstr_to_filterfunc(filter_str, username is not None)
self.context.log('Only download posts with property "{}".'.format(filter_str)) instaloader.context.log('Only download posts with property "{}".'.format(filter_str))
else: else:
filter_func = None filter_func = None
# Login, if desired # Login, if desired
if username is not None: if username is not None:
try: try:
self.load_session_from_file(username, sessionfile) instaloader.load_session_from_file(username, sessionfile)
except FileNotFoundError as err: except FileNotFoundError as err:
if sessionfile is not None: if sessionfile is not None:
print(err, file=sys.stderr) print(err, file=sys.stderr)
self.context.log("Session file does not exist yet - Logging in.") instaloader.context.log("Session file does not exist yet - Logging in.")
if not self.context.is_logged_in or username != self.test_login(): if not instaloader.context.is_logged_in or username != instaloader.test_login():
if password is not None: if password is not None:
self.login(username, password) instaloader.login(username, password)
else: else:
self.interactive_login(username) instaloader.interactive_login(username)
self.context.log("Logged in as %s." % username) instaloader.context.log("Logged in as %s." % username)
# Try block for KeyboardInterrupt (save session on ^C) # Try block for KeyboardInterrupt (save session on ^C)
profiles = set() profiles = set()
try: try:
@ -1703,37 +1705,37 @@ class Instaloader:
for target in targetlist: for target in targetlist:
# strip '/' characters to be more shell-autocompletion-friendly # strip '/' characters to be more shell-autocompletion-friendly
target = target.rstrip('/') target = target.rstrip('/')
with self.context.error_catcher(target): with instaloader.context.error_catcher(target):
if target[0] == '@': if target[0] == '@':
self.context.log("Retrieving followees of %s..." % target[1:]) instaloader.context.log("Retrieving followees of %s..." % target[1:])
profiles.update([followee['username'] for followee in self.get_followees(target[1:])]) profiles.update([followee['username'] for followee in instaloader.get_followees(target[1:])])
elif target[0] == '#': elif target[0] == '#':
self.download_hashtag(hashtag=target[1:], max_count=max_count, fast_update=fast_update, instaloader.download_hashtag(hashtag=target[1:], max_count=max_count, fast_update=fast_update,
filter_func=filter_func) filter_func=filter_func)
elif target == ":feed": elif target == ":feed":
self.download_feed_posts(fast_update=fast_update, max_count=max_count, instaloader.download_feed_posts(fast_update=fast_update, max_count=max_count,
filter_func=filter_func) filter_func=filter_func)
elif target == ":stories": elif target == ":stories":
self.download_stories(fast_update=fast_update) instaloader.download_stories(fast_update=fast_update)
elif target == ":saved": elif target == ":saved":
self.download_saved_posts(fast_update=fast_update, max_count=max_count, instaloader.download_saved_posts(fast_update=fast_update, max_count=max_count,
filter_func=filter_func) filter_func=filter_func)
else: else:
profiles.add(target) profiles.add(target)
if len(profiles) > 1: if len(profiles) > 1:
self.context.log("Downloading {} profiles: {}".format(len(profiles), ','.join(profiles))) instaloader.context.log("Downloading {} profiles: {}".format(len(profiles), ','.join(profiles)))
# Iterate through profiles list and download them # Iterate through profiles list and download them
for target in profiles: for target in profiles:
with self.context.error_catcher(target): with instaloader.context.error_catcher(target):
try: try:
self.download_profile(target, profile_pic, profile_pic_only, fast_update, stories, stories_only, instaloader.download_profile(target, profile_pic, profile_pic_only, fast_update, stories, stories_only,
filter_func=filter_func) filter_func=filter_func)
except ProfileNotExistsException as err: except ProfileNotExistsException as err:
if not self.context.is_logged_in: if not instaloader.context.is_logged_in:
self.context.log(err) instaloader.context.log(err)
self.context.log("Trying again anonymously, helps in case you are just blocked.") instaloader.context.log("Trying again anonymously, helps in case you are just blocked.")
with self.anonymous_copy() as anonymous_loader: with instaloader.anonymous_copy() as anonymous_loader:
with self.context.error_catcher(): with instaloader.context.error_catcher():
anonymous_loader.download_profile(target, profile_pic, profile_pic_only, anonymous_loader.download_profile(target, profile_pic, profile_pic_only,
fast_update, filter_func=filter_func) fast_update, filter_func=filter_func)
else: else:
@ -1741,16 +1743,16 @@ class Instaloader:
except KeyboardInterrupt: except KeyboardInterrupt:
print("\nInterrupted by user.", file=sys.stderr) print("\nInterrupted by user.", file=sys.stderr)
# Save session if it is useful # Save session if it is useful
if self.context.is_logged_in: if instaloader.context.is_logged_in:
self.save_session_to_file(sessionfile) instaloader.save_session_to_file(sessionfile)
# User might be confused if Instaloader does nothing # User might be confused if Instaloader does nothing
if not targetlist: if not targetlist:
if self.context.is_logged_in: if instaloader.context.is_logged_in:
# Instaloader did at least save a session file # Instaloader did at least save a session file
self.context.log("No targets were specified, thus nothing has been downloaded.") instaloader.context.log("No targets were specified, thus nothing has been downloaded.")
else: else:
# Instloader did not do anything # Instloader did not do anything
self.context.log("usage:"+USAGE_STRING) instaloader.context.log("usage:" + USAGE_STRING)
def main(): def main():
@ -1895,7 +1897,8 @@ def main():
download_geotags=download_geotags, download_geotags=download_geotags,
save_captions=save_captions, download_comments=download_comments, save_captions=save_captions, download_comments=download_comments,
save_metadata=save_metadata, max_connection_attempts=args.max_connection_attempts) save_metadata=save_metadata, max_connection_attempts=args.max_connection_attempts)
loader.main(args.profile, _main(loader,
args.profile,
username=args.login.lower() if args.login is not None else None, username=args.login.lower() if args.login is not None else None,
password=args.password, password=args.password,
sessionfile=args.sessionfile, sessionfile=args.sessionfile,